com.davisor.data
Class SimpleNumberValue

java.lang.Object
  extended byjava.lang.Number
      extended bycom.davisor.data.SimpleNumberValue
All Implemented Interfaces:
java.lang.Comparable, Data, DataValue, com.davisor.core.Dupable, NumberValue, java.io.Serializable, SerializableData
Direct Known Subclasses:
BigDecimalData, BigIntegerData, CountData, DoubleData, FloatData, IntegerData, LongData

public abstract class SimpleNumberValue
extends java.lang.Number
implements NumberValue

SimpleNumberValue defines default implementations for NumberValue methods. SimpleNumberValue also inherits the Java Number, which makes it compatible with many external software components.

Simple number value objects contain a generic Java Number instance that represent the object number value. No explicit constrains are set for the exact implementation of the Number instance, making convenient number value assignments and type conversions possible.

Simple number values support also the concept of an unspecified number value (null). An unspecified number value does not take part in comparison or arithmetic operations, as appropriate in each case. In particular, unspecified values are not the same thing as using a default zero value, although in some cases they may yield similar results.

Since:
JDK1.2
See Also:
SimpleNumberType, Serialized Form

Field Summary
protected  NumberType M_type
          The type of this data (may be null).
protected  java.lang.Number M_value
          Underlying number value (may be null).
 
Constructor Summary
protected SimpleNumberValue(NumberType type)
          Create a new data object with given type.
protected SimpleNumberValue(NumberType type, java.lang.Number value)
          Create a new data object with given type and initial value.
protected SimpleNumberValue(SimpleNumberValue snv, boolean deep)
          Deep or shallow copy constructor.
 
Method Summary
abstract  DataValue abs()
          Returns absolute data value of current value.
 DataValue add(DataValue value)
          Adds data values.
 byte byteValue()
          Gets number byte value.
 DataValue ceil(DataValue precision)
          Quantizes the current value up to an even value within given precision.
 int compareTo(java.lang.Object o)
          Compares this number value object with another object.
 double deinterpolate(DataValue min, DataValue max)
          De-interpolates the current data value to a scalar value, in respect, with the given data value range.
 DataValue div(DataValue value)
          Divides the current value with given value.
 double doubleValue()
          Gets number double value.
 boolean equals(java.lang.Object o)
          Tests if this object is equal with another object.
 float floatValue()
          Gets the number float value.
 DataValue floor(DataValue precision)
          Quantizes the current value down to an even value within given precision.
 java.lang.String getFormat()
          Gets the type specification optimal for this particular value.
static java.lang.String getFormat(double value)
          Constructs a JavaDecimalFormat pattern suitable for displaying values of the given magnitude.
 Type getType()
          Gets the data type.
 int hashCode()
          Returns a hash code value for the object.
 DataValue interpolate(double value, DataValue max)
          Interpolates the given scalar value to data value, in respect, with the range defined by this value and the given maximum value.
 int intValue()
          Gets number int value.
 boolean isNull()
          Tests if this data object value is undetermined.
 long longValue()
          Gets number long value.
 DataValue max(DataValue value)
          Finds the larger of this and the given data value.
 DataValue min(DataValue value)
          Finds the smaller of two data value.
 DataValue mul(DataValue value)
          Multiplies the current value with given value.
abstract  DataValue negate()
          Returns the negation of current value.
 java.lang.Number numberValue()
          Gets the data Number value.
 void setValue(byte value)
          Sets the data byte value.
abstract  void setValue(double value)
          Sets the data double value.
 void setValue(float value)
          Sets the data float value.
 void setValue(int value)
          Sets the data int value.
abstract  void setValue(long value)
          Sets the data long value.
abstract  void setValue(java.lang.Number value)
          Sets the data Number value.
 void setValue(short value)
          Sets the data short value.
 short shortValue()
          Gets number short value.
 DataValue sub(DataValue value)
          Substracts a data value.
 java.lang.String toString()
          Gets the string representation of this number value.
 java.lang.String toXML()
          Gets the XML string representation of this value.
 DataValue unit(DataValue targetValue, double maxSteps)
          Gets the largest suitable unit step to represent a value change between this value and the given target value.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface com.davisor.data.DataValue
setValue, toStatement
 
Methods inherited from interface com.davisor.core.Dupable
dup
 

Field Detail

M_type

protected NumberType M_type
The type of this data (may be null).


M_value

protected java.lang.Number M_value
Underlying number value (may be null).

Constructor Detail

SimpleNumberValue

protected SimpleNumberValue(SimpleNumberValue snv,
                            boolean deep)
Deep or shallow copy constructor. If it is shallow, then the new object shares an underlying type with the original object. If it is deep, then the type is dupped. The value is expected to be an immutable number, and it is therefore copied by reference. null source data will leave both the type and value to null values.

Parameters:
snv - number value to be copied (may be null)
deep - determines if the copying is deep or shallow
See Also:
getType(), Type.dup()

SimpleNumberValue

protected SimpleNumberValue(NumberType type)
Create a new data object with given type.

Parameters:
type - the type of data (may be null)

SimpleNumberValue

protected SimpleNumberValue(NumberType type,
                            java.lang.Number value)
Create a new data object with given type and initial value.

Parameters:
type - the type of data (may be null)
value - the data number value (may be null)
Method Detail

compareTo

public int compareTo(java.lang.Object o)
Compares this number value object with another object. SimpleNumberValue data objects are comparable with all Number objects. The comparing is based on object double values.

If one of the two values is null or NaN, it will be considered the smaller of the two values. If both values are null or NaN, they will be considered equal. For an alternative way to compare data values, please see DataValue.min(com.davisor.data.DataValue) and DataValue.max(com.davisor.data.DataValue).

Specified by:
compareTo in interface java.lang.Comparable
Parameters:
o - object to compare this object with (may be null)
Returns:
a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object
Throws:
java.lang.ClassCastException - if the given object is not a Number

getType

public Type getType()
             throws InvalidDataException
Gets the data type.

Specified by:
getType in interface Data
Returns:
data type (may be null)
Throws:
InvalidDataException - if type retrieval fails

isNull

public boolean isNull()
Tests if this data object value is undetermined.

A simple number value is undetermined if the underlying number object is null, it has a NaN double value, or the double value can not be determined at all.

Specified by:
isNull in interface SerializableData
See Also:
Type.isNull(com.davisor.data.SerializableData)

abs

public abstract DataValue abs()
                       throws InvalidDataException
Returns absolute data value of current value.

Absolute values must satisfy a requirement that when added to any other value, the result must be greater than or equal to the original value.

Specified by:
abs in interface DataValue
Returns:
a new data object, sharing the current type of this object
Throws:
InvalidDataException - if absolute value can not be computed

add

public DataValue add(DataValue value)
              throws InvalidDataException
Adds data values.

Specified by:
add in interface DataValue
Parameters:
value - a data value to be added to this data value
Returns:
a new data object, sharing the current type of this object
Throws:
InvalidDataException - if the data values cannot be added.

ceil

public DataValue ceil(DataValue precision)
               throws InvalidDataException
Quantizes the current value up to an even value within given precision. The returned value is more than or equal with the current value, but not more than the current value added by given precision.

All number values are rounded off with the equation:

ceil(thisValue / precision) * precision

Specified by:
ceil in interface DataValue
Parameters:
precision - the precision within the quantization occurs
Returns:
a new data object, sharing the current type of this object
Throws:
InvalidDataException - if precision was of unacceptable type
See Also:
floor(com.davisor.data.DataValue)

deinterpolate

public double deinterpolate(DataValue min,
                            DataValue max)
                     throws InvalidDataException
De-interpolates the current data value to a scalar value, in respect, with the given data value range. This gives scalar values between [0,1], when the current data value is within the range, negative scalar values when the data value falls below the range and scalar values greater than 1.0 otherwise.

Number values are de-interpolated with the equation:

(thisValue - minValue) / (maxValue - minValue)

If current value is null, NaN is returned. If either of the range values is null, InvalidDataException is thrown.

Specified by:
deinterpolate in interface DataValue
Parameters:
min - a data value representing a range's lower limit
max - a data value representing a range's higher limit
Returns:
de-interpolated scalar value (may be NaN)
Throws:
InvalidDataException - if the de-interpolation fails
See Also:
DataValue.interpolate(double, com.davisor.data.DataValue), DeinterpolationException, TupleData.deinterpolate(com.davisor.data.DataValue, com.davisor.data.DataValue)

div

public DataValue div(DataValue value)
              throws InvalidDataException
Divides the current value with given value.

Specified by:
div in interface DataValue
Parameters:
value - the divider
Returns:
a new data object, sharing the current type of this object.
Throws:
InvalidDataException - if current value cannot be divided with given divider.

floor

public DataValue floor(DataValue precision)
                throws InvalidDataException
Quantizes the current value down to an even value within given precision. The returned value is less than or equal with the current value, but not less than the current value substracted by given precision.

All number values are rounded off with the equation:

floor(thisValue / precision) * precision

Specified by:
floor in interface DataValue
Parameters:
precision - the precision within the quantization occurs
Returns:
a new data object, sharing the current type of this object
Throws:
InvalidDataException - if precision was of unacceptable type
See Also:
ceil(com.davisor.data.DataValue)

getFormat

public java.lang.String getFormat()
Gets the type specification optimal for this particular value. The specification returned may differ from the current type specification. If so, setting the current type specification equal to the value returned by this method will ensure optimal value formatting and parsing for values of the same magnitude as this value.

This default implementation returns always null.

Specified by:
getFormat in interface DataValue
See Also:
Data.getType()

interpolate

public DataValue interpolate(double value,
                             DataValue max)
                      throws InvalidDataException
Interpolates the given scalar value to data value, in respect, with the range defined by this value and the given maximum value. Scalar values between [0,1] result in data values between the current value and the given maximum value. Negative scalar values result in values smaller than this value, and scalar values higher than 1.0 return data values above the given maximum.

All number values can interpolate with this simple equation:

minValue + value * (maxValue - minValue)

Specified by:
interpolate in interface DataValue
Parameters:
value - the scalar value to be interpolated
max - a data value representing a range's higher limit
Returns:
a new data object containing the result
Throws:
InvalidDataException - if interpolation fails
See Also:
DataValue.deinterpolate(com.davisor.data.DataValue, com.davisor.data.DataValue)

max

public DataValue max(DataValue value)
              throws InvalidDataException
Finds the larger of this and the given data value.

If the given value is not a Number, or if it's double value can not be resolved for any reason, a reference to this instance is returned. Otherwise, if the current double value can not be resolved for any reason, a reference to the given value is returned. Otherwise, a reference to the object with the greater double value is returned. If the values are equal, a reference to this instance is returned.

Specified by:
max in interface DataValue
Parameters:
value - a data value to be compared with this data value
Returns:
the larger of the two compared data values
Throws:
InvalidDataException - if the data value cannot be compared
See Also:
min(com.davisor.data.DataValue), compareTo(java.lang.Object)

min

public DataValue min(DataValue value)
              throws InvalidDataException
Finds the smaller of two data value.

If the given value is not a Number, or if it's double value can not be resolved for any reason, a reference to this instance is returned. Otherwise, if the current double value can not be resolved for any reason, a reference to the given value is returned. Otherwise, a reference to the object with the smaller double value is returned. If the values are equal, a reference to this instance is returned.

Specified by:
min in interface DataValue
Parameters:
value - a data value to be compared with this data value
Returns:
the smaller of the two compared data values
Throws:
InvalidDataException - if the data value cannot be compared
See Also:
max(com.davisor.data.DataValue), compareTo(java.lang.Object)

mul

public DataValue mul(DataValue value)
              throws InvalidDataException
Multiplies the current value with given value.

Specified by:
mul in interface DataValue
Parameters:
value - the multiplier
Returns:
a new data object, sharing the current type of this object.
Throws:
InvalidDataException - if current value cannot be multiplied with given multiplier

negate

public abstract DataValue negate()
                          throws InvalidDataException
Returns the negation of current value.

Specified by:
negate in interface DataValue
Returns:
a new data object, sharing the current type of this object
Throws:
InvalidDataException - if absolute value can not be computed

sub

public DataValue sub(DataValue value)
              throws InvalidDataException
Substracts a data value.

Specified by:
sub in interface DataValue
Parameters:
value - a data value to be substracted from this data value
Returns:
a new data object, sharing the current type of this object
Throws:
InvalidDataException - if value can not be substracted.

toXML

public java.lang.String toXML()
Gets the XML string representation of this value.

This default implementation returns the object plain string representation, as it is assumed to contain no special characters.

Specified by:
toXML in interface DataValue
Returns:
complete string representing the data.
See Also:
toString()

unit

public DataValue unit(DataValue targetValue,
                      double maxSteps)
               throws InvalidDataException
Gets the largest suitable unit step to represent a value change between this value and the given target value. If the values are equal, a type specific default unit step is returned. If the values are not equal, then the given target value must be reached from this value by adding the returned unit step to this value once or more.

This method measures which u=x*10^n where x belongs to (1,2,5) would fit nicely within the range between the current value and given target value. The resulting unit step will be a DoubleData value of u. If the target value is missing or equals to this value, the range is considered to be equal to the absolute value of current value.

Specified by:
unit in interface DataValue
Parameters:
targetValue - target value to be reached (may be null)
maxSteps - maximum number of steps (zero leaves the choise open)
Returns:
a new data object, sharing the current type of this object
Throws:
InvalidDataException - if target value is unsuitable

byteValue

public byte byteValue()
Gets number byte value. The value is mapped through current number type, if available.

Specified by:
byteValue in interface NumberValue
Throws:
java.lang.NullPointerException - if number value is not set
See Also:
NumberType.byteValue(java.lang.String, java.text.ParsePosition)

doubleValue

public double doubleValue()
Gets number double value. The value is mapped through current number type, if available.

Please note, that a missing double value may be expressed either with a Double.NaN value, or by throwing an appropriate exception. This method will report a missing value by throwing a NullPointerException. An exception is used because an unexpected Double.NaN value may be difficult to notice, and later trace back down to it's source. This policy is also more consistent with integer numbers that do not even have the option to use a NaN value.

Specified by:
doubleValue in interface NumberValue
Throws:
java.lang.NullPointerException - if number value is not set
See Also:
NumberType.doubleValue(java.lang.String, java.text.ParsePosition)

floatValue

public float floatValue()
Gets the number float value. The value is mapped through current number type, if available.

Please note, that a missing float value may be expressed either with a Float.NaN value, or by throwing an appropriate exception. This method will report a missing value by throwing a NullPointerException. An exception is used because an unexpected Float.NaN value may be difficult to notice, and later trace back down to it's source. This policy is also more consistent with integer numbers that do not even have the option to use a NaN value.

Specified by:
floatValue in interface NumberValue
Throws:
java.lang.NullPointerException - if number value is not set
See Also:
NumberType.floatValue(java.lang.String, java.text.ParsePosition)

intValue

public int intValue()
Gets number int value. The value is mapped through current number type, if available.

Specified by:
intValue in interface NumberValue
Throws:
java.lang.NullPointerException - if number value is not set
See Also:
NumberType.intValue(java.lang.String, java.text.ParsePosition)

longValue

public long longValue()
Gets number long value. The value is mapped through current number type, if available.

Specified by:
longValue in interface NumberValue
Throws:
java.lang.NullPointerException - if number value is not set
See Also:
NumberType.longValue(java.lang.String, java.text.ParsePosition)

shortValue

public short shortValue()
Gets number short value. The value is mapped through current number type, if available.

Specified by:
shortValue in interface NumberValue
Throws:
java.lang.NullPointerException - if number value is not set
See Also:
NumberType.shortValue(java.lang.String, java.text.ParsePosition)

numberValue

public java.lang.Number numberValue()
Gets the data Number value.

Specified by:
numberValue in interface NumberValue

setValue

public void setValue(byte value)
              throws InvalidDataException
Sets the data byte value.

This implementation forwards the call to setValue(short).

Specified by:
setValue in interface NumberValue
Throws:
InvalidDataException - if the value cannot be converted to a byte value.

setValue

public abstract void setValue(double value)
                       throws InvalidDataException
Sets the data double value.

Specified by:
setValue in interface NumberValue
Throws:
InvalidDataException - if the value cannot be converted to a double value.

setValue

public void setValue(float value)
              throws InvalidDataException
Sets the data float value.

This implementation forwards the call to setValue(double).

Specified by:
setValue in interface NumberValue
Throws:
InvalidDataException - if the value cannot be converted to a float value.

setValue

public void setValue(int value)
              throws InvalidDataException
Sets the data int value.

This implementation forwards the call to setValue(long).

Specified by:
setValue in interface NumberValue
Throws:
InvalidDataException - if the value cannot be converted to a int value.

setValue

public abstract void setValue(java.lang.Number value)
                       throws InvalidDataException
Sets the data Number value.

Specified by:
setValue in interface NumberValue
Throws:
InvalidDataException - if the value cannot be converted

setValue

public abstract void setValue(long value)
                       throws InvalidDataException
Sets the data long value.

Specified by:
setValue in interface NumberValue
Throws:
InvalidDataException - if the value cannot be converted to a long value.

setValue

public void setValue(short value)
              throws InvalidDataException
Sets the data short value.

This implementation forwards the call to setValue(int).

Specified by:
setValue in interface NumberValue
Throws:
InvalidDataException - if the value cannot be converted to a short value.

equals

public boolean equals(java.lang.Object o)
Tests if this object is equal with another object.

SimpleNumberValue data objects are equal with any Number objects that share the same double value. In particular, the type of the other object does not matter.


hashCode

public int hashCode()
Returns a hash code value for the object.


toString

public java.lang.String toString()
Gets the string representation of this number value. The number value is formatted with current number type NumberType.stringValue(Number) method.

If current number type is null, number native formatting is used. If current value is null or an InvalidDataException occurs during number formatting, the exception is silently ignored, and an empty string is returned.

Specified by:
toString in interface DataValue
Returns:
number value string representation, or an empty string
See Also:
NumberType.stringValue(Number)

getFormat

public static java.lang.String getFormat(double value)
Constructs a JavaDecimalFormat pattern suitable for displaying values of the given magnitude.



Copyright © 2001-2004 Davisor Oy. All Rights Reserved.