Skip to content

Commit

Permalink
avoid BigDecimal when creating SerializedValue from double, int, or long
Browse files Browse the repository at this point in the history
  • Loading branch information
Marsette Vona committed Mar 2, 2024
1 parent 43959cf commit 7526784
Showing 1 changed file with 75 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -67,6 +68,10 @@ public <T> T match(final Visitor<T> visitor) {
}
}

interface DirectNumericValue {
NumericValue asNumericValue();
}

record NumericValue(BigDecimal value) implements SerializedValue {
@Override
public <T> T match(final Visitor<T> visitor) {
Expand All @@ -76,8 +81,12 @@ public <T> T match(final Visitor<T> 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
Expand All @@ -86,6 +95,57 @@ public int hashCode() {
}
}

record IntValue(int value) implements SerializedValue, DirectNumericValue {
@Override
public <T> T match(final Visitor<T> 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> T match(final Visitor<T> 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> T match(final Visitor<T> 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> T match(final Visitor<T> visitor) {
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand Down

0 comments on commit 7526784

Please sign in to comment.