Редактировать : как вы упомянули, вы проверили Exception
s, которые могут быть брошены и аргументы в вашем методе. Поскольку Runnable
не объявляет, что может выдать Exceptions
, а также не принимает никаких параметров, вам необходимо создать свои FunctionalInterface
(нажмите здесь , чтобы увидеть, что они на самом деле):
public interface ThrowingRunnable {
void run(IOutputWriter writer, String fileName, InputStream fileInputStream) throws IOException;
}
Тогда вам просто нужно заменить Runnable
на ThrowingRunnable
в моем предложенном ранее коде, и у вас все будет хорошо.
Вы можете создать отображение HwErrorsEtlConstants
для конкретного метода (использует Java 8):
static final Map<String, Runnable> MAPPING;
static {
Map<String, Runnable> temp = new HashMap<>();
temp.put(HwErrorsEtlConstants.MR_RAW_GRADIENT_ERRORS, this::method1);
temp.put(HwErrorsEtlConstants.MR_RAW_PFEI_ERRORS, this::method2);
temp.put(HwErrorsEtlConstants.MR_RAW_RFAMP_ERRORS, this::method3);
MAPPING = Collections.unmodifiableMap(temp);
}
Тогда в вашем методе вы можете использовать Stream
s, представленные также в Java 8:
// Optional indicates a potentially absent value, it's just a wrapper around a Runnable
Optional<Runnable> optional = MAPPING
// create a Stream from the entries
.entrySet().stream()
// keep the items that match the condition and drop every other
.filter(e -> filename.startsWith(e.getKey()))
// we had Map.Entry<String, Runnable>, but now we only need the value e.g. the Runnable
.map(Map.Entry::getValue)
// short circuit, e.g. we only want the first value that matches
.findFirst();
// checks if anything is present, this is used as the MAPPING "could" be empty
if(optional.isPresent()) {
// unpack the value and call it with arguments
optional.get().run(aWriter, someFileName, anInputStream);
} else {
// nothing matched, throw error or log etc.
}
Хотя, как уже упоминалось, ваше текущее решение выглядит хорошо, я думаю, вы используете Sonar для анализа кода. Иногда у сонара просто ложные срабатывания, поэтому вы также можете спокойно их игнорировать.
Далее читаем, чтобы помочь вам понять Java 8: