ch.javasoft.math
Interface NumberOperations<N extends Number>

All Superinterfaces:
Comparator<N>
All Known Implementing Classes:
AbstractNumberOps, BigDecimalOperations, BigFractionOperations, BigIntegerOperations, DoubleOperations, IntegerOperations, LongOperations

public interface NumberOperations<N extends Number>
extends Comparator<N>

Number operations is a collection of arithmetic and other operations usually supported by different number types.


Method Summary
 N abs(N number)
          Returns the absolute value of the given number, i.e.
 N add(N numA, N numB)
          Returns the sum of the two values, i.e.
 int byteLength()
          Returns the byte array length if toByteArray(Number) is used and if this size is fixed, and -1 if the size may vary depending on the number instance
 N divide(N numA, N numB)
          Returns the ratio of the two values, i.e.
 N fromByteArray(byte[] bytes)
          Converts the byte array containing binary data into a new number instance.
 N invert(N number)
          Returns the inverted value, i.e.
 boolean isNegative(N number)
          Returns true if this value negative, i.e.
 boolean isNonNegative(N number)
          Returns true if this value not negative, i.e.
 boolean isNonPositive(N number)
          Returns true if this value not positive, i.e.
 boolean isNonZero(N number)
          Returns true if this value is numerically unequal to zero, i.e.
 boolean isOne(N number)
          Returns true if this value is numerically equal to one, i.e.
 boolean isPositive(N number)
          Returns true if this value positive, i.e.
 boolean isZero(N number)
          Returns true if this value is numerically equal to zero, i.e.
 N max(N... vals)
          Returns the largest value of the given numbers, i.e.
 N max(N valA, N valB)
          Returns the larger value of the two numbers, i.e.
 N min(N... vals)
          Returns the smallest value of the given numbers, i.e.
 N min(N valA, N valB)
          Returns the smaller value of the two numbers, i.e.
 N multiply(N numA, N numB)
          Returns the product of the two values, i.e.
 N negate(N number)
          Returns the negated value, i.e.
 N[] newArray(int size)
          Create a new array of the underlying number type
 N[][] newArray(int rows, int cols)
          Create a new 2 dimensional array of the underlying number type
 Class<N> numberClass()
          The class representing numbers of this number operations instance
 N one()
          Returns the value representing one
 N pow(N numA, N numB)
          Returns the powered value, i.e.
 N readFrom(DataInput in)
          Reads binary data from the given data input.
 N reduce(N number)
          Converts the number into some reduced form.
 N[] reduceVector(boolean cloneOnChange, N... vector)
          Converts a vector into some reduced form.
 int signum(N number)
          Returns signum of the value, i.e.
 N subtract(N numA, N numB)
          Returns the difference of the two values, i.e.
 byte[] toByteArray(N number)
          Converts this number to binary data and returns it as a byte array.
 N valueOf(double value)
          Create an instance of the underlying number type based on the submitted double value
 N valueOf(long value)
          Create an instance of the underlying number type based on the submitted long integer value
 N valueOf(Number n)
          Create an instance of the underlying number type based on the submitted number
 N valueOf(String s)
          Create an instance of the underlying number type based on the given string representation of the number
 void writeTo(N number, DataOutput out)
          Converts this number to binary data and writes it to the data output.
 N zero()
          Returns the value representing zero
 
Methods inherited from interface java.util.Comparator
compare, equals
 

Method Detail

numberClass

Class<N> numberClass()
The class representing numbers of this number operations instance


newArray

N[] newArray(int size)
Create a new array of the underlying number type


newArray

N[][] newArray(int rows,
               int cols)
Create a new 2 dimensional array of the underlying number type


valueOf

N valueOf(String s)
                         throws NumberFormatException
Create an instance of the underlying number type based on the given string representation of the number

Throws:
NumberFormatException - If converting the string to a number fails

valueOf

N valueOf(Number n)
                         throws IllegalArgumentException
Create an instance of the underlying number type based on the submitted number

Throws:
IllegalArgumentException - If converting into the underlying number type fails or is not supported

valueOf

N valueOf(long value)
                         throws IllegalArgumentException
Create an instance of the underlying number type based on the submitted long integer value

Throws:
IllegalArgumentException - If converting into the underlying number type fails or is not supported

valueOf

N valueOf(double value)
                         throws IllegalArgumentException
Create an instance of the underlying number type based on the submitted double value

Throws:
IllegalArgumentException - If converting into the underlying number type fails or is not supported

abs

N abs(N number)
Returns the absolute value of the given number, i.e. a non-negative value abs(number)


negate

N negate(N number)
Returns the negated value, i.e. -number


add

N add(N numA,
      N numB)
Returns the sum of the two values, i.e. numA + numB


subtract

N subtract(N numA,
           N numB)
Returns the difference of the two values, i.e. numA - numB


multiply

N multiply(N numA,
           N numB)
Returns the product of the two values, i.e. numA * numB


divide

N divide(N numA,
         N numB)
Returns the ratio of the two values, i.e. numA / numB.

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


invert

N invert(N number)
                        throws IllegalArgumentException
Returns the inverted value, i.e. 1 / number

Throws:
IllegalArgumentException - If inversion of the value leads to a number which cannot be converted to the underlying number type

pow

N pow(N numA,
      N numB)
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.


zero

N zero()
Returns the value representing zero


one

N one()
Returns the value representing one


reduce

N reduce(N number)
Converts the number into some reduced form. This can for instance mean reduction for fraction numbers. If no reduction can be made, the number is returned unchanged.


reduceVector

N[] reduceVector(boolean cloneOnChange,
                 N... vector)
Converts a vector into some reduced form. For integer or fraction number vectors, this can for instance mean that the vector is shortened by the greatest common divisor of all vector components.

Note that the vector length might change by this operation. If only individual components of the vector should be reduced, which is not affecting the vector's length, reduce each number separately.

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

int signum(N number)
Returns signum of the value, i.e. -1, 0, 1 for negative, zero or positive values


isOne

boolean isOne(N number)
Returns true if this value is numerically equal to one, i.e. number == 1


isZero

boolean isZero(N number)
Returns true if this value is numerically equal to zero, i.e. number == 0


isNonZero

boolean isNonZero(N number)
Returns true if this value is numerically unequal to zero, i.e. number != 0


isPositive

boolean isPositive(N number)
Returns true if this value positive, i.e. number > 0


isNonPositive

boolean isNonPositive(N number)
Returns true if this value not positive, i.e. number <= 0


isNegative

boolean isNegative(N number)
Returns true if this value negative, i.e. number < 0


isNonNegative

boolean isNonNegative(N number)
Returns true if this value not negative, i.e. number >= 0


max

N max(N valA,
      N valB)
Returns the larger value of the two numbers, i.e. max(valA, valB)


max

N max(N... vals)
Returns the largest value of the given numbers, i.e. max(vals)


min

N min(N valA,
      N valB)
Returns the smaller value of the two numbers, i.e. min(valA, valB)


min

N min(N... vals)
Returns the smallest value of the given numbers, i.e. min(vals)


toByteArray

byte[] toByteArray(N number)
Converts this number to binary data and returns it as a byte array. Back-conversion is possible using the fromByteArray(byte[]) method.


fromByteArray

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


writeTo

void writeTo(N number,
             DataOutput out)
             throws IOException
Converts this number to binary data and writes it to the data output. The counterpart of this method reading binary data is readFrom(DataInput).

Throws:
IOException

readFrom

N 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 writeTo(Number, DataOutput)

Throws:
IOException

byteLength

int byteLength()
Returns the byte array length if toByteArray(Number) is used and if this size is fixed, and -1 if the size may vary depending on the number instance