Skip to content

Instantly share code, notes, and snippets.

@naimdjon
Created December 16, 2022 13:12
Show Gist options
  • Save naimdjon/e2d97e0a71b0e9578330a96ba56c9046 to your computer and use it in GitHub Desktop.
Save naimdjon/e2d97e0a71b0e9578330a96ba56c9046 to your computer and use it in GitHub Desktop.
Validated.java
public final class Validated<E, V> {
private final E validationError;
private final V value;
private Validated(E e, V v) {
this.validationError = e;
this.value = v;
checkState();
}
public static <E, V> Validated<E,V> valid(V v) {
return new Validated<>(null, v);
}
public static <E, V> Validated<E,V> invalid(E e) {
return new Validated<>(e, null);
}
public boolean isValid() {
return this.value!=null && this.validationError==null;
}
public boolean isInvalid() {
return this.value==null && this.validationError!=null;
}
private void checkState() {
if (isValid() == isInvalid()) {
throw new IllegalArgumentException("ValidationError = " + this.validationError + ", value=" + this.value);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment