From 75267843a219839bac4d594935ef560610974dca Mon Sep 17 00:00:00 2001 From: Marsette Vona Date: Thu, 8 Feb 2024 19:46:29 -0500 Subject: [PATCH] avoid BigDecimal when creating SerializedValue from double, int, or long --- .../protocol/types/SerializedValue.java | 80 +++++++++++++++++-- 1 file changed, 75 insertions(+), 5 deletions(-) diff --git a/merlin-sdk/src/main/java/gov/nasa/jpl/aerie/merlin/protocol/types/SerializedValue.java b/merlin-sdk/src/main/java/gov/nasa/jpl/aerie/merlin/protocol/types/SerializedValue.java index 3187d8051f..69731c64b6 100644 --- a/merlin-sdk/src/main/java/gov/nasa/jpl/aerie/merlin/protocol/types/SerializedValue.java +++ b/merlin-sdk/src/main/java/gov/nasa/jpl/aerie/merlin/protocol/types/SerializedValue.java @@ -1,6 +1,7 @@ package gov.nasa.jpl.aerie.merlin.protocol.types; import java.math.BigDecimal; +import java.math.MathContext; import java.util.List; import java.util.Map; import java.util.Objects; @@ -67,6 +68,10 @@ public T match(final Visitor visitor) { } } + interface DirectNumericValue { + NumericValue asNumericValue(); + } + record NumericValue(BigDecimal value) implements SerializedValue { @Override public T match(final Visitor visitor) { @@ -76,8 +81,12 @@ public T match(final Visitor visitor) { // `BigDecimal#equals` is too strict -- values differing only in representation need to be considered the same. @Override public boolean equals(final Object obj) { - if (!(obj instanceof NumericValue other)) return false; - return (this.value.compareTo(other.value) == 0); + if (obj instanceof NumericValue other) { + return (this.value.compareTo(other.value) == 0); + } else if (obj instanceof DirectNumericValue other) { + return (this.value.compareTo(other.asNumericValue().value) == 0); + } + return false; } @Override @@ -86,6 +95,57 @@ public int hashCode() { } } + record IntValue(int value) implements SerializedValue, DirectNumericValue { + @Override + public T match(final Visitor visitor) { + return visitor.onNumeric(new BigDecimal(value)); + } + + @Override + public NumericValue asNumericValue() { + return new NumericValue(new BigDecimal(value)); + } + + @Override + public boolean equals(final Object obj) { + return asNumericValue().equals(obj); + } + } + + record LongValue(long value) implements SerializedValue, DirectNumericValue { + @Override + public T match(final Visitor visitor) { + return visitor.onNumeric(new BigDecimal(value)); + } + + @Override + public NumericValue asNumericValue() { + return new NumericValue(new BigDecimal(value)); + } + + @Override + public boolean equals(final Object obj) { + return asNumericValue().equals(obj); + } + } + + record DoubleValue(double value) implements SerializedValue, DirectNumericValue { + @Override + public T match(final Visitor visitor) { + return visitor.onNumeric(new BigDecimal(value, MathContext.DECIMAL64)); + } + + @Override + public NumericValue asNumericValue() { + return new NumericValue(new BigDecimal(value, MathContext.DECIMAL64)); + } + + @Override + public boolean equals(final Object obj) { + return asNumericValue().equals(obj); + } + } + record BooleanValue(boolean value) implements SerializedValue { @Override public T match(final Visitor visitor) { @@ -136,11 +196,11 @@ static SerializedValue of(final BigDecimal value) { /** * Creates a {@link SerializedValue} containing a real number. * - * @param value Any double} value. + * @param value Any double value. * @return A new {@link SerializedValue} containing a real number. */ static SerializedValue of(final double value) { - return new NumericValue(BigDecimal.valueOf(value)); + return new DoubleValue(value); } /** @@ -150,7 +210,17 @@ static SerializedValue of(final double value) { * @return A new {@link SerializedValue} containing an integral number. */ static SerializedValue of(final long value) { - return new NumericValue(BigDecimal.valueOf(value)); + return new LongValue(value); + } + + /** + * Creates a {@link SerializedValue} containing an integral number. + * + * @param value Any integer value. + * @return A new {@link SerializedValue} containing an integral number. + */ + static SerializedValue of(final int value) { + return new IntValue(value); } /**