net.sf.saxon.value
Class NumericValue

java.lang.Object
  extended bynet.sf.saxon.value.Value
      extended bynet.sf.saxon.value.AtomicValue
          extended bynet.sf.saxon.value.NumericValue
All Implemented Interfaces:
Comparable, Expression, Item, Serializable
Direct Known Subclasses:
DecimalValue, DoubleValue, FloatValue, IntegerValue

public abstract class NumericValue
extends AtomicValue
implements Comparable

NumericValue is an abstract superclass for IntegerValue, DecimalValue, FloatValue, and DoubleValue

See Also:
Serialized Form

Constructor Summary
NumericValue()
           
 
Method Summary
abstract  NumericValue arithmetic(int operator, NumericValue other)
          Perform a binary arithmetic operation
 double asDouble()
          Get the numeric value as a double
 long asLong()
          Return the numeric value as a Java long.
abstract  NumericValue ceiling()
          Implement the XPath ceiling() function
 int compareTo(Object other)
          Compare the value to another numeric value
 boolean equals(Object other)
          The equals() function compares numeric equality among integers, decimals, floats, doubles, and their subtypes
abstract  NumericValue floor()
          Implement the XPath floor() function
abstract  int hashCode()
          hashCode() must be the same for two values that are equal.
 boolean isNaN()
          Test whether the value is the double/float value NaN
abstract  boolean isWholeNumber()
          Determine whether the value is a whole number, that is, whether it compares equal to some integer
abstract  NumericValue negate()
          Change the sign of the number
static ItemType promote(ItemType v1, ItemType v2)
          Identify lowest common supertype of two numeric values for promotion purposes
abstract  NumericValue round()
          Implement the XPath round() function
abstract  NumericValue roundToHalfEven(int scale)
          Implement the XPath 2.0 round-to-half-even() function
 String toString()
          Produce a string representation of the value
 
Methods inherited from class net.sf.saxon.value.AtomicValue
conversionPreference, convert, convert, convert, convertToJava, effectiveBooleanValue, evaluateAsString, evaluateItem, getCardinality, getComponent, getPrimitiveValue, getTypedValue, iterate
 
Methods inherited from class net.sf.saxon.value.Value
analyze, asItem, asValue, collapseWhitespace, getDependencies, getSpecialProperties, getSubExpressions, inverse, normalizeWhitespace, promote, simplify, stringToInteger, stringToNumber, trimWhitespace
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface net.sf.saxon.om.Item
getStringValue
 
Methods inherited from interface net.sf.saxon.expr.Expression
display, getItemType
 

Constructor Detail

NumericValue

public NumericValue()
Method Detail

asDouble

public double asDouble()
Get the numeric value as a double

Returns:
A double representing this numeric value; NaN if it cannot be converted

isNaN

public boolean isNaN()
Test whether the value is the double/float value NaN


asLong

public long asLong()
            throws XPathException
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

public abstract NumericValue negate()
                             throws XPathException
Change the sign of the number

Returns:
a value, of the same type as the original, with its sign inverted
Throws:
XPathException - if the conversion is not possible

floor

public abstract NumericValue floor()
Implement the XPath floor() function

Returns:
a value, of the same type as that supplied, rounded towards minus infinity

ceiling

public abstract NumericValue ceiling()
Implement the XPath ceiling() function

Returns:
a value, of the same type as that supplied, rounded towards plus infinity

round

public abstract NumericValue 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)

roundToHalfEven

public abstract NumericValue roundToHalfEven(int scale)
Implement the XPath 2.0 round-to-half-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

arithmetic

public abstract NumericValue arithmetic(int operator,
                                        NumericValue other)
                                 throws XPathException
Perform a binary arithmetic operation

Parameters:
operator - the binary arithmetic operation to be performed. Uses the constants defined in the Tokenizer class
other - the other operand
Returns:
the result of the arithmetic operation
Throws:
XPathException - if an arithmetic error occurs
See Also:
Tokenizer

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

compareTo

public int compareTo(Object other)
Compare the value to another numeric value

Specified by:
compareTo in interface Comparable
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.
Throws:
ClassCastException - if the other value is not a NumericValue (the parameter is declared as Object to satisfy the Comparable interface)

equals

public final boolean equals(Object other)
The equals() function compares numeric equality among integers, decimals, floats, doubles, and their subtypes

Parameters:
other - the value to be compared with this one
Returns:
true if the two values are numerically equal

promote

public static ItemType promote(ItemType v1,
                               ItemType v2)
Identify lowest common supertype of two numeric values for promotion purposes

Parameters:
v1 - the item type of the first operand
v2 - the item type of the second operand
Returns:
the item type that should be used for arithmetic between operands of the two specified item types

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.

Returns:
the hash code of the numeric value

toString

public String toString()
Produce a string representation of the value

Returns:
The result of casting the number to a string