com.rubecula.jquantity
Class MutableNumber

java.lang.Object
  |
  +--java.lang.Number
        |
        +--com.rubecula.jquantity.BaseNumber
              |
              +--com.rubecula.jquantity.MutableNumber
All Implemented Interfaces:
Arithmetical, Arithmutable, Auditable, java.lang.Cloneable, java.lang.Comparable, Fuzzy, Identifiable, Mutable, Presentable, java.io.Serializable
Direct Known Subclasses:
Rational, WholeNumber

public abstract class MutableNumber
extends BaseNumber
implements Identifiable, Arithmutable

Abstract class from which to derive other concrete classes of mutable numbers.

Objects of this class can be given identifiers, are changeable under standard arithmetic operators, and can be cloned and presented.

Version:
$Revision: 1.3 $
Author:
Robin Hillyard
See Also:
Serialized Form

Field Summary
static BaseNumber $__1
          The constant value: -1
static BaseNumber $0
          The constant value: 0
static BaseNumber $1
          The constant value: 1
static BaseNumber $10
          The constant value: 10
static BaseNumber $2
          The constant value: 2
static BaseNumber $3
          The constant value: 3
 
Fields inherited from interface com.rubecula.jquantity.Fuzzy
$DistGaussian, $DistInvalid, $DistNone, $DistUniform, $Names
 
Constructor Summary
MutableNumber()
           
 
Method Summary
 java.lang.Object clone()
          Creates and returns a copy of this object.
protected abstract  void factor(Integral divisor)
          MUTATING METHOD: Replaces value with (this / divisor).
protected  BaseNumber factored(Integral factor)
          Method to divide this exactly by factor and return the quotient as the result.
 void forcePositive()
          MUTATING Instance method to force the value to be positive, i.e.
 java.lang.String getIdentifier()
          Method to get the specific identifier for an object.
 boolean isLong()
          Method to determine if a value is integral and within range of long.
protected  BaseNumber product(BaseNumber multiplicand)
          Method to multiply multiplicand to this and return their product as the result.
 void setIdentifier(java.lang.String identifier)
          Method to set a specific identifier to an object.
protected  void setIdentifierPost(java.lang.String suffix)
          Method to form a new identifier for an object by appending string and wrapping.
protected  void setIdentifierPre(java.lang.String prefix)
          Method to form a new identifier for an object by prepending string and wrapping.
protected  BaseNumber sum(BaseNumber addend)
          Method to add addend to this and return their sum as the result.
protected  void test(java.io.PrintStream out, long check, java.lang.String label)
          Convenience method for auditing/checking a BaseNumber.
 Integral toIntegral()
          Method to present a MutableNumber as an Integral number.
 
Methods inherited from class com.rubecula.jquantity.BaseNumber
abs, audit, audit, bestModel, BestModel, Characteristic, compareTo, difference, getBound, getBoundAbs, getModel, impartFuzziness, isEqual, isExact, isUncertain, isZero, minus, pow, present, scaled, test, test, toString, valueOfLog10, Wrap
 
Methods inherited from class java.lang.Number
byteValue, doubleValue, floatValue, intValue, longValue, shortValue
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface com.rubecula.jquantity.Arithmutable
add, multiply, negate, raiseToPower, scale
 
Methods inherited from interface com.rubecula.util.Presentable
makePresentableInstance, present
 
Methods inherited from interface com.rubecula.jquantity.Arithmetical
isInfinite, isInteger, isUnity, signum
 
Methods inherited from interface com.rubecula.util.Auditable
audit
 

Field Detail

$0

public static final BaseNumber $0
The constant value: 0


$1

public static final BaseNumber $1
The constant value: 1


$2

public static final BaseNumber $2
The constant value: 2


$3

public static final BaseNumber $3
The constant value: 3


$10

public static final BaseNumber $10
The constant value: 10


$__1

public static final BaseNumber $__1
The constant value: -1

Constructor Detail

MutableNumber

public MutableNumber()
Method Detail

clone

public java.lang.Object clone()
Creates and returns a copy of this object.

Overrides:
clone in class java.lang.Object
Returns:
a clone of this instance.
Throws:
java.lang.Exception - : not Cloneable (should be impossible)
See Also:
Cloneable

sum

protected 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.
IF this.sum(addend) cannot be evaluated; AND IF addend is an instance of this (and therefore also of MutableNumber); THEN we can try to evaluate addend.sum(this).
The actual implementation of the sum is performed using method #sumNoRecurse(BaseNumber), which in turn utilizes Arithmutable.add(BaseNumber).

Specified by:
sum in class BaseNumber
Parameters:
addend - the number to add to this.
Returns:
the sum of this and addend.
Throws:
InvalidOperandException - operands are not compatible for the sum operation.

product

protected BaseNumber product(BaseNumber multiplicand)
                      throws InvalidOperandException
Method to multiply multiplicand to 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.
IF this.product(multiplicand) cannot be evaluated; AND IF multiplicand is an instance of this (and therefore also of MutableNumber); THEN we can try to evaluate multiplicand.product(this).
The actual implementation of the sum is performed using method #productNoRecurse(BaseNumber), which in turn utilizes Arithmutable.multiply(BaseNumber).

Specified by:
product in class BaseNumber
Parameters:
multiplicand - the number to add to this.
Returns:
the product of this and multiplicand.
Throws:
InvalidOperandException - operands are not compatible for the product operation.

isLong

public boolean isLong()
Method to determine if a value is integral and within range of long.

Returns:
true if isInteger() and if abs(value) < Long.MAX_VALUE.

forcePositive

public void forcePositive()
MUTATING Instance method to force the value to be positive, i.e. this = abs (this).

Specified by:
forcePositive in interface Arithmutable

toIntegral

public Integral toIntegral()
Method to present a MutableNumber as an Integral number. If this is already of class Integral then just return this, else created a Manifest from the long value.

Returns:
an instance of Integral with value equivalent to this.

setIdentifier

public void setIdentifier(java.lang.String identifier)
Method to set a specific identifier to an object.

Specified by:
setIdentifier in interface Identifiable
Parameters:
identifier - the identifier to associate with the object

getIdentifier

public java.lang.String getIdentifier()
Method to get the specific identifier for an object. If no identifier has been set, one will be generated.

Specified by:
getIdentifier in interface Auditable
Returns:
the defined identifier, else "<" value ">"

factor

protected abstract void factor(Integral divisor)
MUTATING METHOD: Replaces value with (this / divisor).
This method allows for division by a non-Divisible divisor. In some implementations, an exception will be thrown if the quotient cannot be expressed (for example it is non-integral for a WholeNumber).

Parameters:
divisor - value by which this WholeNumber is to be divided.

factored

protected BaseNumber factored(Integral factor)
Method to divide this exactly by factor and return the quotient 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 divide into this.
Returns:
the exact quotient of this divided by factor. If factor doesn't divide exactly into this, an exception will be thrown.
See Also:
factor(Integral).

setIdentifierPost

protected void setIdentifierPost(java.lang.String suffix)
Method to form a new identifier for an object by appending string and wrapping.

Parameters:
suffix - the identifier to append to object identifier

setIdentifierPre

protected void setIdentifierPre(java.lang.String prefix)
Method to form a new identifier for an object by prepending string and wrapping.

Parameters:
prefix - the identifier to prepend to object identifier

test

protected void test(java.io.PrintStream out,
                    long check,
                    java.lang.String label)
Convenience method for auditing/checking a BaseNumber.

Parameters:
out - the output stream.
check - the number to compare against.
label - the label to print with the audit trals.