Skip to content

Commit 0a27a77

Browse files
authored
fix: handling of double and integer (#316)
Signed-off-by: apulbere <[email protected]>
1 parent 09824e7 commit 0a27a77

File tree

3 files changed

+16
-16
lines changed

3 files changed

+16
-16
lines changed

Diff for: src/main/java/dev/openfeature/sdk/Structure.java

+6-5
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,13 @@ default Object convertValue(Value value) {
5656
return value.asBoolean();
5757
}
5858

59-
if (value.isNumber()) {
60-
Double valueAsDouble = value.asDouble();
61-
if (valueAsDouble == Math.floor(valueAsDouble) && !Double.isInfinite(valueAsDouble)) {
62-
return value.asInteger();
59+
if (value.isNumber() && !value.isNull()) {
60+
Number numberValue = (Number) value.asObject();
61+
if (numberValue instanceof Double) {
62+
return numberValue.doubleValue();
63+
} else if (numberValue instanceof Integer) {
64+
return numberValue.intValue();
6365
}
64-
return valueAsDouble;
6566
}
6667

6768
if (value.isString()) {

Diff for: src/main/java/dev/openfeature/sdk/Value.java

+7-8
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ public Value() {
3535
* (boolean, string, int, double, list, structure, instant)
3636
*/
3737
public Value(Object value) throws InstantiationException {
38-
// integer is a special case, convert those.
39-
this.innerObject = value instanceof Integer ? ((Integer)value).doubleValue() : value;
38+
this.innerObject = value;
4039
if (!this.isNull()
4140
&& !this.isBoolean()
4241
&& !this.isString()
@@ -61,7 +60,7 @@ public Value(String value) {
6160
}
6261

6362
public Value(Integer value) {
64-
this.innerObject = value.doubleValue();
63+
this.innerObject = value;
6564
}
6665

6766
public Value(Double value) {
@@ -113,7 +112,7 @@ public boolean isString() {
113112
* @return boolean
114113
*/
115114
public boolean isNumber() {
116-
return this.innerObject instanceof Double;
115+
return this.innerObject instanceof Number;
117116
}
118117

119118
/**
@@ -187,8 +186,8 @@ public String asString() {
187186
* @return Integer
188187
*/
189188
public Integer asInteger() {
190-
if (this.isNumber()) {
191-
return (int)Math.round((Double)this.innerObject);
189+
if (this.isNumber() && !this.isNull()) {
190+
return ((Number)this.innerObject).intValue();
192191
}
193192
return null;
194193
}
@@ -199,8 +198,8 @@ public Integer asInteger() {
199198
* @return Double
200199
*/
201200
public Double asDouble() {
202-
if (this.isNumber()) {
203-
return (Double)this.innerObject;
201+
if (this.isNumber() && !isNull()) {
202+
return ((Number)this.innerObject).doubleValue();
204203
}
205204
return null;
206205
}

Diff for: src/test/java/dev/openfeature/sdk/ValueTest.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@ class Something {}
6666
}
6767

6868
@Test public void numericArgShouldReturnDoubleOrInt() {
69-
double innerDoubleValue = .75;
69+
double innerDoubleValue = 1.75;
7070
Value doubleValue = new Value(innerDoubleValue);
7171
assertTrue(doubleValue.isNumber());
72-
assertEquals(1, doubleValue.asInteger()); // should be rounded
73-
assertEquals(.75, doubleValue.asDouble());
72+
assertEquals(1, doubleValue.asInteger()); // the double value represented by this object converted to type int
73+
assertEquals(1.75, doubleValue.asDouble());
7474

7575
int innerIntValue = 100;
7676
Value intValue = new Value(innerIntValue);

0 commit comments

Comments
 (0)