com.rubecula.jquantity
Class MutableNumber

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

public abstract class MutableNumber
extends BaseNumber
implements Nameable, 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.
Although objects of this class are constructed mutable, individual instances can be made immutable via the method setConstant(). This allows "constant" values to be created for the class which will be protected against mutation.

Version:
$Revision: 1.12 $
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
protected  void assertMutable()
          This method tests whether this particular instance is mutable, i.e.
 java.lang.Object clone()
          Creates and returns a copy of this object.
 java.lang.String defaultIdentifier()
           
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.
 java.lang.String getIdentifier(boolean acceptDefault)
          Method to get the specific identifier for an object.
 boolean isLong()
          Method to determine if a value is integral and within range of long.
 boolean isMutable()
          This method indicates whether this particular instance is mutable, i.e.
protected  BaseNumber product(BaseNumber multiplicand)
           Method to multiply multiplicand to this and return their product as the result.
protected  MutableNumber setConstant()
          This method sets this instance to be a constant, i.e.
 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  void setIdentifierPrePost(java.lang.String prefix, java.lang.String suffix)
          Method to form a new identifier for an object by prepending one string, appending another then 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 MutableNumber against a long.
 Integral toIntegral()
          Method to present a MutableNumber as an Integral number.
 
Methods inherited from class com.rubecula.jquantity.BaseNumber
abs, bestModel, BestModel, byteValue, characteristic, Characteristic, difference, doubleValue, floatValue, getBound, getBoundAbs, getModel, impartFuzziness, intValue, isEqual, isExact, isUncertain, isZero, longValue, minus, pow, scaled, shortValue, valueOf, valueOf, valueOfLog10, Wrap
 
Methods inherited from class com.rubecula.jquantity.Item
audit, audit, compareTo, Debug, getClassNameShort, getType, IsDebug, present, SetDebug, test, test, toString
 
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.jquantity.Arithmetical
isInfinite, isInteger, isUnity, signum
 
Methods inherited from interface com.rubecula.util.Auditable
audit, audit, audit
 
Methods inherited from interface com.rubecula.util.Presentable
makePresentableInstance, present
 

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 Nameable
Parameters:
identifier - the identifier to associate with the object

getIdentifier

public java.lang.String getIdentifier()
Method to get the specific identifier for an object.

Specified by:
getIdentifier in interface Identifiable
Returns:
the defined identifier, else null

getIdentifier

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

Parameters:
acceptDefault - true if caller is willing to accept a generated identifier.
Returns:
the defined identifier, else "<" value ">"

defaultIdentifier

public java.lang.String defaultIdentifier()

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

setIdentifierPrePost

protected void setIdentifierPrePost(java.lang.String prefix,
                                    java.lang.String suffix)
Method to form a new identifier for an object by prepending one string, appending another then wrapping.

Parameters:
prefix - the identifier to prepend to object identifier
suffix - the identifier to append to object identifier

test

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

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

setConstant

protected MutableNumber setConstant()
This method sets this instance to be a constant, i.e. immutable. Future calls to the isMutable() method will yield false.

Returns:
as a convenience this method returns itself.

isMutable

public boolean isMutable()
This method indicates whether this particular instance is mutable, i.e. it has not been designated a constant.

Specified by:
isMutable in interface Mutable
Returns:
true if this instance can be mutated, otherwise false (it is constant).

assertMutable

protected void assertMutable()
This method tests whether this particular instance is mutable, i.e. it has not been designated a constant. If it is not mutable, an RuntimeException is thrown.