net.sf.mmm.util.math.api
Interface NumberType<NUMBER extends Number>

Type Parameters:
NUMBER - is the generic type of the represented number-class.
All Known Implementing Classes:
NumberTypeImpl

public interface NumberType<NUMBER extends Number>

This class represents the type reflecting a specific Number. It allows to check attributes like isDecimal() or to compare NumberTypes.
Further it acts as factory to create according numbers from string or by converting an given number.
This is a class and NOT an Enum to be extensible.

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

Method Summary
 int getExactnessDifference(NumberType<?> otherType)
          This method gets the difference of the exactness of this NumberType and the given otherType.
 Class<NUMBER> getNumberClass()
          This method gets the Class reflecting the Number represented by this NumberType.
 boolean isDecimal()
          This method determines if the represented Number is a decimal value.
 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.
 

Method Detail

valueOf

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.

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

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.

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.

getNumberClass

Class<NUMBER> getNumberClass()
This method gets the Class reflecting the Number represented by this NumberType.

Returns:
the number type.

isDecimal

boolean isDecimal()
This method determines if the represented Number is a decimal value.
E.g. Double, Float, or BigDecimal represent decimal values while Integer or Long are NOT decimal.

Returns:
true if the represented Number is a decimal value.

getExactnessDifference

int getExactnessDifference(NumberType<?> otherType)
This method gets the difference of the exactness of this NumberType 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

Parameters:
otherType - is the NumberType to compare with.
Returns:
the difference of the exactness. Will be 0 if this NumberType is equal to otherType, positive if this NumberType is more exact (later in the examples above) and negative if otherType is more exact.


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