public final class NumberNode extends Node
Number value.
Number nodes contain a value. You can inspect its type by calling
isNaturalNumber() or isFloatingPointNumber(). Natural
numbers are positive, or negative numbers without a decimal part.
Node.NonNumericFloat| Constructor and Description |
|---|
NumberNode(java.lang.Number value,
SourceLocation sourceLocation) |
| Modifier and Type | Method and Description |
|---|---|
<R> R |
accept(NodeVisitor<R> visitor)
Accepts a visitor with the node.
|
java.util.Optional<java.math.BigDecimal> |
asBigDecimal()
Gets the number value as a BigDecimal if possible.
|
java.util.Optional<NumberNode> |
asNumberNode()
Gets the node as an NumberNode if it is an number.
|
boolean |
equals(java.lang.Object other) |
NumberNode |
expectNumberNode(java.lang.String errorMessage)
Casts the current node to a
NumberNode, throwing
ExpectationNotMetException when the node is the wrong type. |
NumberNode |
expectNumberNode(java.util.function.Supplier<java.lang.String> errorMessage)
Casts the current node to a
NumberNode, throwing
ExpectationNotMetException when the node is the wrong type. |
NodeType |
getType()
Gets the type of the node.
|
java.lang.Number |
getValue()
Gets the number value.
|
int |
hashCode() |
boolean |
isFloatingPointNumber()
Returns true if the node contains a floating point number.
|
boolean |
isInfinite()
Returns true if the number is infinite.
|
boolean |
isNaN()
Returns true if the number is a floating point NaN.
|
boolean |
isNaturalNumber()
Deprecated.
|
boolean |
isNegative()
Check the value is negative, including negative infinity.
|
boolean |
isZero()
Returns true if the value of the number contained in the number node is zero,
accounting for float, double, bigInteger, bigDecimal, and other numeric types
(e.g., 0, 0.0, etc).
|
java.lang.String |
toString() |
arrayNode, arrayNode, asArrayNode, asBooleanNode, asNullNode, asObjectNode, assertEquals, asStringNode, diff, expectArrayNode, expectArrayNode, expectArrayNode, expectBooleanNode, expectBooleanNode, expectBooleanNode, expectNullNode, expectNullNode, expectNullNode, expectNumberNode, expectObjectNode, expectObjectNode, expectObjectNode, expectStringNode, expectStringNode, expectStringNode, from, from, from, from, fromNodes, fromNodes, fromStrings, fromStrings, getSourceLocation, isArrayNode, isBooleanNode, isNullNode, isNumberNode, isObjectNode, isStringNode, loadArrayOfString, nullNode, objectNode, objectNode, objectNodeBuilder, parse, parse, parse, parse, parseJsonWithComments, parseJsonWithComments, prettyPrintJson, prettyPrintJson, printJson, toNode, withDeepSortedKeys, withDeepSortedKeysclone, finalize, getClass, notify, notifyAll, wait, wait, waitcomparepublic NumberNode(java.lang.Number value,
SourceLocation sourceLocation)
public java.lang.Number getValue()
public java.util.Optional<java.math.BigDecimal> asBigDecimal()
NaN and infinite numbers will return an empty Optional.
@Deprecated public boolean isNaturalNumber()
public boolean isNegative()
Any number >= 0, +Infinity, and NaN return false.
public boolean isFloatingPointNumber()
public boolean isNaN()
public boolean isInfinite()
public <R> R accept(NodeVisitor<R> visitor)
Nodepublic NumberNode expectNumberNode(java.lang.String errorMessage)
NodeNumberNode, throwing
ExpectationNotMetException when the node is the wrong type.expectNumberNode in class NodeerrorMessage - Error message to use if the node is of the wrong type.public NumberNode expectNumberNode(java.util.function.Supplier<java.lang.String> errorMessage)
NodeNumberNode, throwing
ExpectationNotMetException when the node is the wrong type.expectNumberNode in class NodeerrorMessage - Error message supplier.public java.util.Optional<NumberNode> asNumberNode()
NodeasNumberNode in class Nodepublic boolean isZero()
Note that -0 and +0 are considered 0. However, NaN is not considered zero. When unknown number types are encountered, this method will return true if the toString of the given number returns "0", or "0.0". Other kinds of unknown number types will be treated like a double.
Double and float comparisons to zero are exact and use no rounding. The majority of values seen by this method come from models that use "0" or "0.0". However, we can improve this in the future with some kind of epsilon if the need arises.
public boolean equals(java.lang.Object other)
equals in class java.lang.Objectpublic int hashCode()
hashCode in class java.lang.Objectpublic java.lang.String toString()
toString in class java.lang.Object