Package net.sf.saxon.value
Class NumericValue
java.lang.Object
net.sf.saxon.value.Value<AtomicValue>
net.sf.saxon.value.AtomicValue
net.sf.saxon.value.NumericValue
- All Implemented Interfaces:
Comparable
,PullEvent
,GroundedValue<AtomicValue>
,Item<AtomicValue>
,ValueRepresentation<AtomicValue>
,ConversionResult
- Direct Known Subclasses:
DecimalValue
,DoubleValue
,FloatValue
,IntegerValue
NumericValue is an abstract superclass for IntegerValue, DecimalValue,
FloatValue, and DoubleValue
-
Field Summary
Fields inherited from class net.sf.saxon.value.AtomicValue
typeLabel
Fields inherited from class net.sf.saxon.value.Value
INDETERMINATE_ORDERING
Fields inherited from interface net.sf.saxon.om.ValueRepresentation
EMPTY_VALUE_ARRAY
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionabstract NumericValue
abs()
Get the absolute value as defined by the XPath abs() functionabstract NumericValue
ceiling()
Implement the XPath ceiling() functionabstract int
compareTo
(long other) Compare the value to a longint
Compare the value to another numeric valuefinal boolean
The equals() function compares numeric equality among integers, decimals, floats, doubles, and their subtypesabstract NumericValue
floor()
Implement the XPath floor() functionabstract BigDecimal
Get the numeric value converted to a decimalabstract double
Get the numeric value as a doubleabstract float
Get the numeric value converted to a floatfinal Object
getXPathComparable
(boolean ordered, StringCollator collator, XPathContext context) Get a Comparable value that implements the XPath ordering comparison semantics for this value.abstract int
hashCode()
hashCode() must be the same for two values that are equal.static boolean
isInteger
(AtomicValue value) Test whether a value is an integer (an instance of a subtype of xs:integer)abstract boolean
Determine whether the value is a whole number, that is, whether it compares equal to some integerabstract long
Return the numeric value as a Java long.abstract NumericValue
negate()
Change the sign of the numberstatic NumericValue
parseNumber
(String in) Get a numeric value by parsing a string; the type of numeric value depends on the lexical form of the string, following the rules for XPath numeric literals.abstract NumericValue
round
(int scale) Implement the XPath round() functionabstract NumericValue
roundHalfToEven
(int scale) Implement the XPath 2.0 round-half-to-even() functionabstract int
signum()
Determine whether the value is negative, zero, or positivetoString()
Produce a string representation of the valueMethods inherited from class net.sf.saxon.value.AtomicValue
asAtomic, checkPermittedContents, copyAsSubType, effectiveBooleanValue, getCardinality, getComponent, getItemType, getLength, getPrimitiveStringValue, getPrimitiveType, getSchemaComparable, getStringValue, getStringValueCS, getTypedValue, getTypeLabel, isIdentical, isNaN, itemAt, iterate, process, setTypeLabel, subsequence
Methods inherited from class net.sf.saxon.value.Value
asItem, asItem, asIterator, asValue, convertToJava, fromItem, getCanonicalLexicalRepresentation, getIterator, getSequenceLength, reduce
-
Constructor Details
-
NumericValue
public NumericValue()
-
-
Method Details
-
parseNumber
Get a numeric value by parsing a string; the type of numeric value depends on the lexical form of the string, following the rules for XPath numeric literals.- Parameters:
in
- the input string- Returns:
- a NumericValue representing the value of the string. Returns Double.NaN if the value cannot be parsed as a string.
-
getDoubleValue
public abstract double getDoubleValue()Get the numeric value as a double- Returns:
- A double representing this numeric value; NaN if it cannot be converted
-
getFloatValue
public abstract float getFloatValue()Get the numeric value converted to a float- Returns:
- a float representing this numeric value; NaN if it cannot be converted
-
getDecimalValue
Get the numeric value converted to a decimal- Returns:
- a decimal representing this numeric value;
- Throws:
XPathException
- if the value cannot be converted, for example if it is NaN or infinite
-
isInteger
Test whether a value is an integer (an instance of a subtype of xs:integer)- Parameters:
value
- the value being tested- Returns:
- true if the value is an instance of xs:integer or a type derived therefrom
-
longValue
Return the numeric value as a Java long.- Returns:
- the numeric value as a Java long. This performs truncation towards zero.
- Throws:
XPathException
- if the value cannot be converted
-
negate
Change the sign of the number- Returns:
- a value, of the same type as the original, with its sign inverted
-
floor
Implement the XPath floor() function- Returns:
- a value, of the same type as that supplied, rounded towards minus infinity
-
ceiling
Implement the XPath ceiling() function- Returns:
- a value, of the same type as that supplied, rounded towards plus infinity
-
round
Implement the XPath round() function- Returns:
- a value, of the same type as that supplied, rounded towards the nearest whole number (0.5 rounded up)
-
roundHalfToEven
Implement the XPath 2.0 round-half-to-even() function- Parameters:
scale
- the decimal position for rounding: e.g. 2 rounds to a multiple of 0.01, while -2 rounds to a multiple of 100- Returns:
- a value, of the same type as the original, rounded towards the nearest multiple of 10**(-scale), with rounding towards the nearest even number if two values are equally near
-
signum
public abstract int signum()Determine whether the value is negative, zero, or positive- Returns:
- -1 if negative, 0 if zero (including negative zero) or NaN, +1 if positive
-
isWholeNumber
public abstract boolean isWholeNumber()Determine whether the value is a whole number, that is, whether it compares equal to some integer- Returns:
- true if the value is a whole number
-
abs
Get the absolute value as defined by the XPath abs() function- Returns:
- the absolute value
- Since:
- 9.2
-
getXPathComparable
public final Object getXPathComparable(boolean ordered, StringCollator collator, XPathContext context) Get a Comparable value that implements the XPath ordering comparison semantics for this value. Returns null if the value is not comparable according to XPath rules. The implementation for all kinds of NumericValue returns the value itself.- Specified by:
getXPathComparable
in classAtomicValue
- Parameters:
ordered
-collator
-context
-- Returns:
- an Object whose equals() and hashCode() methods implement the XPath comparison semantics with respect to this atomic value. If ordered is specified, the result will either be null if no ordering is defined, or will be a Comparable
-
compareTo
Compare the value to another numeric value- Specified by:
compareTo
in interfaceComparable
- Parameters:
other
- The other numeric value- Returns:
- -1 if this one is the lower, 0 if they are numerically equal, +1 if this one is the higher, or if either value is NaN. Where NaN values are involved, they should be handled by the caller before invoking this method.
- Throws:
ClassCastException
- if the other value is not a NumericValue (the parameter is declared as Object to satisfy the Comparable interface)
-
compareTo
public abstract int compareTo(long other) Compare the value to a long- Parameters:
other
- the value to be compared with- Returns:
- -1 if this is less, 0 if this is equal, +1 if this is greater or if this is NaN
-
equals
The equals() function compares numeric equality among integers, decimals, floats, doubles, and their subtypes- Specified by:
equals
in classAtomicValue
- Parameters:
other
- the value to be compared with this one- Returns:
- true if the two values are numerically equal
-
hashCode
public abstract int hashCode()hashCode() must be the same for two values that are equal. One way to ensure this is to convert the value to a double, and take the hashCode of the double. But this is expensive in the common case where we are comparing integers. So we adopt the rule: for values that are in the range of a Java Integer, we use the int value as the hashcode. For values outside that range, we convert to a double and take the hashCode of the double. This method needs to have a compatible implementation in each subclass.- Overrides:
hashCode
in classValue<AtomicValue>
- Returns:
- the hash code of the numeric value
-
toString
Produce a string representation of the value- Overrides:
toString
in classAtomicValue
- Returns:
- The result of casting the number to a string
-