From f4841b120ad57b256c61a1a80ee3adbaf40bca0b Mon Sep 17 00:00:00 2001 From: apulbere Date: Tue, 7 Mar 2023 14:28:54 +0200 Subject: [PATCH] fix: handling of double and integer Signed-off-by: apulbere --- src/main/java/dev/openfeature/sdk/Structure.java | 11 ++++++----- src/main/java/dev/openfeature/sdk/Value.java | 15 +++++++-------- src/test/java/dev/openfeature/sdk/ValueTest.java | 6 +++--- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/main/java/dev/openfeature/sdk/Structure.java b/src/main/java/dev/openfeature/sdk/Structure.java index 9d66c2bed..f8b552125 100644 --- a/src/main/java/dev/openfeature/sdk/Structure.java +++ b/src/main/java/dev/openfeature/sdk/Structure.java @@ -56,12 +56,13 @@ default Object convertValue(Value value) { return value.asBoolean(); } - if (value.isNumber()) { - Double valueAsDouble = value.asDouble(); - if (valueAsDouble == Math.floor(valueAsDouble) && !Double.isInfinite(valueAsDouble)) { - return value.asInteger(); + if (value.isNumber() && !value.isNull()) { + Number numberValue = (Number) value.asObject(); + if (numberValue instanceof Double) { + return numberValue.doubleValue(); + } else if (numberValue instanceof Integer) { + return numberValue.intValue(); } - return valueAsDouble; } if (value.isString()) { diff --git a/src/main/java/dev/openfeature/sdk/Value.java b/src/main/java/dev/openfeature/sdk/Value.java index b1ad1c156..f4ad909de 100644 --- a/src/main/java/dev/openfeature/sdk/Value.java +++ b/src/main/java/dev/openfeature/sdk/Value.java @@ -35,8 +35,7 @@ public Value() { * (boolean, string, int, double, list, structure, instant) */ public Value(Object value) throws InstantiationException { - // integer is a special case, convert those. - this.innerObject = value instanceof Integer ? ((Integer)value).doubleValue() : value; + this.innerObject = value; if (!this.isNull() && !this.isBoolean() && !this.isString() @@ -61,7 +60,7 @@ public Value(String value) { } public Value(Integer value) { - this.innerObject = value.doubleValue(); + this.innerObject = value; } public Value(Double value) { @@ -113,7 +112,7 @@ public boolean isString() { * @return boolean */ public boolean isNumber() { - return this.innerObject instanceof Double; + return this.innerObject instanceof Number; } /** @@ -187,8 +186,8 @@ public String asString() { * @return Integer */ public Integer asInteger() { - if (this.isNumber()) { - return (int)Math.round((Double)this.innerObject); + if (this.isNumber() && !this.isNull()) { + return ((Number)this.innerObject).intValue(); } return null; } @@ -199,8 +198,8 @@ public Integer asInteger() { * @return Double */ public Double asDouble() { - if (this.isNumber()) { - return (Double)this.innerObject; + if (this.isNumber() && !isNull()) { + return ((Number)this.innerObject).doubleValue(); } return null; } diff --git a/src/test/java/dev/openfeature/sdk/ValueTest.java b/src/test/java/dev/openfeature/sdk/ValueTest.java index cf25e7b37..aa6ac62f9 100644 --- a/src/test/java/dev/openfeature/sdk/ValueTest.java +++ b/src/test/java/dev/openfeature/sdk/ValueTest.java @@ -66,11 +66,11 @@ class Something {} } @Test public void numericArgShouldReturnDoubleOrInt() { - double innerDoubleValue = .75; + double innerDoubleValue = 1.75; Value doubleValue = new Value(innerDoubleValue); assertTrue(doubleValue.isNumber()); - assertEquals(1, doubleValue.asInteger()); // should be rounded - assertEquals(.75, doubleValue.asDouble()); + assertEquals(1, doubleValue.asInteger()); // the double value represented by this object converted to type int + assertEquals(1.75, doubleValue.asDouble()); int innerIntValue = 100; Value intValue = new Value(innerIntValue);