Created
December 16, 2022 13:12
-
-
Save naimdjon/e2d97e0a71b0e9578330a96ba56c9046 to your computer and use it in GitHub Desktop.
Validated.java
This file contains hidden or 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 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