au.gov.tas.dpiwe.mr.util
Class Numbers

java.lang.Object
  extended by au.gov.tas.dpiwe.mr.util.Numbers

public class Numbers
extends Object

This class contains static utility methods to manipulate numbers of the standard Java immutable kind, primitive numeric types, mutable numbers of the Mr Architecture kind and NullableNumbers.

There is a reason why this class is used for output and type conversion only (and is therefore not symmetrical/ orthogonal)- the output methods are useful in the toolkit subpackage, and often users of the toolkit subpackage don't want to be dependent on J2EE, which is what would happen if we were to move those to PresentationUtilities where they really belong. Naturally, advanced stuff should be left to Ms Architecture.

Since:
Mr Architecture 2.3
Version:
1.0.0.8
Author:
Kade Hansson

Field Summary
static BigDecimal Number_1000
          A modest number.
static BigDecimal Number_1E12
          A huge number.
static BigDecimal Number_1E15
          A massive number.
static BigDecimal Number_1E18
          An enormous number.
static BigDecimal Number_1E6
          A large number.
static BigDecimal Number_1E9
          A great number.
static BigDecimal Number_1EMinus12
          A very tiny number.
static BigDecimal Number_1EMinus15
          A miniscule number.
static BigDecimal Number_1EMinus18
          An infinitesimal number.
static BigDecimal Number_1EMinus3
          A small number.
static BigDecimal Number_1EMinus6
          A very small number.
static BigDecimal Number_1EMinus9
          A tiny number.
 
Constructor Summary
Numbers()
           
 
Method Summary
static NumberFormat applyMetaData(NumberFormat formatter, TypedNumber number)
          Takes the metadata supplied with a number and applies it to the given formatter.
static TypedNumber applyMetaData(TypedNumber number, MetaData.TypeRecord metaData)
          Takes the metadata supplied and applies it to the given number.
static byte byteValue(Number input)
          Produces a primitive value.
static double doubleValue(Number input)
          Produces a primitive value.
static String expressValue(NumberFormat formatter, BigDecimal value, int exponent)
          Scales and expresses a value using the specified formatter.
static String expressValue(NumberFormat formatter, TypedNumber number)
          Expresses a numeric value in the specified format, while obeying restrinctions on minimal and maximal places before and after the radix point.
static String expressValue(TypedNumber number)
          Expresses a numeric value in the default format, while obeying restrinctions on minimal and maximal places before and after the radix point.
static String expressValueWithUnit(NumberFormat formatter, TypedNumber number)
          Expresses a numeric value with its unit in the specified format, while obeying restrinctions on minimal and maximal places before and after the radix point.
static String expressValueWithUnit(TypedNumber number)
          Expresses a numeric value with its unit in the default format, while obeying restrinctions on minimal and maximal places before and after the radix point.
static float floatValue(Number input)
          Produces a primitive value.
static Number immutable(byte input)
          Produces an immutable object wrapping the specified primitive value.
static Number immutable(double input)
          Produces an immutable object wrapping the specified primitive value.
static Number immutable(float input)
          Produces an immutable object wrapping the specified primitive value.
static Number immutable(int input)
          Produces an immutable object wrapping the specified primitive value.
static Number immutable(long input)
          Produces an immutable object wrapping the specified primitive value.
static Number immutable(Number input)
          Changes the specified number into an immutable object, where a corresponding immutable Number sublcass is known, otherwise the input is returned.
static Number immutable(short input)
          Produces an immutable object wrapping the specified primitive value.
static Interval interval(Number input)
          Changes the specified number into an Interval object, where a corresponding interval Number sublcass is known, otherwise an exception is thrown.
static int intValue(Number input)
          Produces a primitive value.
static long longValue(Number input)
          Produces a primitive value.
static Number mutable(byte input)
          Produces a mutable object wrapping the specified primitive value.
static Number mutable(double input)
          Produces a mutable object wrapping the specified primitive value.
static Number mutable(float input)
          Produces a mutable object wrapping the specified primitive value.
static Number mutable(int input)
          Produces a mutable object wrapping the specified primitive value.
static Number mutable(long input)
          Produces a mutable object wrapping the specified primitive value.
static Number mutable(Number input)
          Changes the specified number into an mutable object, where a corresponding mutable Number sublcass is known, otherwise the input is returned.
static Number mutable(short input)
          Produces a mutable object wrapping the specified primitive value.
static Number reconcile(Number prototype, Number value)
          Produces a number with the specified type and value.
static Interval setUpperBound(Interval throughput, Number input)
          Changes the specified interval, or returns a copy with the upper bound changed.
static short shortValue(Number input)
          Produces a primitive value.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

Number_1E18

public static final BigDecimal Number_1E18
An enormous number.


Number_1E15

public static final BigDecimal Number_1E15
A massive number.


Number_1E12

public static final BigDecimal Number_1E12
A huge number.


Number_1E9

public static final BigDecimal Number_1E9
A great number.


Number_1E6

public static final BigDecimal Number_1E6
A large number.


Number_1000

public static final BigDecimal Number_1000
A modest number.


Number_1EMinus3

public static final BigDecimal Number_1EMinus3
A small number.


Number_1EMinus6

public static final BigDecimal Number_1EMinus6
A very small number.


Number_1EMinus9

public static final BigDecimal Number_1EMinus9
A tiny number.


Number_1EMinus12

public static final BigDecimal Number_1EMinus12
A very tiny number.


Number_1EMinus15

public static final BigDecimal Number_1EMinus15
A miniscule number.


Number_1EMinus18

public static final BigDecimal Number_1EMinus18
An infinitesimal number.

Constructor Detail

Numbers

public Numbers()
Method Detail

setUpperBound

public static Interval setUpperBound(Interval throughput,
                                     Number input)
Changes the specified interval, or returns a copy with the upper bound changed.

Parameters:
throughput - an interval, from which the lower bound (at least) is preserved.
input - the new upper bound for the interval.
Returns:
the interval changed, or a new interval with the upper bound input and the same lower bound as thoughput.

immutable

public static Number immutable(Number input)
Changes the specified number into an immutable object, where a corresponding immutable Number sublcass is known, otherwise the input is returned.

Parameters:
input - containing the value to make an immutable number from.
Returns:
an immutable form of the input number, if the input subtype is recognised.

interval

public static Interval interval(Number input)
Changes the specified number into an Interval object, where a corresponding interval Number sublcass is known, otherwise an exception is thrown.

Parameters:
input - containing the value to make an Interval from.
Returns:
an Interval form of the input Number, if the input subtype is recognised.
Throws:
IllegalArgumentException - if the input subtype is not recognised.

immutable

public static Number immutable(byte input)
Produces an immutable object wrapping the specified primitive value.

Parameters:
input - the value to make an immutable object from.
Returns:
an immutable object containing the input value.

immutable

public static Number immutable(short input)
Produces an immutable object wrapping the specified primitive value.

Parameters:
input - the value to make an immutable object from.
Returns:
an immutable object containing the input value.

immutable

public static Number immutable(int input)
Produces an immutable object wrapping the specified primitive value.

Parameters:
input - the value to make an immutable object from.
Returns:
an immutable object containing the input value.

immutable

public static Number immutable(long input)
Produces an immutable object wrapping the specified primitive value.

Parameters:
input - the value to make an immutable object from.
Returns:
an immutable object containing the input value.

immutable

public static Number immutable(float input)
Produces an immutable object wrapping the specified primitive value.

Parameters:
input - the value to make an immutable object from.
Returns:
an immutable object containing the input value.

immutable

public static Number immutable(double input)
Produces an immutable object wrapping the specified primitive value.

Parameters:
input - the value to make an immutable object from.
Returns:
an immutable object containing the input value.

byteValue

public static byte byteValue(Number input)
Produces a primitive value.

Parameters:
input - the value to make a byte from.
Returns:
a byte containing the input value, or 0 if null.

shortValue

public static short shortValue(Number input)
Produces a primitive value.

Parameters:
input - the value to make a short from.
Returns:
a short containing the input value, or 0 if null.

doubleValue

public static double doubleValue(Number input)
Produces a primitive value.

Parameters:
input - the value to make a double from.
Returns:
a double containing the input value, or 0 if null.

floatValue

public static float floatValue(Number input)
Produces a primitive value.

Parameters:
input - the value to make a float from.
Returns:
a float containing the input value, or 0 if null.

longValue

public static long longValue(Number input)
Produces a primitive value.

Parameters:
input - the value to make a long from.
Returns:
a long containing the input value, or 0 if null.

intValue

public static int intValue(Number input)
Produces a primitive value.

Parameters:
input - the value to make an integer from.
Returns:
an int containing the input value, or 0 if null.

mutable

public static Number mutable(Number input)
Changes the specified number into an mutable object, where a corresponding mutable Number sublcass is known, otherwise the input is returned.

Parameters:
number - containing the value to make a mutable number from.
Returns:
a mutable form of the input number, if the input subtype is recognised.
Throws:
NullPointerException - if the number represented by the input is null, or if input is null.

mutable

public static Number mutable(byte input)
Produces a mutable object wrapping the specified primitive value.

Parameters:
input - the value to make a mutable object from.
Returns:
a mutable object containing the input value.

mutable

public static Number mutable(short input)
Produces a mutable object wrapping the specified primitive value.

Parameters:
input - the value to make a mutable object from.
Returns:
a mutable object containing the input value.

mutable

public static Number mutable(int input)
Produces a mutable object wrapping the specified primitive value.

Parameters:
input - the value to make a mutable object from.
Returns:
a mutable object containing the input value.

mutable

public static Number mutable(long input)
Produces a mutable object wrapping the specified primitive value.

Parameters:
input - the value to make a mutable object from.
Returns:
a mutable object containing the input value.

mutable

public static Number mutable(float input)
Produces a mutable object wrapping the specified primitive value.

Parameters:
input - the value to make a mutable object from.
Returns:
a mutable object containing the input value.

mutable

public static Number mutable(double input)
Produces a mutable object wrapping the specified primitive value.

Parameters:
input - the value to make a mutable object from.
Returns:
a mutable object containing the input value.

reconcile

public static Number reconcile(Number prototype,
                               Number value)
Produces a number with the specified type and value. If the prototype is mutable, then the prototype will be mutated to contain the new value.

All the warnings to do with Java value promotion and demotion apply.

Parameters:
prototype - a number of the type to be produced, which may not be null.
value - the value to be assigned to the produced number. If the value is null, then a NullableNumber subclass is used to wrap the value irrespective of the prototype specified.
Returns:
a number of the given type with the given value.

applyMetaData

public static TypedNumber applyMetaData(TypedNumber number,
                                        MetaData.TypeRecord metaData)
Takes the metadata supplied and applies it to the given number.

Parameters:
number - the number whose type information is to be changed.
metaData - the metadata describing the new type information.

applyMetaData

public static NumberFormat applyMetaData(NumberFormat formatter,
                                         TypedNumber number)
Takes the metadata supplied with a number and applies it to the given formatter.

Parameters:
formatter - the formatter whose formatting parameters are to be changed.
number - the number describing the new formatting parameters.

expressValueWithUnit

public static String expressValueWithUnit(TypedNumber number)
Expresses a numeric value with its unit in the default format, while obeying restrinctions on minimal and maximal places before and after the radix point.

Parameters:
number - the number to be expressed.
Returns:
the expressed number.
Throws:
NullPointerException - if this value has no units.

expressValueWithUnit

public static String expressValueWithUnit(NumberFormat formatter,
                                          TypedNumber number)
Expresses a numeric value with its unit in the specified format, while obeying restrinctions on minimal and maximal places before and after the radix point.

Parameters:
formatter - the object through which formatting will be achieved.
number - the number to be expressed.
Returns:
the expressed number.
Throws:
NullPointerException - if this value has no units.

expressValue

public static String expressValue(TypedNumber number)
Expresses a numeric value in the default format, while obeying restrinctions on minimal and maximal places before and after the radix point.

Parameters:
number - the number to be expressed.
Returns:
the expressed number.
Throws:
NullPointerException - if this value has no units.

expressValue

public static String expressValue(NumberFormat formatter,
                                  TypedNumber number)
Expresses a numeric value in the specified format, while obeying restrinctions on minimal and maximal places before and after the radix point.

Parameters:
formatter - the object through which formatting will be achieved.
number - the number to be expressed.
Returns:
the expressed number.
Throws:
NullPointerException - if this value has no units.

expressValue

public static String expressValue(NumberFormat formatter,
                                  BigDecimal value,
                                  int exponent)
Scales and expresses a value using the specified formatter.

Parameters:
formatter - the object through which formatting will be achieved.
value - the decimal to be expressed.
exponent - the power of ten to scale by.