Skip to content

Commit

Permalink
FasterXML#2885 hasFractionalPart method added
Browse files Browse the repository at this point in the history
  • Loading branch information
siavashsoleymani committed Oct 17, 2020
1 parent ebf0732 commit 4418af9
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/main/java/com/fasterxml/jackson/databind/JsonNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,14 @@ public final boolean isNumber() {
*/
public boolean isInt() { return false; }

/**
* Method that can be used to check whether contained value
* has a fractional part or not.
*
* @return True if the value has fractional part
*/
public boolean hasFractionalPart() { return false; }

/**
* Method that can be used to check whether contained value
* is a number represented as Java <code>long</code>.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ public class DecimalNode
@Override
public boolean isBigDecimal() { return true; }

@Override
public boolean hasFractionalPart() {
BigDecimal fractionalPart = _value.remainder( BigDecimal.ONE );
return !fractionalPart.equals(new BigDecimal("0").setScale(fractionalPart.scale()));
}

@Override public boolean canConvertToInt() {
return (_value.compareTo(MIN_INTEGER) >= 0) && (_value.compareTo(MAX_INTEGER) <= 0);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ public class DoubleNode
@Override
public boolean isDouble() { return true; }

@Override
public boolean hasFractionalPart() {
long iPart = (long) _value;
double fPart = _value - iPart;
return fPart != 0.0;
}

@Override public boolean canConvertToInt() {
return (_value >= Integer.MIN_VALUE && _value <= Integer.MAX_VALUE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ public class FloatNode extends NumericNode
@Override
public boolean isFloat() { return true; }

@Override
public boolean hasFractionalPart() {
long iPart = (long) _value;
double fPart = _value - iPart;
return fPart != 0.0;
}

@Override public boolean canConvertToInt() {
return (_value >= Integer.MIN_VALUE && _value <= Integer.MAX_VALUE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public void testIntViaMapper() throws Exception
assertType(result, IntNode.class);
assertFalse(result.isLong());
assertFalse(result.isFloatingPointNumber());
assertFalse(result.hasFractionalPart());
assertFalse(result.isDouble());
assertFalse(result.isNull());
assertFalse(result.isTextual());
Expand Down Expand Up @@ -131,6 +132,7 @@ public void testLongViaMapper() throws Exception
assertType(result, LongNode.class);
assertFalse(result.isInt());
assertFalse(result.isFloatingPointNumber());
assertFalse(result.hasFractionalPart());
assertFalse(result.isDouble());
assertFalse(result.isNull());
assertFalse(result.isTextual());
Expand Down Expand Up @@ -185,6 +187,7 @@ public void testDoubleViaMapper() throws Exception
assertFalse(result.isNull());
assertType(result, DoubleNode.class);
assertTrue(result.isFloatingPointNumber());
assertTrue(result.hasFractionalPart());
assertTrue(result.isDouble());
assertFalse(result.isInt());
assertFalse(result.isLong());
Expand All @@ -211,7 +214,9 @@ public void testFloat()
assertEquals(JsonToken.VALUE_NUMBER_FLOAT, n.asToken());
assertEquals(JsonParser.NumberType.FLOAT, n.numberType());
assertEquals(0, n.intValue());

assertTrue(n.isFloatingPointNumber());
assertTrue(n.hasFractionalPart());

// NOTE: conversion to double NOT as simple as with exact numbers like 0.25:
assertEquals(0.45f, n.floatValue());
assertEquals("0.45", n.asText());
Expand Down Expand Up @@ -281,6 +286,7 @@ public void testDecimalNode() throws Exception
assertType(result, DecimalNode.class);
assertFalse(result.isInt());
assertTrue(result.isFloatingPointNumber());
assertTrue(result.hasFractionalPart());
assertTrue(result.isBigDecimal());
assertFalse(result.isDouble());
assertFalse(result.isNull());
Expand Down

0 comments on commit 4418af9

Please sign in to comment.