List of usage examples for java.lang Number longValue
public abstract long longValue();
From source file:com.amazonaws.hal.client.ConversionUtil.java
private static Object convertFromNumber(Class<?> clazz, Number value) { if (String.class.isAssignableFrom(clazz)) { return value.toString(); } else if (int.class.isAssignableFrom(clazz) || Integer.class.isAssignableFrom(clazz)) { return value.intValue(); } else if (long.class.isAssignableFrom(clazz) || Long.class.isAssignableFrom(clazz)) { return value.longValue(); } else if (short.class.isAssignableFrom(clazz) || Short.class.isAssignableFrom(clazz)) { return value.shortValue(); } else if (double.class.isAssignableFrom(clazz) || Double.class.isAssignableFrom(clazz)) { return value.doubleValue(); } else if (float.class.isAssignableFrom(clazz) || Float.class.isAssignableFrom(clazz)) { return value.floatValue(); } else if (boolean.class.isAssignableFrom(clazz) || Boolean.class.isAssignableFrom(clazz)) { return Boolean.valueOf(value.toString()); } else if (char.class.isAssignableFrom(clazz) || Character.class.isAssignableFrom(clazz)) { if (value.longValue() <= 255) { return (char) value.longValue(); } else {/*from w w w . ja v a 2 s .c om*/ throw new RuntimeException("Not sure how to convert " + value + " to a " + clazz.getSimpleName()); } } else if (byte.class.isAssignableFrom(clazz) || Byte.class.isAssignableFrom(clazz)) { return value.byteValue(); } else if (BigDecimal.class.isAssignableFrom(clazz)) { return new BigDecimal(value.toString()); } else if (BigInteger.class.isAssignableFrom(clazz)) { // Necessary because BigInteger(long) is a private method and we need to convert the Number to a long to // prevent the constructor from throwing a NumberFormatException Example: BigInteger(1.2) return new BigInteger(String.valueOf(value.longValue())); } else if (Date.class.isAssignableFrom(clazz)) { return new Date(value.longValue()); } else if (clazz.isEnum()) { try { //noinspection unchecked return Enum.valueOf((Class<Enum>) clazz, value.toString()); } catch (IllegalArgumentException e) { log.error(String.format( "'%s' is not a recognized enum value for %s. Returning default of %s instead.", value, clazz.getName(), clazz.getEnumConstants()[0])); return clazz.getEnumConstants()[0]; } } else { throw new RuntimeException("Not sure how to convert " + value + " to a " + clazz.getSimpleName()); } }
From source file:org.batoo.jpa.common.reflect.ReflectHelper.java
/** * Converts the number into number Type//w w w .j ava 2s. c om * * @param value * the number value * @param numberType * the number type * @return the converted number value * * @since $version * @author hceylan */ public static Number convertNumber(Number value, Class<?> numberType) { if (numberType == Integer.class) { return value.intValue(); } if (numberType == Long.class) { return value.longValue(); } if (numberType == Short.class) { return value.shortValue(); } if (numberType == Byte.class) { return value.byteValue(); } if (numberType == Float.class) { return value.floatValue(); } if (numberType == Double.class) { return value.doubleValue(); } throw new IllegalArgumentException(numberType + " not supported"); }
From source file:NumberUtils.java
/** * Compares the first number to the second one numerically and * returns an integer depending on the comparison result: * a negative value if the first number is the smaller one, * a zero value if they are equal, and//from w w w . j ava 2 s .c o m * a positive value if the first number is the larger one. * * The main strategy goes like follows: * 1. If one of the arguments is <code>null</code> or 'not a number', * throw an exception. * 2. If both values are 'long compatible', compare their <code>longValue()</code> * using the usual comparison operators for primitive types (<, ==, >). * 3. If both values are 'double compatible', compare their <code>doubleValue()</code> * using the usual comparison operators for primitive types (<, ==, >). * 4. If one of the values is infinite (and the other is finite), * determine the result depending on the sign of the infinite value. * 5. Otherwise convert both values to <code>java.math.BigDecimal</code> and * return the result of the <code>BigDecimal.compareTo(BigDecimal)</code> method. * * As a consequence, the method is not suitable to implement a * <code>java.util.Comparator</code> for numbers. To achieve this, * one had to accept 'not a number' arguments and place them somewhere * in the row of numbers (probably at the upper end, i.e. larger than * positive infinity, as <code>Double.compare(double, double)</code> * does it). * So the behavior of this method is like that of the comparison * operator for primitive types and not like that of the related * <code>compareTo(...)</code> methods. Besides the handling of * 'not a number' values this makes a difference, when comparing * the float or double values <code>-0.0</code> and <code>0.0</code>: * again, like the operators, we consider them as equal (whereas * according to <code>Double.compareTo(...)</code> <code>-0.0</code> * is less than <code>0.0</code>). * * @param first * @param second * @return int * @throws ArithmeticException One or both of the given numbers is <code>null</code> or 'not a number'. */ public static int compare(Number first, Number second) throws ArithmeticException { if (first == null || second == null || isNaN(first) || isNaN(second)) throw new ArithmeticException("Arguments must not be null or NaN."); int result = -2; if (isLongCompatible(first) && isLongCompatible(second)) { long v1 = first.longValue(), v2 = second.longValue(); result = v1 < v2 ? -1 : v1 == v2 ? 0 : v1 > v2 ? 1 : 2; } else if (isDoubleCompatible(first) && isDoubleCompatible(second)) { double v1 = first.doubleValue(), v2 = second.doubleValue(); result = v1 < v2 ? -1 : v1 == v2 ? 0 : v1 > v2 ? 1 : 2; } if (result == 2) // should not happen throw new ArithmeticException("Arguments " + first + " and " + second + " are not comparable."); if (result > -2) return result; if (isInfinite(first)) // => second is finite return first.doubleValue() == Double.NEGATIVE_INFINITY ? -1 : 1; if (isInfinite(second)) // => first is finite return second.doubleValue() == Double.POSITIVE_INFINITY ? -1 : 1; return toBigDecimal(first).compareTo(toBigDecimal(second)); }
From source file:iDynoOptimizer.MOEAFramework26.src.org.moeaframework.util.tree.NumberArithmetic.java
/** * Returns the smallest integer value greater than or equal to the given * number.// w ww . j a v a 2 s .co m * * @param a the number * @return the smallest integer value greater than or equal to the given * number * @see Math#ceil(double) */ public static Number ceil(Number a) { if (isFloatingPoint(a)) { return Math.ceil(a.doubleValue()); } else { return a.longValue(); } }
From source file:iDynoOptimizer.MOEAFramework26.src.org.moeaframework.util.tree.NumberArithmetic.java
/** * Returns the largest integer value less than or equal to the given * number./*from w w w . ja va 2 s.co m*/ * * @param a the number * @return the largest integer value less than or equal to the given * number * @see Math#floor(double) */ public static Number floor(Number a) { if (isFloatingPoint(a)) { return Math.floor(a.doubleValue()); } else { return a.longValue(); } }
From source file:iDynoOptimizer.MOEAFramework26.src.org.moeaframework.util.tree.NumberArithmetic.java
/** * Returns the value of the number rounded to the nearest integer. * /*from w w w . java 2 s . c o m*/ * @param a the number * @return the value of the number rounded to the nearest integer * @see Math#round(double) */ public static Number round(Number a) { if (isFloatingPoint(a)) { return Math.round(a.doubleValue()); } else { return a.longValue(); } }
From source file:iDynoOptimizer.MOEAFramework26.src.org.moeaframework.util.tree.NumberArithmetic.java
/** * Returns the absolute value of the number. * //from www . j a v a2s. c om * @param a the number * @return the absolute value of the number * @see Math#abs(long) * @see Math#abs(double) */ public static Number abs(Number a) { if (isFloatingPoint(a)) { return Math.abs(a.doubleValue()); } else { return Math.abs(a.longValue()); } }
From source file:iDynoOptimizer.MOEAFramework26.src.org.moeaframework.util.tree.NumberArithmetic.java
/** * Returns the sign of the number.// w w w. java 2s . c om * * @param a the number * @return the sign of the number * @see Long#signum(long) * @see Math#signum(double) */ public static Number sign(Number a) { if (isFloatingPoint(a)) { return Math.signum(a.doubleValue()); } else { return Long.signum(a.longValue()); } }
From source file:iDynoOptimizer.MOEAFramework26.src.org.moeaframework.util.tree.NumberArithmetic.java
/** * Returns {@code true} if the first number is less than the second; * {@code false} otherwise.// w w w . j a va2 s .c om * * @param a the first number * @param b the second number * @return {@code true} if the first number is less than the second; * {@code false} otherwise */ public static boolean lessThan(Number a, Number b) { if (isFloatingPoint(a) || isFloatingPoint(b)) { return a.doubleValue() < b.doubleValue(); } else { return a.longValue() < b.longValue(); } }
From source file:iDynoOptimizer.MOEAFramework26.src.org.moeaframework.util.tree.NumberArithmetic.java
/** * Returns {@code true} if the first number is greater than the second; * {@code false} otherwise./* ww w . j a va 2 s. co m*/ * * @param a the first number * @param b the second number * @return {@code true} if the first number is greater than the second; * {@code false} otherwise */ public static boolean greaterThan(Number a, Number b) { if (isFloatingPoint(a) || isFloatingPoint(b)) { return a.doubleValue() > b.doubleValue(); } else { return a.longValue() > b.longValue(); } }