Skip to content

Instantly share code, notes, and snippets.

@jandk
Last active February 9, 2021 22:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jandk/e90f82923e4e467d943b1bd2d97bb1a8 to your computer and use it in GitHub Desktop.
Save jandk/e90f82923e4e467d943b1bd2d97bb1a8 to your computer and use it in GitHub Desktop.
So don't be lazy, and use value types!
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