import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Strings;
import java.math.BigDecimal;
+import java.math.RoundingMode;
import java.util.Optional;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
// Fractions are next
break;
}
- if (intLen == MAX_FRACTION_DIGITS) {
+ if (intLen == MAX_SCALE) {
return CanonicalValueViolation.variantOf(
- "Integer part is longer than " + MAX_FRACTION_DIGITS + " digits");
+ "Integer part is longer than " + MAX_SCALE + " digits");
}
intPart = 10 * intPart + toInt(ch, idx);
limit--;
}
- final int fracLimit = MAX_FRACTION_DIGITS - intLen + 1;
+ final int fracLimit = MAX_SCALE - intLen + 1;
byte fracLen = 0;
long fracPart = 0;
for (; idx <= limit; idx++, fracLen++) {
private static final CanonicalValueSupport<Decimal64> SUPPORT = new Support();
private static final long serialVersionUID = 1L;
- private static final int MAX_FRACTION_DIGITS = 18;
+ private static final int MAX_SCALE = 18;
- private static final long[] SCALE = {
+ private static final long[] FACTOR = {
10,
100,
1000,
1000000000000000000L
};
+ private static final Decimal64Conversion[] CONVERSION = Decimal64Conversion.values();
+ private static final Decimal64[] MIN_VALUE;
+ private static final Decimal64[] MAX_VALUE;
+
static {
- verify(SCALE.length == MAX_FRACTION_DIGITS);
+ verify(CONVERSION.length == MAX_SCALE);
+ verify(FACTOR.length == MAX_SCALE);
+
+ MIN_VALUE = new Decimal64[MAX_SCALE];
+ MAX_VALUE = new Decimal64[MAX_SCALE];
+ for (byte i = 0; i < MAX_SCALE; ++i) {
+ MIN_VALUE[i] = new Decimal64(i, -9223372036854775808L);
+ MAX_VALUE[i] = new Decimal64(i, 9223372036854775807L);
+ }
}
- private final byte scaleOffset;
+ private final byte offset;
private final long value;
@VisibleForTesting
- Decimal64(final int fractionDigits, final long intPart, final long fracPart, final boolean negative) {
- checkArgument(fractionDigits >= 1 && fractionDigits <= MAX_FRACTION_DIGITS);
- this.scaleOffset = (byte) (fractionDigits - 1);
+ Decimal64(final int scale, final long intPart, final long fracPart, final boolean negative) {
+ offset = offsetOf(scale);
+
+ final long bits = intPart * FACTOR[offset] + fracPart;
+ value = negative ? -bits : bits;
+ }
+
+ private Decimal64(final byte offset, final long intPart, final boolean negative) {
+ this.offset = offset;
+ final long bits = intPart * FACTOR[offset];
+ value = negative ? -bits : bits;
+ }
- final long bits = intPart * SCALE[this.scaleOffset] + fracPart;
- this.value = negative ? -bits : bits;
+ private Decimal64(final byte offset, final long value) {
+ this.offset = offset;
+ this.value = value;
}
protected Decimal64(final Decimal64 other) {
- this.scaleOffset = other.scaleOffset;
- this.value = other.value;
+ this(other.offset, other.value);
}
- public static Decimal64 valueOf(final byte byteVal) {
- return byteVal < 0 ? new Decimal64(1, -byteVal, 0, true) : new Decimal64(1, byteVal, 0, false);
+ /**
+ * Return a {@link Decimal64} with specified scale and unscaled value.
+ *
+ * @param scale scale to use
+ * @param unscaledValue unscaled value to use
+ * @return A Decimal64 instance
+ * @throws IllegalArgumentException if {@code scale} is not in range {@code [1..18]}
+ */
+ public static Decimal64 of(final int scale, final long unscaledValue) {
+ return new Decimal64(offsetOf(scale), unscaledValue);
}
- public static Decimal64 valueOf(final short shortVal) {
- return shortVal < 0 ? new Decimal64(1, -shortVal, 0, true) : new Decimal64(1, shortVal, 0, false);
+ /**
+ * Return the minimum value supported in specified scale.
+ *
+ * @param scale scale to use
+ * @return Minimum value in that scale
+ * @throws IllegalArgumentException if {@code scale} is not in range {@code [1..18]}
+ */
+ public static Decimal64 minValueIn(final int scale) {
+ return MIN_VALUE[offsetOf(scale)];
+ }
+
+ /**
+ * Return the maximum value supported in specified scale.
+ *
+ * @param scale scale to use
+ * @return Maximum value in that scale
+ * @throws IllegalArgumentException if {@code scale} is not in range {@code [1..18]}
+ */
+ public static Decimal64 maxValueIn(final int scale) {
+ return MAX_VALUE[offsetOf(scale)];
}
- public static Decimal64 valueOf(final int intVal) {
- return intVal < 0 ? new Decimal64(1, - (long)intVal, 0, true) : new Decimal64(1, intVal, 0, false);
+ // >>> FIXME: these need truncating counterparts
+ public static Decimal64 valueOf(final int scale, final byte byteVal) {
+ final byte offset = offsetOf(scale);
+ final var conv = CONVERSION[offset];
+ if (byteVal < conv.minByte || byteVal > conv.maxByte) {
+ throw new IllegalArgumentException("Value " + byteVal + " is not in range ["
+ + conv.minByte + ".." + conv.maxByte + "] to fit scale " + scale);
+ }
+ return byteVal < 0 ? new Decimal64(offset, -byteVal, true) : new Decimal64(offset, byteVal, false);
}
- public static Decimal64 valueOf(final long longVal) {
+ public static Decimal64 valueOf(final int scale, final short shortVal) {
+ final byte offset = offsetOf(scale);
+ final var conv = CONVERSION[offset];
+ if (shortVal < conv.minShort || shortVal > conv.maxShort) {
+ throw new IllegalArgumentException("Value " + shortVal + " is not in range ["
+ + conv.minShort + ".." + conv.maxShort + "] to fit scale " + scale);
+ }
+ return shortVal < 0 ? new Decimal64(offset, -shortVal, true) : new Decimal64(offset, shortVal, false);
+ }
+
+ public static Decimal64 valueOf(final int scale, final int intVal) {
+ final byte offset = offsetOf(scale);
+ final var conv = CONVERSION[offset];
+ if (intVal < conv.minInt || intVal > conv.maxInt) {
+ throw new IllegalArgumentException("Value " + intVal + " is not in range ["
+ + conv.minInt + ".." + conv.maxInt + "] to fit scale " + scale);
+ }
+ return intVal < 0 ? new Decimal64(offset, - (long)intVal, true) : new Decimal64(offset, intVal, false);
+ }
+
+ public static Decimal64 valueOf(final int scale, final long longVal) {
+ final byte offset = offsetOf(scale);
+ final var conv = CONVERSION[offset];
+ if (longVal < conv.minLong || longVal > conv.maxLong) {
+ throw new IllegalArgumentException("Value " + longVal + " is not in range ["
+ + conv.minLong + ".." + conv.maxLong + "] to fit scale " + scale);
+ }
+ return longVal < 0 ? new Decimal64(offset, -longVal, true) : new Decimal64(offset, longVal, false);
+ }
+ // <<< FIXME
+
+ // FIXME: this should take a RoundingMode and perform rounding
+ // FIXME: this should have a truncating counterpart
+ public static Decimal64 valueOf(final float floatVal, final RoundingMode rounding) {
// XXX: we should be able to do something smarter here
- return valueOf(Long.toString(longVal));
+ return valueOf(Float.toString(floatVal));
}
- public static Decimal64 valueOf(final double doubleVal) {
+ // FIXME: this should take a RoundingMode and perform rounding
+ // FIXME: this should have a truncating counterpart
+ public static Decimal64 valueOf(final double doubleVal, final RoundingMode rounding) {
// XXX: we should be able to do something smarter here
return valueOf(Double.toString(doubleVal));
}
public static Decimal64 valueOf(final BigDecimal decimalVal) {
- // XXX: we should be able to do something smarter here
+ // FIXME: we should be able to do something smarter here using BigDecimal.unscaledValue() and BigDecimal.scale()
return valueOf(decimalVal.toPlainString());
}
throw message.isPresent() ? new NumberFormatException(message.get()) : new NumberFormatException();
}
+ /**
+ * Return the scale of this decimal. This is the number of fraction digits, in range {@code [1..18]}.
+ *
+ * @return This decimal's scale
+ */
+ public final int scale() {
+ return offset + 1;
+ }
+
+ /**
+ * Return the unscaled value of this decimal.
+ *
+ * @return This decimal's unscaled value
+ */
+ public final long unscaledValue() {
+ return value;
+ }
+
public final BigDecimal decimalValue() {
- return BigDecimal.valueOf(value, scaleOffset + 1);
+ return BigDecimal.valueOf(value, scale());
}
@Override
@Override
public final double doubleValue() {
- return 1.0 * value / SCALE[scaleOffset];
+ return 1.0 * value / FACTOR[offset];
}
/**
if (this == o) {
return 0;
}
- if (scaleOffset == o.scaleOffset) {
+ if (offset == o.offset) {
return Long.compare(value, o.value);
}
final long fracPart = fracPart();
if (fracPart != 0) {
// We may need to zero-pad the fraction part
- sb.append(Strings.padStart(Long.toString(fracPart), scaleOffset + 1, '0'));
+ sb.append(Strings.padStart(Long.toString(fracPart), scale(), '0'));
} else {
sb.append('0');
}
@Override
public final boolean equals(final @Nullable Object obj) {
- return this == obj || obj instanceof Decimal64 && equalsImpl((Decimal64) obj);
+ return this == obj || obj instanceof Decimal64 other && equalsImpl(other);
}
/**
}
private boolean equalsImpl(final Decimal64 other) {
- return scaleOffset == other.scaleOffset ? value == other.value
+ return offset == other.offset ? value == other.value
// We need to normalize both
: intPart() == other.intPart() && fracPart() == other.fracPart();
}
private long intPart() {
- return value / SCALE[scaleOffset];
+ return value / FACTOR[offset];
}
private long fracPart() {
- return Math.abs(value % SCALE[scaleOffset]);
+ return Math.abs(value % FACTOR[offset]);
+ }
+
+ private static byte offsetOf(final int scale) {
+ checkArgument(scale >= 1 && scale <= MAX_SCALE);
+ return (byte) (scale - 1);
}
}