net.sf.saxon.value
Class DecimalValue

java.lang.Object
  extended by net.sf.saxon.value.Value
      extended by net.sf.saxon.value.AtomicValue
          extended by net.sf.saxon.value.NumericValue
              extended by net.sf.saxon.value.DecimalValue
All Implemented Interfaces:
Serializable, Comparable, PullEvent, SequenceIterable, GroundedValue, Item, ValueRepresentation, ConversionResult

public final class DecimalValue
extends NumericValue

A decimal value

See Also:
Serialized Form

Nested Class Summary
protected static class DecimalValue.DecimalComparable
           
 
Field Summary
static BigDecimal BIG_DECIMAL_ONE
           
static BigDecimal BIG_DECIMAL_ONE_MILLION
           
static BigInteger BIG_INTEGER_TEN
           
static int DIVIDE_PRECISION
           
static DecimalValue ONE
           
static DecimalValue ZERO
           
 
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
DecimalValue(BigDecimal value)
          Constructor supplying a BigDecimal
DecimalValue(double in)
          Constructor supplying a double
DecimalValue(long in)
          Constructor supplying a long integer
 
Method Summary
 NumericValue abs()
          Get the absolute value as defined by the XPath abs() function
static boolean castableAsDecimal(CharSequence in)
          Test whether a string is castable to a decimal value
 NumericValue ceiling()
          Implement the XPath ceiling() function
 int compareTo(long other)
          Compare the value to a long
 int compareTo(Object other)
          Compare the value to another numeric value
 ConversionResult convertPrimitive(BuiltInAtomicType requiredType, boolean validate, XPathContext context)
          Convert to target data type
 AtomicValue copyAsSubType(AtomicType typeLabel)
          Create a copy of this atomic value, with a different type label
static FastStringBuffer decimalToString(BigDecimal value, FastStringBuffer fsb)
          Convert a decimal value to a string, using the XPath rules for formatting
 boolean effectiveBooleanValue()
          Get the effective boolean value of the value
 NumericValue floor()
          Implement the XPath floor() function
 CharSequence getCanonicalLexicalRepresentation()
          Get the canonical lexical representation as defined in XML Schema.
 BigDecimal getDecimalValue()
          Get the value
 CharSequence getPrimitiveStringValue()
          Get the value as a String
 BuiltInAtomicType getPrimitiveType()
          Determine the primitive type of the value.
 Comparable getSchemaComparable()
          Get an object that implements XML Schema comparison semantics
 int hashCode()
          Get the hashCode.
 boolean isIdentical(Value v)
          Determine whether two atomic values are identical, as determined by XML Schema rules.
 boolean isWholeNumber()
          Determine whether the value is a whole number, that is, whether it compares equal to some integer
static ConversionResult makeDecimalValue(CharSequence in, boolean validate)
          Factory method to construct a DecimalValue from a string
 NumericValue negate()
          Negate the value
 NumericValue round()
          Implement the XPath round() function
 NumericValue roundHalfToEven(int scale)
          Implement the XPath round-half-to-even() function
 double signum()
          Determine whether the value is negative, zero, or positive
 
Methods inherited from class net.sf.saxon.value.NumericValue
equals, getDoubleValue, getFloatValue, getXPathComparable, isInteger, longValue, parseNumber, promote, toString
 
Methods inherited from class net.sf.saxon.value.AtomicValue
asAtomic, checkPermittedContents, convert, convert, getCardinality, getComponent, getItemType, getLength, getStringValue, getStringValueCS, getTypedValue, getTypeLabel, isNaN, itemAt, iterate, process, setTypeLabel, subsequence
 
Methods inherited from class net.sf.saxon.value.Value
asItem, asItem, asIterator, asValue, convertToJava, fromItem, getIterator, iterate, reduce, stringToNumber
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

DIVIDE_PRECISION

public static final int DIVIDE_PRECISION
See Also:
Constant Field Values

BIG_DECIMAL_ONE

public static final BigDecimal BIG_DECIMAL_ONE

BIG_INTEGER_TEN

public static final BigInteger BIG_INTEGER_TEN

BIG_DECIMAL_ONE_MILLION

public static final BigDecimal BIG_DECIMAL_ONE_MILLION

ZERO

public static final DecimalValue ZERO

ONE

public static final DecimalValue ONE
Constructor Detail

DecimalValue

public DecimalValue(BigDecimal value)
Constructor supplying a BigDecimal

Parameters:
value - the value of the DecimalValue

DecimalValue

public DecimalValue(double in)
             throws ValidationException
Constructor supplying a double

Parameters:
in - the value of the DecimalValue
Throws:
ValidationException

DecimalValue

public DecimalValue(long in)
Constructor supplying a long integer

Parameters:
in - the value of the DecimalValue
Method Detail

makeDecimalValue

public static ConversionResult makeDecimalValue(CharSequence in,
                                                boolean validate)
Factory method to construct a DecimalValue from a string

Parameters:
in - the value of the DecimalValue
validate - true if validation is required; false if the caller knows that the value is valid
Returns:
the required DecimalValue if the input is valid, or a ValidationFailure encapsulating the error message if not.

castableAsDecimal

public static boolean castableAsDecimal(CharSequence in)
Test whether a string is castable to a decimal value

Parameters:
in - the string to be tested
Returns:
true if the string has the correct format for a decimal

copyAsSubType

public AtomicValue copyAsSubType(AtomicType typeLabel)
Create a copy of this atomic value, with a different type label

Specified by:
copyAsSubType in class AtomicValue
Parameters:
typeLabel - the type label of the new copy. The caller is responsible for checking that the value actually conforms to this type.
Returns:
the copied value

getPrimitiveType

public BuiltInAtomicType getPrimitiveType()
Determine the primitive type of the value. This delivers the same answer as getItemType().getPrimitiveItemType(). The primitive types are the 19 primitive types of XML Schema, plus xs:integer, xs:dayTimeDuration and xs:yearMonthDuration, and xs:untypedAtomic. For external objects, the result is AnyAtomicType.

Specified by:
getPrimitiveType in class AtomicValue
Returns:
the primitive type

getDecimalValue

public BigDecimal getDecimalValue()
Get the value

Overrides:
getDecimalValue in class NumericValue
Returns:
a decimal representing this numeric value;

hashCode

public int hashCode()
Get the hashCode. This must conform to the rules for other NumericValue hashcodes

Specified by:
hashCode in class NumericValue
Returns:
the hash code of the numeric value
See Also:
NumericValue.hashCode()

effectiveBooleanValue

public boolean effectiveBooleanValue()
Description copied from class: AtomicValue
Get the effective boolean value of the value

Overrides:
effectiveBooleanValue in class AtomicValue
Returns:
true, unless the value is boolean false, numeric zero, or zero-length string

convertPrimitive

public ConversionResult convertPrimitive(BuiltInAtomicType requiredType,
                                         boolean validate,
                                         XPathContext context)
Convert to target data type

Specified by:
convertPrimitive in class AtomicValue
Parameters:
requiredType - the required atomic type. This must either be a primitive type, or a built-in type derived from the same primitive type as this atomic value.
validate - true if validation is required. If set to false, the caller guarantees that the value is valid for the target data type, and that further validation is therefore not required. Note that a validation failure may be reported even if validation was not requested.
context - The conversion context to be used. This is required at present only when converting to xs:Name or similar types: it determines the NameChecker to be used.
Returns:
the result of the conversion, if successful. If unsuccessful, the value returned will be a ValidationFailure. The caller must check for this condition. No exception is thrown, instead the exception information will be encapsulated within the ValidationFailure.

getCanonicalLexicalRepresentation

public CharSequence getCanonicalLexicalRepresentation()
Get the canonical lexical representation as defined in XML Schema. This is not always the same as the result of casting to a string according to the XPath rules. For xs:decimal, the canonical representation always contains a decimal point.

Overrides:
getCanonicalLexicalRepresentation in class Value
Returns:
the canonical lexical representation if defined in XML Schema; otherwise, the result of casting to string according to the XPath 2.0 rules

getPrimitiveStringValue

public CharSequence getPrimitiveStringValue()
Get the value as a String

Specified by:
getPrimitiveStringValue in class AtomicValue
Returns:
a String representation of the value

decimalToString

public static FastStringBuffer decimalToString(BigDecimal value,
                                               FastStringBuffer fsb)
Convert a decimal value to a string, using the XPath rules for formatting

Parameters:
value - the decimal value to be converted
fsb - the FastStringBuffer to which the value is to be appended
Returns:
the supplied FastStringBuffer, suitably populated

negate

public NumericValue negate()
Negate the value

Specified by:
negate in class NumericValue
Returns:
a value, of the same type as the original, with its sign inverted

floor

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

Specified by:
floor in class NumericValue
Returns:
a value, of the same type as that supplied, rounded towards minus infinity

ceiling

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

Specified by:
ceiling in class NumericValue
Returns:
a value, of the same type as that supplied, rounded towards plus infinity

round

public NumericValue round()
Implement the XPath round() function

Specified by:
round in class NumericValue
Returns:
a value, of the same type as that supplied, rounded towards the nearest whole number (0.5 rounded up)

roundHalfToEven

public NumericValue roundHalfToEven(int scale)
Implement the XPath round-half-to-even() function

Specified by:
roundHalfToEven in class NumericValue
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 double signum()
Determine whether the value is negative, zero, or positive

Specified by:
signum in class NumericValue
Returns:
-1 if negative, 0 if zero, +1 if positive, NaN if NaN

isWholeNumber

public boolean isWholeNumber()
Determine whether the value is a whole number, that is, whether it compares equal to some integer

Specified by:
isWholeNumber in class NumericValue
Returns:
true if the value is a whole number

abs

public NumericValue abs()
Get the absolute value as defined by the XPath abs() function

Specified by:
abs in class NumericValue
Returns:
the absolute value
Since:
9.2

compareTo

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

Specified by:
compareTo in interface Comparable
Overrides:
compareTo in class NumericValue
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.

compareTo

public int compareTo(long other)
Compare the value to a long

Specified by:
compareTo in class NumericValue
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

getSchemaComparable

public Comparable getSchemaComparable()
Get an object that implements XML Schema comparison semantics

Specified by:
getSchemaComparable in class AtomicValue
Returns:
a Comparable that follows XML Schema comparison rules

isIdentical

public boolean isIdentical(Value v)
Determine whether two atomic values are identical, as determined by XML Schema rules. This is a stronger test than equality (even schema-equality); for example two dateTime values are not identical unless they are in the same timezone.

Note that even this check ignores the type annotation of the value. The integer 3 and the short 3 are considered identical, even though they are not fully interchangeable. "Identical" means the same point in the value space, regardless of type annotation.

NaN is identical to itself.

Overrides:
isIdentical in class AtomicValue
Parameters:
v - the other value to be compared with this one
Returns:
true if the two values are identical, false otherwise.


Copyright (c) Saxonica Limited. All rights reserved.