Package net.sf.saxon.value
Class NumericValue
java.lang.Object
net.sf.saxon.value.AtomicValue
net.sf.saxon.value.NumericValue
- All Implemented Interfaces:
Comparable<XPathComparable>
,Iterable<AtomicValue>
,AtomicMatchKey
,XPathComparable
,AtomicSequence
,GroundedValue
,IdentityComparable
,Item
,Sequence
,ConversionResult
,ContextFreeAtomicValue
- Direct Known Subclasses:
DecimalValue
,DoubleValue
,FloatValue
public abstract class NumericValue
extends AtomicValue
implements XPathComparable, AtomicMatchKey, ContextFreeAtomicValue
NumericValue is an abstract superclass for IntegerValue, DecimalValue,
FloatValue, and DoubleValue
-
Field Summary
Fields inherited from class net.sf.saxon.value.AtomicValue
typeLabel
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionabstract NumericValue
abs()
Get the absolute value as defined by the XPath abs() functionabstract int
Test whether a number is a possible subscript into a sequence, that is, a whole number greater than zero and less than 2^31abstract NumericValue
ceiling()
Implement the XPath ceiling() functionabstract int
compareTo
(long other) Compare the value to a longint
compareTo
(XPathComparable other) Compare the value to another numeric valueabstract boolean
Get the effective boolean value of the value.final boolean
The equals() function compares numeric equality among integers, decimals, floats, doubles, and their subtypes.abstract 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 floatGet an XPathComparable object that supports the semantics of context-free eq and lt comparisons between atomic values.getXPathComparable
(StringCollator collator, int implicitTimezone) Get an object value that implements the XPath equality and ordering comparison semantics for this value.final AtomicMatchKey
getXPathMatchKey
(StringCollator collator, int implicitTimezone) 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)boolean
Ask whether this value is negative zeroabstract boolean
Ask 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
round
(int scale, Round.RoundingRule roundingRule) Implement the XPath 2.0 round-half-to-even() and XPath 4.0 round#3 functionsshow()
Produce a string representation of the valueabstract int
signum()
Ask whether the value is negative, zero, or positiveMethods inherited from class net.sf.saxon.value.AtomicValue
asAtomic, asMapKey, atomize, checkPermittedContents, checkValidInJavascript, copyAsSubType, getCanonicalLexicalRepresentation, getCardinality, getComponent, getGenre, getItemType, getLength, getPrimitiveStringValue, getPrimitiveType, getUnicodeStringValue, getUType, head, identityHashCode, isIdentical, isIdentical, isNaN, isUntypedAtomic, itemAt, iterate, iterator, toShortString, toString
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface net.sf.saxon.expr.sort.AtomicMatchKey
asAtomic
Methods inherited from interface net.sf.saxon.om.GroundedValue
asIterable, concatenate, containsNode, materialize
Methods inherited from interface net.sf.saxon.om.Item
getStringValue, isStreamed, reduce, subsequence
Methods inherited from interface java.lang.Iterable
forEach, spliterator
Methods inherited from interface net.sf.saxon.om.Sequence
makeRepeatable
-
Constructor Details
-
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:
ValidationException
- if the value cannot be converted, for example if it is NaN or infinite
-
effectiveBooleanValue
public abstract boolean effectiveBooleanValue()Get the effective boolean value of the value. This override of this method throws no exceptions.- Specified by:
effectiveBooleanValue
in interfaceGroundedValue
- Overrides:
effectiveBooleanValue
in classAtomicValue
- Returns:
- true, unless the value is boolean false, numeric zero, or zero-length string
-
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- Parameters:
scale
- the number of decimal places required in the result (supply 0 for rounding to an integer)- Returns:
- a value, of the same type as that supplied, rounded towards the nearest whole number (0.5 rounded up)
-
round
Implement the XPath 2.0 round-half-to-even() and XPath 4.0 round#3 functions- Parameters:
scale
- the decimal position for rounding: e.g. 2 rounds to a multiple of 0.01, while -2 rounds to a multiple of 100roundingRule
- the rule to be used for rounding (up, down, towards zero, etc)- Returns:
- a value, of the same type as the original, rounded to a multiple of 10^(-scale), according to the rounding rules requested
-
signum
public abstract int signum()Ask whether the value is negative, zero, or positive- Returns:
- -1 if negative, 0 if zero (including negative zero) or NaN, +1 if positive
-
isNegativeZero
public boolean isNegativeZero()Ask whether this value is negative zero- Returns:
- true if this value is float or double negative zero
-
isWholeNumber
public abstract boolean isWholeNumber()Ask 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
-
asSubscript
public abstract int asSubscript()Test whether a number is a possible subscript into a sequence, that is, a whole number greater than zero and less than 2^31- Returns:
- the number as an int if it is a possible subscript, or -1 otherwise
-
abs
Get the absolute value as defined by the XPath abs() function- Returns:
- the absolute value
- Since:
- 9.2
-
getXPathMatchKey
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:
getXPathMatchKey
in classAtomicValue
- Parameters:
collator
- the collation to be used when comparing stringsimplicitTimezone
- the implicit timezone in the dynamic context, used when comparing dates/times with and without timezone- Returns:
- a collation key that implements the comparison semantics
-
getXPathComparable
public XPathComparable getXPathComparable(StringCollator collator, int implicitTimezone) throws NoDynamicContextException Description copied from class:AtomicValue
Get an object value that implements the XPath equality and ordering comparison semantics for this value. A collation is supplied for comparing strings, and an implicit timezone for comparing date/time values that have no saved timezone. An atomic value may return itself as the result, provided that its ordering rules are independent of the collation and timezone, and provided that it implements the XPathComparable interface: which means that its compareTo, equals, and hashCode methods must be compatible with the rules for XPath value comparisons.- Specified by:
getXPathComparable
in classAtomicValue
- Parameters:
collator
- the collation to be used when comparing stringsimplicitTimezone
- the implicit timezone in the dynamic context, used when comparing dates/times with and without timezone- Returns:
- an Object that implements the XPath value comparison semantics with respect to this atomic value. For an atomic type that is not ordered (according to XPath rules), return null.
- Throws:
NoDynamicContextException
- if the supplied implicit timezone is "NO_TIMEZONE" (meaning unknown), and the implicit timezone is actually required because the value in question is a date/time value with no timezone. This can cause a failure to evaluate expressions statically (because the implicit timezone is not known statically), and it will then be caught, meaning that the expression has to be evaluated dynamically.
-
getXPathComparable
Description copied from interface:ContextFreeAtomicValue
Get an XPathComparable object that supports the semantics of context-free eq and lt comparisons between atomic values. Note that in many cases the returned XPathComparable will be the AtomicValue itself; however because of the constraints of the genericComparable
interface, this cannot be assumed.- Specified by:
getXPathComparable
in interfaceContextFreeAtomicValue
- Returns:
- an XPathComparable that can be used in comparisons with other atomic values.
-
compareTo
Compare the value to another numeric value- Specified by:
compareTo
in interfaceComparable<XPathComparable>
- 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.- Overrides:
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 classAtomicValue
- Returns:
- the hash code of the numeric value
-
show
Produce a string representation of the value- Overrides:
show
in classAtomicValue
- Returns:
- The result of casting the number to a string
-