com.rubecula.jquantity
Class BaseNumber

java.lang.Object
  extended bycom.rubecula.util.Item
      extended bycom.rubecula.jquantity.BaseNumber
All Implemented Interfaces:
Arithmetical, Auditable, java.lang.Comparable, Fuzzy, Identifiable, Millable, Presentable, java.io.Serializable
Direct Known Subclasses:
Approximation, JQuantity, Manifest, MutableNumber

public abstract class BaseNumber
extends Item
implements Arithmetical, Fuzzy, Millable

Abstract (scalar) numerical class from which to derive other concrete classes. Ideally, (if Java was 100% perfect and supported multiple inheritance of abstract classes) this class would inherit from both java.lang.Number AND Item. Instead, we just copy the Number code. // TODO Can we solve this problem by defining an inner class to handle Number methods? In addition to the Comparable, Auditable and Presentable properties of the Item base class, objects of this class have a numerical value, which "may be" fuzzy - that is they support the Fuzzy interface.

// TODO extend for dates.

Since:
V_0_1
Version:
$Revision: 1.28 $
Author:
Robin Hillyard
See Also:
Serialized Form

Field Summary
private static long serialVersionUID
          use serialVersionUID from JDK 1.0.2 for interoperability
 
Fields inherited from class com.rubecula.util.Item
JQUANTITY_TEST
 
Fields inherited from interface com.rubecula.jquantity.Fuzzy
$DistGaussian, $DistInvalid, $DistNone, $DistUniform, $Names
 
Constructor Summary
BaseNumber()
           
 
Method Summary
protected  BaseNumber abs()
          Method to yield this or a copy with the sign changed, depending whether the value is positive or negative.
 int bestModel(Fuzzy that)
          Method to help in combining distribution models.
protected static int BestModel(int modelX, int modelY)
          Class method to help in combining distribution models.
 byte byteValue()
          Returns the value of the specified number as a byte.
(package private)  int characteristic()
          Method to yield the characteristic of a BaseNumber.
(package private) static int Characteristic(double x)
          Package-scope class method to yield the characteristic of value x (regardless of sign).
protected  BaseNumber difference(BaseNumber subtrahend)
          Method to yield the difference of this less a BaseNumber (subtrahend).
abstract  double doubleValue()
          Returns the value of the specified number as a double.
abstract  float floatValue()
          Returns the value of the specified number as a float.
 double getBound()
          Method to get the relative bound of this non-fuzzy value;
 double getBoundAbs()
          Method to get the absolute bound of this non-fuzzy value;
 int getModel()
          Method to get the error distribution model of this non-fuzzy value.
 Presentation impartFuzziness(NumberPresentation presentation)
          Method to impart a NumberPresentation presentation with appropriate fuzzy information and ensure that the resulting string value is properly rounded.
abstract  int intValue()
          Returns the value of the specified number as an int.
 boolean isEqual(BaseNumber other)
          Method to determine effect equality with another BaseNumber.
 boolean isExact()
          Method to return true for this non-fuzzy instance.
 boolean isUncertain()
          Method to determine if this object is of uncertain value due to fuzziness.
 boolean isZero()
          Method to make standard test against zero.
abstract  long longValue()
          Returns the value of the specified number as a long.
protected abstract  BaseNumber minus()
          Method to yield a copy of this but with the sign changed.
protected abstract  BaseNumber pow(int power)
          Method to raise this to the power power return the result.
protected abstract  BaseNumber product(BaseNumber multiplicand)
          Method to multiply multiplicand by this and return their product as the result.
protected abstract  BaseNumber scaled(long factor)
          Method to multiply multiplicand by this and return their product as the result.
 short shortValue()
          Returns the value of the specified number as a short.
protected abstract  BaseNumber sum(BaseNumber addend)
          Method to add addend to this and return their sum as the result.
static BaseNumber valueOf(java.lang.String string)
          Factory method to create a BaseNumber from a String string, without an explicit identifier (the string itself is used as the id).
static BaseNumber valueOf(java.lang.String string, java.lang.String id)
          Factory method to create a BaseNumber from a String string, optionally identified by id.
(package private) static double valueOfLog10(double x)
          Package-scope class method to yield the value of log[base 10] (x).
 
Methods inherited from class com.rubecula.util.Item
audit, audit, compareTo, Debug, getClassNameShort, getType, IsDebug, present, SetDebug, test, test, toString, Wrap
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface com.rubecula.jquantity.Arithmetical
isInfinite, isInteger, isUnity, signum
 
Methods inherited from interface com.rubecula.util.Auditable
audit, audit, audit
 
Methods inherited from interface com.rubecula.util.Identifiable
getIdentifier
 
Methods inherited from interface com.rubecula.util.Presentable
makePresentableInstance, present
 

Field Detail

serialVersionUID

private static final long serialVersionUID
use serialVersionUID from JDK 1.0.2 for interoperability

See Also:
Constant Field Values
Constructor Detail

BaseNumber

public BaseNumber()
Method Detail

valueOf

public static BaseNumber valueOf(java.lang.String string,
                                 java.lang.String id)
                          throws PresentationException
Factory method to create a BaseNumber from a String string, optionally identified by id. By design this method will always return an object of the same class as that overriding the method. The acceptable formats of string vary from class to class.

Parameters:
string - String representation of the object to be constructed.
id - Identifier (may be null).
Returns:
nothing - this base implementation always throws an exception - in other words this method MUST be overridden by extenders of BaseNumber.
Throws:
PresentationException - if this method not overridden.
See Also:
PresentationException.PresentationException(String)

valueOf

public static BaseNumber valueOf(java.lang.String string)
                          throws PresentationException
Factory method to create a BaseNumber from a String string, without an explicit identifier (the string itself is used as the id). By design this method will always return an object of the same class as that overriding the method. The acceptable formats of string vary from class to class.

Parameters:
string - String representation of the object to be constructed.
Returns:
BaseNumber a new instance "widened" from the string string and identified by string.
Throws:
PresentationException - if the string cannot be parsed into the an instance of the defining class.
See Also:
valueOf(String, String)

intValue

public abstract int intValue()
Returns the value of the specified number as an int. This may involve rounding or truncation.

Returns:
the numeric value represented by this object after conversion to type int.

longValue

public abstract long longValue()
Returns the value of the specified number as a long. This may involve rounding or truncation.

Returns:
the numeric value represented by this object after conversion to type long.

floatValue

public abstract float floatValue()
Returns the value of the specified number as a float. This may involve rounding.

Returns:
the numeric value represented by this object after conversion to type float.

doubleValue

public abstract double doubleValue()
Returns the value of the specified number as a double. This may involve rounding.

Returns:
the numeric value represented by this object after conversion to type double.

byteValue

public byte byteValue()
Returns the value of the specified number as a byte. This may involve rounding or truncation.

Returns:
the numeric value represented by this object after conversion to type byte.
Since:
JDK1.1

shortValue

public short shortValue()
Returns the value of the specified number as a short. This may involve rounding or truncation.

Returns:
the numeric value represented by this object after conversion to type short.
Since:
JDK1.1

BestModel

protected static int BestModel(int modelX,
                               int modelY)
Class method to help in combining distribution models. Note: this method would be defined in the Fuzzy interface itself if that was legal.

Parameters:
modelX - the first model to combine.
modelY - the second model to combine.
Returns:
the best model combined from modelX and modelY.
See Also:
Fuzzy.$DistGaussian, Fuzzy.$DistInvalid, Fuzzy.$DistNone, Fuzzy.$DistUniform

minus

protected abstract BaseNumber minus()

Method to yield a copy of this but with the sign changed.
NOTE: it is a requirement of this method that the class of the result is the same as the class of this so that the result can be safely cast to the original class.

Note that this method is parallel but not congruent with the method of the same name defined in the Integral interface.

Returns:
negative version of this.

sum

protected abstract BaseNumber sum(BaseNumber addend)
                           throws InvalidOperandException
Method to add addend to this and return their sum as the result.
NOTE: it is a requirement of this method that the class of the result is the same as the class of this so that the result can be safely cast to the original class.

Parameters:
addend - the number to add to this.
Returns:
the sum of this and addend.
Throws:
InvalidOperandException - if the operands are incompatible.

difference

protected BaseNumber difference(BaseNumber subtrahend)
                         throws InvalidOperandException
Method to yield the difference of this less a BaseNumber (subtrahend).
NOTE: it is a requirement of this method that the class of the result is the same as the class of this so that the result can be safely cast to the original class.

Parameters:
subtrahend - the number to subtract from this.
Returns:
the difference of this less subtrahend.
Throws:
InvalidOperandException - if the operands are incompatible.
See Also:
sum(BaseNumber), minus()

product

protected abstract BaseNumber product(BaseNumber multiplicand)
                               throws InvalidOperandException
Method to multiply multiplicand by this and return their product as the result.
NOTE: it is a requirement of this method that the class of the result is the same as the class of this so that the result can be safely cast to the original class.

Parameters:
multiplicand - the number to multiply by this.
Returns:
the product of this and multiplicand.
Throws:
InvalidOperandException - if the operands are incompatible.

scaled

protected abstract BaseNumber scaled(long factor)
Method to multiply multiplicand by this and return their product as the result.
NOTE: it is a requirement of this method that the class of the result is the same as the class of this so that the result can be safely cast to the original class.

Parameters:
factor - the number to multiply by this.
Returns:
the product of this and multiplicand.

pow

protected abstract BaseNumber pow(int power)
Method to raise this to the power power return the result.
NOTE: it is a requirement of this method that the class of the result is the same as the class of this so that the result can be safely cast to the original class.

Parameters:
power - the exponent for this.
Returns:
the value of this raised to the power power.

getBoundAbs

public double getBoundAbs()
Method to get the absolute bound of this non-fuzzy value;

Returns:
0 double
See Also:
getBound(), doubleValue(), Math.abs(double)

getBound

public double getBound()
Method to get the relative bound of this non-fuzzy value;

Specified by:
getBound in interface Fuzzy
Returns:
0 double

getModel

public int getModel()
Method to get the error distribution model of this non-fuzzy value.

Specified by:
getModel in interface Fuzzy
Returns:
Fuzzy.$DistNone

isExact

public boolean isExact()
Method to return true for this non-fuzzy instance.

Specified by:
isExact in interface Fuzzy
Returns:
true boolean: always

impartFuzziness

public Presentation impartFuzziness(NumberPresentation presentation)
                             throws PresentationException
Method to impart a NumberPresentation presentation with appropriate fuzzy information and ensure that the resulting string value is properly rounded.

Parameters:
presentation - the unfuzzy NumberPresentation.
Returns:
if this exact, then return presentation; else return a copy of presentation, with fuzzy information set appropriately.
Throws:
PresentationException
See Also:
isExact(), getBound(), NumberPresentation.makeFuzzy(int, double)

isUncertain

public boolean isUncertain()
Method to determine if this object is of uncertain value due to fuzziness.

Returns:
false if isExact(); else true if this is zero and bounds are non-zero; else true if the relative bounds are either NaN or greater/equal to 1.
See Also:
isExact(), Arithmetical.signum(), getBound(), Double.isNaN(double)

abs

protected final BaseNumber abs()
Method to yield this or a copy with the sign changed, depending whether the value is positive or negative.

Returns:
this or a negative version of this.
See Also:
Arithmetical.signum(), minus()

isZero

public final boolean isZero()
Method to make standard test against zero.

Returns:
if this is an exact number, then signnum()==0; else isUncertain().
See Also:
isExact(), Arithmetical.signum(), isUncertain()

isEqual

public final boolean isEqual(BaseNumber other)
Method to determine effect equality with another BaseNumber. This is NOT as strict a test of equality as the equals method inherited from Object. Indeed, for fuzzy objects, equality means that the arithmetical difference is not significant, given the error bounds of the two objects.
Important Note for implementers: do not invoke this method from a compareTo(Object) method otherwise an infinite recursion will surely result.

Parameters:
other - the number to compare.
Returns:
true if the arithmetical difference between this and other is not significant.
See Also:
Item.compareTo(Object)

bestModel

public final int bestModel(Fuzzy that)
Method to help in combining distribution models.

Parameters:
that - the other model source.
Returns:
the value of the best model from Fuzzy.$DistUniform, etc.
See Also:
isExact(), Fuzzy.getModel(), BestModel(int, int)

characteristic

int characteristic()
Method to yield the characteristic of a BaseNumber.

Returns:
the power of 10 which, if divided into this, would yield a value between 1 and 9.99999
Throws:
JQuantityException
See Also:
doubleValue(), Characteristic(double)

Characteristic

static int Characteristic(double x)
Package-scope class method to yield the characteristic of value x (regardless of sign).

Parameters:
x - the value whose characteristic is required.
Returns:
if (x from 1.0->9.999) then 0; else if (x from 0.1->0.9999) then -1; and other numbers as appropriate.
Throws:
JQuantityException - if x is zero.
See Also:
Math.abs(double), valueOfLog10(double), Math.round(double), Math.floor(double)

valueOfLog10

static double valueOfLog10(double x)
Package-scope class method to yield the value of log[base 10] (x). The value is calculated as: log(x)/log(10).

Parameters:
x - the value whose log is required.
Returns:
log10(x).
See Also:
Math.log(double)