org.eclipse.ocl.internal.evaluation
Class NumberUtil

java.lang.Object
  extended by org.eclipse.ocl.internal.evaluation.NumberUtil

public class NumberUtil
extends java.lang.Object

Utility for converting numeric values to double or integer precision as needed.

Since:
1.2

Constructor Summary
NumberUtil()
           
 
Method Summary
static java.lang.Number coerceNumber(java.lang.Number number)
           Tests whether a given number can be safely coerced to Double or Integer without changing the value of the number.
static java.lang.Number commonPrecisionNumber(java.lang.Number number, java.lang.Number referenceNumber)
           Coerces the given number to a common or greater precision than referenceNumber, BigDecimal is greater than Double or BigInteger which is greater than Long precision.
static java.lang.Number higherPrecisionNumber(java.lang.Number number)
           Coerces the given number to Double or Long precision, if possible.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

NumberUtil

public NumberUtil()
Method Detail

coerceNumber

public static java.lang.Number coerceNumber(java.lang.Number number)

Tests whether a given number can be safely coerced to Double or Integer without changing the value of the number. Safe means that coercing a number to Double or Integer and then coercing it back to the original type will result in the same value (no loss of precision). This is trivial for types, which have a smaller domain then Integer or Double, but for example a Long number may not be safely coerced to Integer.

If the coercion is safe, the number will be returned as either Double or Integer, as appropriate to the original precision. Otherwise the original number is returned.

Parameters:
number - a number to coerce to Integer or Double
Returns:
the coerced number, or the original number, if coercion was not safe

commonPrecisionNumber

public static java.lang.Number commonPrecisionNumber(java.lang.Number number,
                                                     java.lang.Number referenceNumber)

Coerces the given number to a common or greater precision than referenceNumber, BigDecimal is greater than Double or BigInteger which is greater than Long precision.

Parameters:
number - a number to coerce to a common precision
referenceNumber - another number to share the common precision
Returns:
the coerced number, or the original number, in case of overflow

higherPrecisionNumber

public static java.lang.Number higherPrecisionNumber(java.lang.Number number)

Coerces the given number to Double or Long precision, if possible. Note that this is only impossible for BigDecimal or BigInteger values, respectively, that are out of range of their primitive counterparts.

Parameters:
number - a number to coerce to Long or Double
Returns:
the coerced number, or the original number, in case of overflow