I have the below default method in an interface and it seems to be pretty complex because of the many if-else conditions.
default void validate() {
Application application = application().get();
if (StringUtils.isEmpty(application.name()) || application.data() == null) {
throw new IllegalArgumentException());
} else {
for (Form form : application.data().forms) {
if (StringUtils.isEmpty(form.getId())) {
throw new IllegalArgumentException();
}
for (Question question : form.getQuestions()) {
if (StringUtils.isEmpty(question.getQuestion())
|| StringUtils.isEmpty(question.getValue())) {
throw new IllegalArgumentException();
}
}
}
}
}
I want to refactor this into different methods to reduce the complexity. However since the project is configured to use Java8, private methods cannot be used in the interface. How else can I break this down and reduce the complexity? The reason for this being an interface is because I'm using Immutables library and Jackson Json to deserialize a Json string into a Java object. Therefore I'd like to keep this an interface. Any advice would be much appreciated.
ApplicationData
(the type of the return value ofapplication.data()
) should itself have avalidate()
method, which in turn calls avalidate()
method onQuestion
, and so on.else
after athrow
at all?