Last active
February 9, 2021 22:16
-
-
Save jandk/e90f82923e4e467d943b1bd2d97bb1a8 to your computer and use it in GitHub Desktop.
So don't be lazy, and use value types!
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
import com.fasterxml.jackson.annotation.JsonCreator; | |
import com.fasterxml.jackson.annotation.JsonValue; | |
import java.util.Objects; | |
public abstract class ValueOf<T> { | |
private final T value; | |
@JsonCreator | |
protected ValueOf(T value) { | |
this.value = value; | |
validate(); | |
} | |
protected void validate() { | |
} | |
@JsonValue | |
public final T get() { | |
return value; | |
} | |
@Override | |
public final boolean equals(Object obj) { | |
if (this == obj) return true; | |
if (obj == null || getClass() != obj.getClass()) return false; | |
return Objects.equals(value, ((ValueOf<?>) obj).value); | |
} | |
@Override | |
public final int hashCode() { | |
return Objects.hashCode(value); | |
} | |
@Override | |
public final String toString() { | |
return Objects.toString(value); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment