Skip to content

Instantly share code, notes, and snippets.

@kirillsulim
Created October 24, 2018 13:30
Show Gist options
  • Save kirillsulim/b7802a709cea1648ffd303474ea0acbd to your computer and use it in GitHub Desktop.
Save kirillsulim/b7802a709cea1648ffd303474ea0acbd to your computer and use it in GitHub Desktop.
public static <T> Validator<T extends Comparable<T>> positiveValidator(T zeroValue) {
return value -> {
if (zeroValue.compareTo(value) < 0) {
return Optional.empty()
}
else {
rerurn Optional.of(String.format("Value must be positive but was '%s'", value));
};
}
public static Validator<Integer> positiveValidator() {
return positiveValidator(0);
}
public static Validator<Long> positiveValidator() {
return positiveValidator(0L);
}
// VS
public static Validator<Long> positive() {
return value -> {
if (value > 0) {
return Optional.empty();
}
return Optional.of("Value should be positive but was " + value);
};
}
public static Validator<Integer> positive() {
return value -> {
if (value > 0) {
return Optional.empty();
}
return Optional.of("Value should be positive but was " + value);
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment