net.sf.mmm.util.math.base
Class MathUtilImpl

java.lang.Object
  extended by net.sf.mmm.util.component.base.AbstractComponent
      extended by net.sf.mmm.util.component.base.AbstractLoggableComponent
          extended by net.sf.mmm.util.math.base.MathUtilImpl
All Implemented Interfaces:
MathUtil

@Singleton
@Named
public class MathUtilImpl
extends AbstractLoggableComponent
implements MathUtil

This class is a collection of utility functions for dealing with numbers.

Since:
1.0.0
Author:
Joerg Hohwiller (hohwille at users.sourceforge.net)

Field Summary
private static MathUtil instance
           
 
Constructor Summary
MathUtilImpl()
          The constructor.
 
Method Summary
static MathUtil getInstance()
          This method gets the singleton instance of MathUtil.
 NumberType<? extends Number> getNumberType(Class<?> numericType)
          This method gets the NumberType for the given numericType.
<NUMBER extends Number>
NumberType<NUMBER>
getNumberTypeGeneric(Class<NUMBER> numericType)
          This method gets the NumberType for the given numericType.
 Number toSimplestNumber(Number value)
          This method converts the given value to the simplest suitable number-type.
 
Methods inherited from class net.sf.mmm.util.component.base.AbstractLoggableComponent
doInitialize, getLogger, setLogger
 
Methods inherited from class net.sf.mmm.util.component.base.AbstractComponent
doInitialized, getInitializationState, initialize
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

instance

private static MathUtil instance
See Also:
getInstance()
Constructor Detail

MathUtilImpl

public MathUtilImpl()
The constructor.

Method Detail

getInstance

public static MathUtil getInstance()
This method gets the singleton instance of MathUtil.
This design is the best compromise between easy access (via this indirection you have direct, static access to all offered functionality) and IoC-style design which allows extension and customization.
For IoC usage, simply ignore all static getInstance() methods and construct new instances via the container-framework of your choice (like plexus, pico, springframework, etc.). To wire up the dependent components everything is properly annotated using common-annotations (JSR-250). If your container does NOT support this, you should consider using a better one.

Returns:
the singleton instance.

getNumberType

public NumberType<? extends Number> getNumberType(Class<?> numericType)
This method gets the NumberType for the given numericType.

Specified by:
getNumberType in interface MathUtil
Parameters:
numericType - is the class reflecting a Number. It may be primitive (such as int.class). The signature is NOT bound to Number to make it easy for the caller (e.g. Number.class.isAssignableFrom(int.class) is false).
Returns:
the NumberType representing the given numericType or null if the given numericType is no Number or is NOT known (you may extend this MathUtil in such case).
See Also:
MathUtil.getNumberTypeGeneric(Class)

getNumberTypeGeneric

public <NUMBER extends Number> NumberType<NUMBER> getNumberTypeGeneric(Class<NUMBER> numericType)
This method gets the NumberType for the given numericType.

Specified by:
getNumberTypeGeneric in interface MathUtil
Type Parameters:
NUMBER - is the generic type of the numericType.
Parameters:
numericType - is the class reflecting a Number. It may be primitive (such as int.class).
Returns:
the NumberType representing the given numericType or null if the given numericType is NOT known (you may extend this MathUtil in such case).

toSimplestNumber

public Number toSimplestNumber(Number value)
This method converts the given value to the simplest suitable number-type. The ordering implied by "simplest" is Byte < Short < Integer < Long < Float < Double.
Please note that a decimal Double is only converted to Float if the result is exactly the same. Be aware that 0.2F - 0.2 is NOT 0.0 (but 2.980232227667301E-9).

Specified by:
toSimplestNumber in interface MathUtil
Parameters:
value - is the value to convert.
Returns:
a number with the same Number.doubleValue() as the given value and the simplest possible type.


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