diff --git a/changelog/@unreleased/pr-2073.v2.yml b/changelog/@unreleased/pr-2073.v2.yml new file mode 100644 index 000000000..a86762a14 --- /dev/null +++ b/changelog/@unreleased/pr-2073.v2.yml @@ -0,0 +1,5 @@ +type: improvement +improvement: + description: Simplify SafeLong implementation + links: + - https://github.com/palantir/conjure-java/pull/2073 diff --git a/conjure-lib/build.gradle b/conjure-lib/build.gradle index 855e36f74..8ca7a9f31 100644 --- a/conjure-lib/build.gradle +++ b/conjure-lib/build.gradle @@ -27,8 +27,5 @@ dependencies { testImplementation 'org.assertj:assertj-core' testImplementation 'com.palantir.safe-logging:preconditions-assertj' testImplementation 'org.junit.jupiter:junit-jupiter' - - annotationProcessor 'org.immutables:value' - compileOnly 'org.immutables:value::annotations' } diff --git a/conjure-lib/src/main/java/com/palantir/conjure/java/lib/SafeLong.java b/conjure-lib/src/main/java/com/palantir/conjure/java/lib/SafeLong.java index dd4d6927a..4ff79b24e 100644 --- a/conjure-lib/src/main/java/com/palantir/conjure/java/lib/SafeLong.java +++ b/conjure-lib/src/main/java/com/palantir/conjure/java/lib/SafeLong.java @@ -19,11 +19,9 @@ import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonValue; import com.palantir.logsafe.Preconditions; -import org.immutables.value.Value; /** A wrapper around a long which is safe for json-serialization as a number without loss of precision. */ -@Value.Immutable -public abstract class SafeLong implements Comparable { +public final class SafeLong implements Comparable { private static final long MIN_SAFE_VALUE = -(1L << 53) + 1; private static final long MAX_SAFE_VALUE = (1L << 53) - 1; @@ -31,34 +29,58 @@ public abstract class SafeLong implements Comparable { public static final SafeLong MAX_VALUE = SafeLong.of(MAX_SAFE_VALUE); public static final SafeLong MIN_VALUE = SafeLong.of(MIN_SAFE_VALUE); + private final long longValue; + + private SafeLong(long longValue) { + this.longValue = check(longValue); + } + @JsonValue - @Value.Parameter - public abstract long longValue(); + public long longValue() { + return longValue; + } - @Value.Check - protected final void check() { + private static long check(long value) { Preconditions.checkArgument( - MIN_SAFE_VALUE <= longValue() && longValue() <= MAX_SAFE_VALUE, + MIN_SAFE_VALUE <= value && value <= MAX_SAFE_VALUE, "number must be safely representable in javascript i.e. " + "lie between -9007199254740991 and 9007199254740991"); + return value; } public static SafeLong valueOf(String value) { - return SafeLong.of(Long.parseLong(value)); + return of(Long.parseLong(value)); } @JsonCreator public static SafeLong of(long value) { - return ImmutableSafeLong.of(value); + return new SafeLong(value); + } + + @Override + public String toString() { + return Long.toString(longValue); + } + + @Override + public int compareTo(SafeLong other) { + return Long.compare(longValue, other.longValue); } @Override - public final String toString() { - return Long.toString(longValue()); + public boolean equals(Object other) { + if (this == other) { + return true; + } + if (other == null || getClass() != other.getClass()) { + return false; + } + SafeLong safeLong = (SafeLong) other; + return longValue == safeLong.longValue; } @Override - public final int compareTo(SafeLong other) { - return Long.compare(longValue(), other.longValue()); + public int hashCode() { + return 177573 + Long.hashCode(longValue); } }