|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.rubecula.util.Item
com.rubecula.jquantity.BaseNumber
com.rubecula.jquantity.MutableNumber
com.rubecula.jquantity.WholeNumber
This class is a logical extension of the BigInteger class, but is an actual extension of MutableNumber. It also implements the Estimable and Integral interfaces. The objects of this class are exact integers (without size limitation).
Note that I would have preferred to extend BigInteger rather than referencing it in an instance field but that proved to be impossible because BigInteger does not provide any mutating methods (see Arithmetical for comments on why this matters).
Note also that this is not a perfect extension of BigInteger. In particular, some of the names have changed because the convention used in this package is that a verb name implies a mutating operation, whereas a noun name tends to imply an operation that returns a new object. For example, the equivalent of BigInteger.negate() is WholeNumber.minus(). WholeNumber.negate() is a mutating method, returning void.
Nested Class Summary | |
class |
WholeNumber.WholeNumberException
|
Field Summary | |
private double |
_Estimate
|
private java.math.BigInteger |
_Super
This is a reference to the super instance (a BigInteger) which we would have preferred to simply extend. |
static WholeNumber |
$1
$1 is the WholeNumber representation of the exact value 1. |
static WholeNumber |
$10
$10 is the WholeNumber representation of the exact value 10. |
static java.math.BigInteger |
$10B
|
Fields inherited from class com.rubecula.jquantity.MutableNumber |
$__1, $0, $2, $3 |
Fields inherited from class com.rubecula.jquantity.BaseNumber |
|
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 | |
protected |
WholeNumber()
This constructor is here solely for the purpose of working around the fact that interfaces may not define class methods. |
|
WholeNumber(java.math.BigInteger bigInteger)
Constructs an identified WholeNumber from a BigInteger. |
|
WholeNumber(java.math.BigInteger bigInteger,
java.lang.String identifier)
Primary constructor to create a WholeNumber from a BigInteger. |
|
WholeNumber(IntegerPresentation presentation)
Constructs a WholeNumber from a NumberPresentation. |
|
WholeNumber(Integral integer)
Constructs a WholeNumber from a Manifest using a default Identifier. |
|
WholeNumber(Integral integer,
java.lang.String identifier)
Constructs a WholeNumber from a Manifest and an identifier. |
|
WholeNumber(java.lang.String string)
Constructs a WholeNumber from a String. |
Method Summary | |
void |
add(BaseNumber that)
MUTATING Instance method to add another Arithmetical into this object. |
java.lang.String |
audit(java.lang.String label)
Method to return a detailed string from an arithmetical object for debugging purposes. |
java.lang.Object |
clone()
Creates and returns a deep copy of this object. |
int |
compareTo(java.math.BigInteger o)
Compares this WholeNumber with the specified Object o. |
int |
compareTo(Integral o)
Compares this WholeNumber with the specified Object o. |
int |
compareTo(WholeNumber o)
Compares this WholeNumber with the specified Object o. |
void |
divide(Integral divisor)
MUTATING METHOD: Replaces value with (this / divisor). |
java.math.BigInteger[] |
divideAndRemainder(Integral divisor)
Returns an array of two BigIntegers containing (this / divisor) followed by (this % divisor). |
double |
doubleValue()
Converts this WholeNumber to a double. |
boolean |
equals(java.lang.Object x)
Compares this WholeNumber with the specified Object for equality. |
protected void |
factor(Integral factor)
MUTATING METHOD: Replaces value with (this / divisor). |
float |
floatValue()
Converts this WholeNumber to a float. |
java.math.BigInteger |
gcd(Integral val)
Returns a BigInteger whose value is the greatest common divisor of abs(this) and abs(val). |
double |
getEstimate()
Method to get the current estimate of an arithmetical value. |
protected java.math.BigInteger |
getValue()
Protected method to get the value as a BigInteger. |
int |
hashCode()
Returns the hash code for this WholeNumber. |
int |
intValue()
Converts this WholeNumber to an int. |
boolean |
isInfinite()
Method to determine if an arithmetical object is infinite. |
boolean |
isInteger()
Method determines if it is safe and appropriate to convert this arithmetical object into a long. |
boolean |
isPowerOfTen()
Method to determine if this is a power of ten and ten only. |
boolean |
isUnity()
Method to determine if this arithmetical object has the value of 1. |
(package private) int |
logTen()
Method to determine how many times ten divides into this. |
long |
longValue()
Converts this WholeNumber to a long. |
static void |
main(java.lang.String[] args)
Main method for testing this class. |
Presentable |
makePresentableInstance(Presentation presentation)
Instance method to convert a Presentation into a Presentable. |
WholeNumber |
max(WholeNumber val)
Returns the maximum of this WholeNumber and val. |
WholeNumber |
min(WholeNumber val)
Returns the minimum of this WholeNumber and val. |
BaseNumber |
minus()
Method to yield a copy of this but with the sign changed. |
void |
multiply(BaseNumber that)
MUTATING Instance method to multiply another BaseNumber into this object. |
void |
negate()
MUTATING Instance method to negate this. |
protected BaseNumber |
pow(int power)
Method to raise this to the power power return the result. |
Presentation |
present(AttrMap attributes)
Convert this Presentable object into a Presentation, according to the set of attributes provided. |
private Presentation |
present(boolean exponential)
Method to convert this WholeNumber into an equivalent Presentation, using radix 10 and exponential form. |
private Presentation |
present(int radix)
Method to convert this WholeNumber into an equivalent Presentation using non-exponential form. |
private Presentation |
present(int radix,
boolean exponential)
Method to convert this WholeNumber into an equivalent Presentation. |
WholeNumber |
quotient(Integral that)
Method to divide a WholeNumber by an Integral number. |
void |
raiseToPower(int power)
MUTATING Instance method to raise this to power power. |
void |
scale(long factor)
MUTATING Instance method to multiply this by a scale factor. |
protected BaseNumber |
scaled(long multiplicand)
Method to multiply multiplicand by this and return their product as the result. |
private void |
setValue(java.math.BigInteger value)
MUTATING method to set a new value. |
int |
signum()
Method to find the sign of the magnitude of this arithmetical object. |
void |
subtract(WholeNumber subtrahend)
MUTATING METHOD: Replaces value with (this - val). |
static WholeNumber |
TenToPower(int index)
Factory method to create a WholeNumber equal to ten to the power of index. |
java.math.BigInteger |
toBigInteger()
Convert this into a BigInteger. |
java.lang.String |
toString()
Returns the decimal String representation of this WholeNumber. |
java.lang.String |
toString(int radix)
Returns the String representation of this WholeNumber in the given radix. |
void |
updateEstimate()
MUTATING method to update the current estimate. |
static WholeNumber |
valueOf(BaseNumber number)
Factory method to create a WholeNumber from an instance of BaseNumber, which is also Integral. |
static WholeNumber |
valueOf(Integral number)
Factory method to create a WholeNumber from an instance of Integral, such as a Manifest, which is not a BaseNumber. |
static WholeNumber |
valueOf(java.lang.String string,
int radix)
Factory method to create a WholeNumber from a string, and being interpreted in base radix. |
(package private) static WholeNumber |
valueOfExponent(int exponent)
Factory method to create a Rational which is equal to 10^exponent. |
(package private) static WholeNumber |
valueOfExponent(int exponent,
int radix)
Factory method to create a WholeNumber which is equal to radix^exponent. |
(package private) static WholeNumber |
valueOfExponent(java.lang.String exponentStr,
int radix)
Factory method to create a Rational which is equal to the multiplier represented by an exponent string. |
Methods inherited from class com.rubecula.jquantity.MutableNumber |
assertMutable, defaultIdentifier, factored, forcePositive, getIdentifier, getIdentifier, isLong, isMutable, product, setConstant, setIdentifier, setIdentifierPost, setIdentifierPre, setIdentifierPrePost, sum, test, toIntegral |
Methods inherited from class com.rubecula.jquantity.BaseNumber |
abs, bestModel, BestModel, byteValue, characteristic, Characteristic, difference, getBound, getBoundAbs, getModel, impartFuzziness, isEqual, isExact, isUncertain, isZero, shortValue, valueOf, valueOf, valueOfLog10 |
Methods inherited from class com.rubecula.util.Item |
audit, audit, compareTo, Debug, getClassNameShort, getType, IsDebug, present, SetDebug, test, test, Wrap |
Methods inherited from class java.lang.Object |
finalize, getClass, notify, notifyAll, wait, wait, wait |
Methods inherited from interface com.rubecula.jquantity.Integral |
isExact, isZero |
Methods inherited from interface com.rubecula.util.Auditable |
audit, audit |
Methods inherited from interface com.rubecula.util.Identifiable |
getIdentifier |
Field Detail |
public static final WholeNumber $1
public static final java.math.BigInteger $10B
public static final WholeNumber $10
MutableNumber.$10
for use in
TenToPower(int)
.
private double _Estimate
private java.math.BigInteger _Super
Constructor Detail |
public WholeNumber(java.math.BigInteger bigInteger, java.lang.String identifier) throws WholeNumber.WholeNumberException
bigInteger
- the value to widen (must be non-null).identifier
- the identifier for this WholeNumber (may be null).
WholeNumber.WholeNumberException
- WholeNumber(x): null argument.public WholeNumber(java.math.BigInteger bigInteger)
bigInteger
- the value of copy.public WholeNumber(IntegerPresentation presentation)
presentation
- the string form to use.public WholeNumber(java.lang.String string) throws PresentationException
string
- a String in the form required by IntegerPresentation.
PresentationException
- when string cannot be parsed.public WholeNumber(Integral integer, java.lang.String identifier)
integer
- the Manifest (pure constant number) from which this WholeNumber
will be widened.
Must be non-null otherwise a NullPointerException will be thrown.identifier
- the identifier for this WholeNumber (may be null).public WholeNumber(Integral integer)
integer
- the Manifest (pure constant number) from which this WholeNumber
will be widened.
Must be non-null otherwise a NullPointerException will be thrown.protected WholeNumber()
Method Detail |
public static WholeNumber valueOf(BaseNumber number)
valueOf(Integral)
.
number
- the BaseNumber to be widened into WholeNumber and which
must be an Integral object.
JQuantityException
- number is not Integral.public static WholeNumber valueOf(Integral number)
WholeNumber(BigInteger,String)
with the two arguments given as:
Integral.toBigInteger()
and Identifiable.getIdentifier()
both
applied to number.
number
- the value to be widened into WholeNumber.
public static WholeNumber valueOf(java.lang.String string, int radix)
string
- the value to be parsed into WholeNumber.radix
- the radix with which the string is to be interpreted.
WholeNumber(BigInteger)
,
BigInteger.BigInteger(java.lang.String, int)
public static WholeNumber TenToPower(int index)
index
- the exponent of ten.
$10
to the power of index using method pow(int)
static WholeNumber valueOfExponent(int exponent, int radix)
exponent
- the power to which radix is to be raised.radix
- the base of the number system.
JQuantityException
- if exponent is less than zero.BigInteger.valueOf(long)
,
BigInteger.pow(int)
,
Math.abs(int)
,
WholeNumber(BigInteger)
static WholeNumber valueOfExponent(int exponent)
exponent
- the power to which radix is to be raised.
TenToPower(int)
, else
valueOfExponent(int,int)
with the second argument of 10.static WholeNumber valueOfExponent(java.lang.String exponentStr, int radix)
exponentStr
- the power to which radix is to be raised,
as a string.radix
- the base of the number system in which exponentStr is to
be interpreted and also the number to be raised by the appropriate power.
$1
else get the exponent as an int using Integer.parseInt(java.lang.String, int)
and then pass it, with the radix, into valueOfExponent(int,int)
.
JQuantityException
- WholeNumber.exponent: invalid exponent string:
exponentStr for radix: radix.public static void main(java.lang.String[] args)
args
- the command line options.public int intValue()
intValue
in interface Integral
intValue
in class BaseNumber
public long longValue()
longValue
in interface Integral
longValue
in class BaseNumber
public float floatValue()
floatValue
in class BaseNumber
public double doubleValue()
doubleValue
in class BaseNumber
public java.lang.Object clone()
clone
in class MutableNumber
WholeNumber.WholeNumberException
- : WholeNumber not Cloneable (should be impossible)Cloneable
public boolean isInfinite()
isInfinite
in interface Arithmetical
public boolean isInteger()
isInteger
in interface Arithmetical
public boolean isUnity()
isUnity
in interface Arithmetical
public int signum()
signum
in interface Arithmetical
public void raiseToPower(int power)
raiseToPower
in interface Arithmutable
power
- the number of times this should be multiplied by itself.BigInteger.pow(int)
public void scale(long factor)
scale
in interface Arithmutable
factor
- the scale factor.public void negate()
negate
in interface Arithmutable
public void add(BaseNumber that)
add
in interface Arithmutable
that
- the addend.public void multiply(BaseNumber that) throws InvalidOperandException
multiply
in interface Arithmutable
that
- the multiplicand.
InvalidOperandException
public void updateEstimate()
updateEstimate
in interface Estimable
public double getEstimate()
getEstimate
in interface Estimable
public java.math.BigInteger toBigInteger()
toBigInteger
in interface Integral
public java.lang.String audit(java.lang.String label)
audit
in interface Auditable
label
- the label to associate with the data
public int compareTo(java.math.BigInteger o)
getValue()
.BigInteger.compareTo(Object)
;
else if o is Integral, the result is equal to
Item.compareTo(Object)
using Integral.toBigInteger()
as the
argument;
else if o is Integer or Long, the result is equal to
Item.compareTo(Object)
using Manifest.Manifest(long)
with Number.longValue()
as the
argument;
otherwise a WholeNumberException is thrown.
o
- BigInteger to which this WholeNumber is to be compared.
WholeNumber.WholeNumberException
- Cannot compare with object of class: class.Comparable
public int compareTo(Integral o)
getValue()
.BigInteger.compareTo(Object)
;
else if o is Integral, the result is equal to
Item.compareTo(Object)
using Integral.toBigInteger()
as the
argument;
else if o is Integer or Long, the result is equal to
Item.compareTo(Object)
using Manifest.Manifest(long)
with Number.longValue()
as the
argument;
otherwise a WholeNumberException is thrown.
o
- Integral to which this WholeNumber is to be compared.
WholeNumber.WholeNumberException
- Cannot compare with object of class: class.Comparable
public int compareTo(WholeNumber o)
getValue()
.BigInteger.compareTo(Object)
;
else if o is Integral, the result is equal to
Item.compareTo(Object)
using Integral.toBigInteger()
as the
argument;
else if o is Integer or Long, the result is equal to
Item.compareTo(Object)
using Manifest.Manifest(long)
with Number.longValue()
as the
argument;
otherwise a WholeNumberException is thrown.
o
- Integral to which this WholeNumber is to be compared.
WholeNumber.WholeNumberException
- Cannot compare with object of class: class.Comparable
public Presentable makePresentableInstance(Presentation presentation) throws PresentationException
makePresentableInstance
in interface Presentable
presentation
- a presentation of a number which you want to convert into a number.
PresentationException
- if presentation cannot be parsed appropriately.public Presentation present(AttrMap attributes) throws PresentationException
present
in interface Presentable
attributes
- the map of attributes required for the presentation.
PresentationException
- when string cannot be parsed.public BaseNumber minus()
minus
in interface Integral
minus
in class BaseNumber
protected BaseNumber scaled(long multiplicand)
scaled
in class BaseNumber
multiplicand
- the number to multiply this by.
protected BaseNumber pow(int power)
pow
in class BaseNumber
power
- the exponent for this.
protected void factor(Integral factor)
factor
in class MutableNumber
factor
- value by which this WholeNumber is to be divided.
WholeNumberException:
- WholeNumber.factor:
factor is not a factor
WholeNumberException:
- WholeNumber.factor:
factor should be positivepublic WholeNumber quotient(Integral that)
that
- WholeNumber (or WholeNumber): the divisor (denominator).
public void subtract(WholeNumber subtrahend)
subtrahend
- value to be subtracted from this WholeNumber.public void divide(Integral divisor)
divisor
- value by which this WholeNumber is to be divided.
java.lang.ArithmeticException
- divisor==0public java.math.BigInteger[] divideAndRemainder(Integral divisor)
divisor
- value by which this WholeNumber is to be divided, and the
remainder computed.
java.lang.ArithmeticException
- val==0public java.math.BigInteger gcd(Integral val)
val
- value with with the GCD is to be computed.
public boolean equals(java.lang.Object x)
x
- Object to which this WholeNumber is to be compared.
public WholeNumber min(WholeNumber val)
val
- value with with the minimum is to be computed.
public WholeNumber max(WholeNumber val)
val
- value with with the maximum is to be computed.
public int hashCode()
public java.lang.String toString(int radix)
radix
- radix of the String representation.
Integer.toString()
,
Character.forDigit(int, int)
,
getValue()
,
toString(int)
public java.lang.String toString()
toString
in class Item
Character.forDigit(int, int)
,
WholeNumber(java.lang.String)
protected final java.math.BigInteger getValue()
setValue(BigInteger)
.
final int logTen()
logTen()
where quotient is a WholeNumber
whose value is this/$10
.public boolean isPowerOfTen()
isPowerOfTen
in interface Integral
private Presentation present(int radix, boolean exponential) throws PresentationException
radix
- the radix in which the presentation will be made.exponential
- true if the number is to be presented in the form xEn.
PresentationException
- when string cannot be parsed.private Presentation present(int radix) throws PresentationException
radix
- the radix in which the presentation will be made.
PresentationException
- when string cannot be parsed.private Presentation present(boolean exponential) throws PresentationException
exponential
- true if the number is to be presented in the form xEn.
PresentationException
- when string cannot be parsed.private final void setValue(java.math.BigInteger value)
value
- the BigInteger which is to be the value of this WholeNumber
from now on. In addition, updateEstimate()
is
called.
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |