Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions java/dfp/src/main/java/com/epam/deltix/dfp/Decimal64.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public class Decimal64 extends Number implements Comparable<Decimal64> {

final long value;

Decimal64(final long value) {
Decimal64(@Decimal final long value) {
this.value = value;
}

Expand All @@ -129,7 +129,7 @@ public class Decimal64 extends Number implements Comparable<Decimal64> {
* @param value 64-bit DFP value
* @return new {@code Decimal64} instance
*/
public static Decimal64 fromUnderlying(final long value) {
public static Decimal64 fromUnderlying(@Decimal final long value) {
return Decimal64Utils.NULL == value ? null : new Decimal64(value);
}

Expand All @@ -139,10 +139,21 @@ public static Decimal64 fromUnderlying(final long value) {
* @param obj {@code Decimal64} instance, {@code null} can be passed too
* @return underlying binary representation as {@code long}
*/
@Decimal
public static long toUnderlying(final Decimal64 obj) {
return null == obj ? Decimal64Utils.NULL : obj.value;
}

/**
* Get binary representation as {@code long} (unboxing).
*
* @return underlying binary representation as {@code long}
*/
@Decimal
public long toUnderlying() {
return value;
}

/**
* Create {@code Decimal64} instance from fixed point decimal value: (12345, 2) -&gt; 123.45
*
Expand Down Expand Up @@ -358,6 +369,7 @@ public boolean isNormal() {
* @see #equals(Decimal64, Decimal64)
* @see #equals(Object)
*/
@SuppressWarnings("NumberEquality")
public boolean equals(final Decimal64 other) {
return this == other || other != null && Decimal64Utils.equals(this.value, other.value);
}
Expand Down
19 changes: 14 additions & 5 deletions java/dfp/src/main/java/com/epam/deltix/dfp/Decimal64Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -309,11 +309,6 @@ public static long fromUnderlying(@Decimal final long value) {
return value;
}

@Decimal
public static long toUnderlying(@Decimal final long value) {
return value;
}

/**
* Create {@code @Decimal long} value from {@code BigDecimal} binary floating point value.
* <p>Note that not all binary FP values can be exactly represented as decimal FP values.
Expand Down Expand Up @@ -3128,6 +3123,20 @@ public static int compareToChecked(@Decimal final long a, final Object b) {
return compareTo(a, ((Decimal64) b).value);
}

/**
* Checks underlying binary value for null-value and returns it; do not use directly.
*
* @param value 64-bit DFP value
* @return same 64-bit DFP value (unless it is DFP null-value)
* @throws NullPointerException if the value is null
*/
@Deprecated
@Decimal
public static long toUnderlyingChecked(@Decimal final long value) {
checkNull(value);
return value;
}

/// endregion

/// region Array boxing/unboxing (array conversions from long[] / to long[])
Expand Down
5 changes: 5 additions & 0 deletions java/dfp/src/test/java/com/epam/deltix/dfp/Decimal64Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ public void unbox() {
Assert.assertEquals(Decimal64Utils.NULL, Decimal64.toUnderlying(null));
}

@Test
public void unboxInstance() {
Assert.assertEquals(5L, Decimal64.fromUnderlying(5L).toUnderlying());
}

@Test
public void equality() {

Expand Down
25 changes: 25 additions & 0 deletions java/dfp/src/test/java/com/epam/deltix/dfp/JavaImplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1112,4 +1112,29 @@ public void issue110BoxedEquals() {
assertTrue(Decimal64Utils.compareTo(val2, cache.get("val")) == 0);
assertTrue(Decimal64Utils.equals(val2, cache.get("val")));
}

// https://github.com/epam/DFP/issues/110
@Test
public void issue110BoxedEquals_2() {
Map<String, Decimal64> cache = new HashMap<>();
Decimal64 val = Decimal64.fromDouble(0.5);
cache.put("val", val);
@Decimal long val2 = Decimal64Utils.fromDouble(0.5);

// Compare unboxed value with boxed value (the best way for this case)
assertTrue(Decimal64Utils.equals(val2, cache.get("val")));

// Compare boxed values
Decimal64 value2boxed = Decimal64.fromUnderlying(val2);
assertTrue(value2boxed.equals(cache.get("val")));
assertTrue(value2boxed.compareTo(cache.get("val")) == 0);

// Compare binary representations
assertTrue(val2 == Decimal64.toUnderlying(cache.get("val")));
assertTrue(Decimal64Utils.compareTo(val2, Decimal64.toUnderlying(cache.get("val"))) == 0);

// Compare binary representations with use of new toUnderlying()
assertTrue(val2 == cache.get("val").toUnderlying()); // May produce NPE, if no value in the cache!
assertTrue(Decimal64Utils.compareTo(val2, cache.get("val").toUnderlying()) == 0); // May produce NPE, if no value in the cache!
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,7 @@ public static void main(String[] args) {
sDecimal.toScientificString() + "(=0x" + Long.toHexString(Decimal64.toUnderlying(sDecimal)) + "L) ) = " +
qDecimal.toScientificString() + "(=0x" + Long.toHexString(Decimal64.toUnderlying(qDecimal)) + "L) ) is a double " +
q + "(=0x" + Long.toHexString(Double.doubleToRawLongBits(q)) + "L)");

System.out.println(Decimal64.fromUnderlying(aDecimal.toUnderlying()));
}
}
Loading