Last active
November 19, 2017 18:03
-
-
Save Anrimian/88279147e9c11bc50b33a0e476dce397 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public enum Field { | |
SOMETHING | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ValidateError { | |
private Field field; | |
public ValidateError(Field field) { | |
this.field = field; | |
} | |
public Field getField() { | |
return field; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ValidateException extends RuntimeException { | |
private List<ValidateError> validateErrors; | |
ValidateException(List<ValidateError> validateErrors) { | |
this.validateErrors = validateErrors; | |
} | |
public List<ValidateError> getValidateErrors() { | |
return validateErrors; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ValidateResultModel<T> { | |
private T model; | |
private List<ValidateError> validateErrors; | |
ValidateResultModel(T model, List<ValidateError> validateErrors) { | |
this.model = model; | |
this.validateErrors = validateErrors; | |
} | |
T getModel() { | |
return model; | |
} | |
List<ValidateError> getValidateErrors() { | |
return validateErrors; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class Validator<Model> { | |
public Single<Model> validate(Model model) { | |
return Single.just(model) | |
.map(this::validateModel) | |
.flatMap(this::processResult); | |
} | |
private Single<Model> processResult(ValidateResultModel<Model> validateResultModel) { | |
return Single.create(subscriber -> { | |
List<ValidateError> validateErrors = validateResultModel.getValidateErrors(); | |
if (validateErrors.isEmpty()) { | |
subscriber.onSuccess(validateResultModel.getModel()); | |
} else { | |
subscriber.onError(new ValidateException(validateErrors)); | |
} | |
}); | |
} | |
private ValidateResultModel<Model> validateModel(Model model) { | |
List<ValidateError> errors = new LinkedList<>(); | |
for (ValidateFunction validateFunctions : getValidateFunctions(model)) { | |
ValidateError error = validateFunctions.validate(); | |
if (error != null) { | |
errors.add(error); | |
} | |
} | |
return new ValidateResultModel<>(model, errors); | |
} | |
protected abstract List<ValidateFunction> getValidateFunctions(Model model); | |
protected interface ValidateFunction { | |
@Nullable | |
ValidateError validate(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment