net.sf.mmm.util.math.base
Class NumberTypeImpl<NUMBER extends Number>

java.lang.Object
  extended by net.sf.mmm.util.math.base.NumberTypeImpl<NUMBER>
Type Parameters:
NUMBER - is the generic type of the represented number-class.
All Implemented Interfaces:
NumberType<NUMBER>

public abstract class NumberTypeImpl<NUMBER extends Number>
extends Object
implements NumberType<NUMBER>

This is the implementation of the NumberType interface.

Since:
1.0.1
Author:
Joerg Hohwiller (hohwille at users.sourceforge.net)
See Also:
MathUtilImpl.getNumberType(Class)

Field Summary
static NumberTypeImpl<AtomicInteger> ATOMIC_INTEGER
          The NumberTypeImpl for AtomicInteger.
static NumberTypeImpl<AtomicLong> ATOMIC_LONG
          The NumberTypeImpl for AtomicLong.
static NumberTypeImpl<BigDecimal> BIG_DECIMAL
          The NumberTypeImpl for BigDecimal.
static NumberTypeImpl<BigInteger> BIG_INTEGER
          The NumberTypeImpl for BigInteger.
static NumberTypeImpl<Byte> BYTE
          The NumberTypeImpl for Byte.
static NumberTypeImpl<Double> DOUBLE
          The NumberTypeImpl for Double.
private  int exactness
           
static NumberTypeImpl<Float> FLOAT
          The NumberTypeImpl for Float.
static NumberTypeImpl<Integer> INTEGER
          The NumberTypeImpl for Integer.
static NumberTypeImpl<Long> LONG
          The NumberTypeImpl for Long.
private static double REQUIRED_PRECISION
          The maximum delta allowed for valueOf(Number, boolean).
static NumberTypeImpl<Short> SHORT
          The NumberTypeImpl for Short.
 
Constructor Summary
NumberTypeImpl(int exactness)
          The constructor.
 
Method Summary
protected abstract  NUMBER convert(Number number)
          This method converts the given number to the number-class represented by this object.
 int getExactnessDifference(NumberType<?> otherType)
          This method gets the difference of the exactness of this NumberTypeImpl and the given otherType.
protected abstract  NUMBER parse(String number)
          This method gets an instance of the represented number-class with the numeric value identified by the given string number.
 NUMBER valueOf(Number number, boolean failIfUnprecise)
          This method gets an instance of the represented number-class with the numeric value given by number.
 NUMBER valueOf(String number)
          This method gets an instance of the represented number-class with the numeric value identified by the given string number.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface net.sf.mmm.util.math.api.NumberType
getNumberClass, isDecimal
 

Field Detail

REQUIRED_PRECISION

private static final double REQUIRED_PRECISION
The maximum delta allowed for valueOf(Number, boolean).

See Also:
Constant Field Values

BYTE

public static final NumberTypeImpl<Byte> BYTE
The NumberTypeImpl for Byte.


SHORT

public static final NumberTypeImpl<Short> SHORT
The NumberTypeImpl for Short.


INTEGER

public static final NumberTypeImpl<Integer> INTEGER
The NumberTypeImpl for Integer.


ATOMIC_INTEGER

public static final NumberTypeImpl<AtomicInteger> ATOMIC_INTEGER
The NumberTypeImpl for AtomicInteger.


LONG

public static final NumberTypeImpl<Long> LONG
The NumberTypeImpl for Long.


ATOMIC_LONG

public static final NumberTypeImpl<AtomicLong> ATOMIC_LONG
The NumberTypeImpl for AtomicLong.


FLOAT

public static final NumberTypeImpl<Float> FLOAT
The NumberTypeImpl for Float.


DOUBLE

public static final NumberTypeImpl<Double> DOUBLE
The NumberTypeImpl for Double.


BIG_INTEGER

public static final NumberTypeImpl<BigInteger> BIG_INTEGER
The NumberTypeImpl for BigInteger.


BIG_DECIMAL

public static final NumberTypeImpl<BigDecimal> BIG_DECIMAL
The NumberTypeImpl for BigDecimal.


exactness

private final int exactness
See Also:
getExactnessDifference(NumberType)
Constructor Detail

NumberTypeImpl

NumberTypeImpl(int exactness)
The constructor.

Parameters:
exactness - is the internal exactness level for getExactnessDifference(NumberType).
Method Detail

convert

protected abstract NUMBER convert(Number number)
This method converts the given number to the number-class represented by this object. Like a cast this operation may loose precision (e.g. when converting a Double to Integer) without warning. Use valueOf(Number, boolean) instead to avoid this.

Parameters:
number - is the number to convert.
Returns:
the converted number.

valueOf

public NUMBER valueOf(Number number,
                      boolean failIfUnprecise)
                              throws NumberConversionException
This method gets an instance of the represented number-class with the numeric value given by number.

Specified by:
valueOf in interface NumberType<NUMBER extends Number>
Parameters:
number - is the numeric value to convert.
failIfUnprecise - - if true and conversion causes precision loss, a NumberConversionException is thrown, if false this method acts like a primitive cast and conversion is always successfully.
Returns:
a Number instance of the represented number-class with the value of the given number. This will be the same instance as number if it is an instance of the represented number-class .
Throws:
NumberConversionException - if the conversion will loose precision (e.g. when converting a decimal Double to Integer)

valueOf

public NUMBER valueOf(String number)
                              throws NumberConversionException
This method gets an instance of the represented number-class with the numeric value identified by the given string number.

Specified by:
valueOf in interface NumberType<NUMBER extends Number>
Parameters:
number - is the string to be parsed as number.
Returns:
the parsed number of the according type.
Throws:
NumberConversionException - if the given number has an illegal format.

parse

protected abstract NUMBER parse(String number)
                                        throws NumberConversionException,
                                               NumberFormatException
This method gets an instance of the represented number-class with the numeric value identified by the given string number.

Parameters:
number - is the string to be parsed as number.
Returns:
the parsed number of the according type.
Throws:
NumberConversionException - if the given number has an illegal format.
NumberFormatException - if the given number has an illegal format. This exception will be converted to a NumberConversionException.

getExactnessDifference

public int getExactnessDifference(NumberType<?> otherType)
This method gets the difference of the exactness of this NumberTypeImpl and the given otherType.
ATTENTION:
Some types such as Double and BigInteger are NOT really comparable so the exactness difference might only make sense if the compared types are both decimal or both non-decimal (mathematical integers). However the order of typical types is:
  1. Byte
  2. Short
  3. Integer
  4. Long
  5. Float
  6. Double
  7. BigInteger
  8. BigDecimal

Specified by:
getExactnessDifference in interface NumberType<NUMBER extends Number>
Parameters:
otherType - is the NumberTypeImpl to compare with.
Returns:
the difference of the exactness. Will be 0 if this NumberTypeImpl is equal to otherType, positive if this NumberTypeImpl is more exact (later in the examples above) and negative if otherType is more exact.


Copyright © 2001-2010 mmm-Team. All Rights Reserved.