Мне нужна помощь в улучшении кода проверки ошибок. Итак, в моем примере ниже у меня есть три метода, которые должны проверить полученные параметры. Но я хотел бы улучшить это, учитывая некоторые моменты:
1) Centralize tests in a single class;
2) Return a specific error for each case, for example:
ObjectReceived.one == null, message ( "Parameter one must not be null")
3) Reduce ifs number and code repetition
У меня есть несколько примеров для inte rnet like (Java - Specifi c ошибка проверки ), но это не относится к моему случаю, или, по крайней мере, я не смог.
заранее спасибо!
public class MyClassDAO extends GenericDAO {
public void methodoOne (Object objectReceived, String parameter1) {
if ( objectReceived != null && !parameter1.isEmpty() ) {
if ( !objectReceived.one() && !objectReceived.two() && !objectReceived.three() ) {
// to do it
} else {
throw new IllegalArgumentException();
}
}
}
public void methodoTwo (Object objectReceived, String parameter2) {
if ( objectReceived != null && !parameter2.isEmpty() ) {
if ( !objectReceived.one().isEmpty() && !objectReceived.two().isEmpty() &&
!objectReceived.three().isEmpty() && !objectReceived.four().isEmpty() ) {
// to do it
} else {
throw new IllegalArgumentException();
}
}
}
public void methodoThree (String parameter) {
if ( parameter3.isEmpty() ) {
throw new IllegalArgumentException();
}
}
}