ch.javasoft.math
Class BigDecimalOperations

java.lang.Object
  extended by ch.javasoft.math.AbstractNumberOps<BigDecimal>
      extended by ch.javasoft.math.BigDecimalOperations
All Implemented Interfaces:
NumberOperations<BigDecimal>, Comparator<BigDecimal>

public class BigDecimalOperations
extends AbstractNumberOps<BigDecimal>

Number operations for BigDecimal numbers. The instantiation of a BigDecimalOperations requires a MathContext which defines rounding.


Constructor Summary
BigDecimalOperations(int setPrecision)
          Constructs a new BigDecimalOperations with the specified precision and the HALF_UP rounding mode.
BigDecimalOperations(MathContext mathContext)
          Constructor for BigDecimalOperations with specified math context defining rounding
BigDecimalOperations(String contextVal)
          Constructs a new BigDecimalOperations with MathContext from a string.
 
Method Summary
 BigDecimal abs(BigDecimal number)
          Returns the absolute value of the given number, i.e.
 BigDecimal add(BigDecimal numA, BigDecimal numB)
          Returns the sum of the two values, i.e.
 int byteLength()
          Returns the byte array length if NumberOperations.toByteArray(Number) is used and if this size is fixed, and -1 if the size may vary depending on the number instance
 int compare(BigDecimal o1, BigDecimal o2)
           
 BigDecimal divide(BigDecimal numA, BigDecimal numB)
          Returns the ratio of the two values, i.e.
 BigDecimal fromByteArray(byte[] bytes)
          Converts the byte array containing binary data into a new number instance.
 MathContext getMathContext()
          Returns the math context defined when instantiating this BigDecimalOperations
 BigDecimal invert(BigDecimal number)
          Returns the inverted value, i.e.
 boolean isOne(BigDecimal number)
          Returns true if this value is numerically equal to one, i.e.
 BigDecimal multiply(BigDecimal numA, BigDecimal numB)
          Returns the product of the two values, i.e.
 BigDecimal negate(BigDecimal number)
          Returns the negated value, i.e.
 BigDecimal[] newArray(int size)
          Create a new array of the underlying number type
 BigDecimal[][] newArray(int rows, int cols)
          Create a new 2 dimensional array of the underlying number type
 Class<BigDecimal> numberClass()
          The class representing numbers of this number operations instance
 BigDecimal one()
          Returns the value representing one
 BigDecimal pow(BigDecimal numA, BigDecimal numB)
          Returns the powered value, i.e.
 BigDecimal readFrom(DataInput in)
          Reads binary data from the given data input.
 BigDecimal reduce(BigDecimal number)
          Returns the rounded number according to the current math context, that is, BigDecimal.round(MathContext) is called
 BigDecimal[] reduceVector(boolean cloneOnChange, BigDecimal... vector)
          Returns the unchanged vector
 int signum(BigDecimal number)
          Returns signum of the value, i.e.
 BigDecimal subtract(BigDecimal numA, BigDecimal numB)
          Returns the difference of the two values, i.e.
 byte[] toByteArray(BigDecimal number)
          Converts this number to binary data and returns it as a byte array.
 BigDecimal valueOf(double value)
          Create an instance of the underlying number type based on the submitted double value
 BigDecimal valueOf(long value)
          Create an instance of the underlying number type based on the submitted long integer value
 BigDecimal valueOf(Number number)
          Create an instance of the underlying number type based on the submitted number
 BigDecimal valueOf(String s)
          Create an instance of the underlying number type based on the given string representation of the number
 void writeTo(BigDecimal number, DataOutput out)
          Converts this number to binary data and writes it to the data output.
 BigDecimal zero()
          Returns the value representing zero
 
Methods inherited from class ch.javasoft.math.AbstractNumberOps
isNegative, isNonNegative, isNonPositive, isNonZero, isPositive, isZero, max, max, min, min
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface java.util.Comparator
equals
 

Constructor Detail

BigDecimalOperations

public BigDecimalOperations(MathContext mathContext)
Constructor for BigDecimalOperations with specified math context defining rounding


BigDecimalOperations

public BigDecimalOperations(int setPrecision)
Constructs a new BigDecimalOperations with the specified precision and the HALF_UP rounding mode.

Parameters:
setPrecision - The non-negative int precision setting.
Throws:
IllegalArgumentException - if the setPrecision parameter is less than zero.

BigDecimalOperations

public BigDecimalOperations(String contextVal)
Constructs a new BigDecimalOperations with MathContext from a string. The string must be in the same format as that produced by the MathContext.toString() method.

An IllegalArgumentException is thrown if the precision section of the string is out of range (< 0) or the string is not in the format created by the Object.toString() method.

Parameters:
contextVal - The string to be parsed
Throws:
IllegalArgumentException - if the precision section is out of range or of incorrect format
NullPointerException - if the argument is null
Method Detail

getMathContext

public MathContext getMathContext()
Returns the math context defined when instantiating this BigDecimalOperations


numberClass

public Class<BigDecimal> numberClass()
Description copied from interface: NumberOperations
The class representing numbers of this number operations instance


newArray

public BigDecimal[] newArray(int size)
Description copied from interface: NumberOperations
Create a new array of the underlying number type


newArray

public BigDecimal[][] newArray(int rows,
                               int cols)
Description copied from interface: NumberOperations
Create a new 2 dimensional array of the underlying number type


valueOf

public BigDecimal valueOf(String s)
Description copied from interface: NumberOperations
Create an instance of the underlying number type based on the given string representation of the number


valueOf

public BigDecimal valueOf(Number number)
Description copied from interface: NumberOperations
Create an instance of the underlying number type based on the submitted number


valueOf

public BigDecimal valueOf(long value)
Description copied from interface: NumberOperations
Create an instance of the underlying number type based on the submitted long integer value


valueOf

public BigDecimal valueOf(double value)
Description copied from interface: NumberOperations
Create an instance of the underlying number type based on the submitted double value


abs

public BigDecimal abs(BigDecimal number)
Description copied from interface: NumberOperations
Returns the absolute value of the given number, i.e. a non-negative value abs(number)

Specified by:
abs in interface NumberOperations<BigDecimal>
Overrides:
abs in class AbstractNumberOps<BigDecimal>

add

public BigDecimal add(BigDecimal numA,
                      BigDecimal numB)
Description copied from interface: NumberOperations
Returns the sum of the two values, i.e. numA + numB


divide

public BigDecimal divide(BigDecimal numA,
                         BigDecimal numB)
Description copied from interface: NumberOperations
Returns the ratio of the two values, i.e. numA / numB.

For integer types, an integer division is performed, ignoring possible remainders


invert

public BigDecimal invert(BigDecimal number)
Description copied from interface: NumberOperations
Returns the inverted value, i.e. 1 / number

Specified by:
invert in interface NumberOperations<BigDecimal>
Overrides:
invert in class AbstractNumberOps<BigDecimal>

isOne

public boolean isOne(BigDecimal number)
Description copied from interface: NumberOperations
Returns true if this value is numerically equal to one, i.e. number == 1

Specified by:
isOne in interface NumberOperations<BigDecimal>
Overrides:
isOne in class AbstractNumberOps<BigDecimal>

multiply

public BigDecimal multiply(BigDecimal numA,
                           BigDecimal numB)
Description copied from interface: NumberOperations
Returns the product of the two values, i.e. numA * numB


negate

public BigDecimal negate(BigDecimal number)
Description copied from interface: NumberOperations
Returns the negated value, i.e. -number


one

public BigDecimal one()
Description copied from interface: NumberOperations
Returns the value representing one


subtract

public BigDecimal subtract(BigDecimal numA,
                           BigDecimal numB)
Description copied from interface: NumberOperations
Returns the difference of the two values, i.e. numA - numB


zero

public BigDecimal zero()
Description copied from interface: NumberOperations
Returns the value representing zero


compare

public int compare(BigDecimal o1,
                   BigDecimal o2)

reduce

public BigDecimal reduce(BigDecimal number)
Returns the rounded number according to the current math context, that is, BigDecimal.round(MathContext) is called


reduceVector

public BigDecimal[] reduceVector(boolean cloneOnChange,
                                 BigDecimal... vector)
Returns the unchanged vector

Parameters:
cloneOnChange - if a reduction can be made, the array is cloned and the original array is not changed. If no reduction can be made, or if cloneOnChange is false, the original array will be returned, unmodified in the former case, and with reduced values in the latter case.
vector - the vector to reduce

signum

public int signum(BigDecimal number)
Description copied from interface: NumberOperations
Returns signum of the value, i.e. -1, 0, 1 for negative, zero or positive values


pow

public BigDecimal pow(BigDecimal numA,
                      BigDecimal numB)
Description copied from interface: NumberOperations
Returns the powered value, i.e. numAnumB. Negative exponents are allowed, also for integer types, without causing an exception. E.g. 3^-1 yields 0.


toByteArray

public byte[] toByteArray(BigDecimal number)
Description copied from interface: NumberOperations
Converts this number to binary data and returns it as a byte array. Back-conversion is possible using the NumberOperations.fromByteArray(byte[]) method.


writeTo

public void writeTo(BigDecimal number,
                    DataOutput out)
             throws IOException
Description copied from interface: NumberOperations
Converts this number to binary data and writes it to the data output. The counterpart of this method reading binary data is NumberOperations.readFrom(DataInput).

Throws:
IOException

fromByteArray

public BigDecimal fromByteArray(byte[] bytes)
Converts the byte array containing binary data into a new number instance. Forward-conversion is possible using the NumberOperations.toByteArray(Number) method.

Returns the value as stored, without applying rounding according to the math context.


readFrom

public BigDecimal readFrom(DataInput in)
                    throws IOException
Reads binary data from the given data input. Binary data is converted and a new number instance is returned. The counterpart of this method writing binary data is NumberOperations.writeTo(Number, DataOutput)

Returns the value as stored, without applying rounding according to the math context.

Throws:
IOException

byteLength

public int byteLength()
Description copied from interface: NumberOperations
Returns the byte array length if NumberOperations.toByteArray(Number) is used and if this size is fixed, and -1 if the size may vary depending on the number instance