/** * Safely convert the argument to a {@code double} value. The only difference * between this method and {@code Number.doubleValue()} is that Byte values * are handled as unsigned (e.g. range 0 - 255). * * @param number the object to convert * * @return the object value as a {@code double} */ public static double doubleValue(Number number) { ClassInfo ci = ClassInfo.get(number.getClass()); switch (ci) { case BYTE: return (double) (number.intValue() & 0xff); default: return number.doubleValue(); } }
ClassInfo ci = ClassInfo.get(number.getClass()); float value = 0.0F;
ClassInfo ci = ClassInfo.get(number.getClass()); int value = 0;
ClassInfo ci = ClassInfo.get(number.getClass()); long value = 0;
ClassInfo ciIn = ClassInfo.get(highestClass(n1, n2)); ClassInfo ciOut = null; if (type == OpType.COMPARE) {
/** * Determine the lowest ranking class (in terms of numeric precision) * among the arguments * * @param numbers {@code Number} objects to compare * * @return the lowest ranking class, or {@code null} if no arguments were supplied */ public static Class<? extends Number> lowestClass(Number ...numbers) { int minRank = ClassInfo.DOUBLE.getRank() + 1; Class<? extends Number> result = null; for (Number n : numbers) { int rank = ClassInfo.get(n.getClass()).getRank(); if (rank < minRank) { minRank = rank; result = n.getClass(); } } return result; }
/** * Determine the highest ranking class (in terms of numeric precision) * among the arguments * * @param numbers {@code Number} objects to compare * * @return the highest ranking class, or {@code null} if no arguments were supplied */ public static Class<? extends Number> highestClass(Number ...numbers) { int maxRank = -1; Class<? extends Number> result = null; for (Number n : numbers) { int rank = ClassInfo.get(n.getClass()).getRank(); if (rank > maxRank) { maxRank = rank; result = n.getClass(); } } return result; }
/** * Cast the value from {@code number} to the class {@code clazz} * * @param number the number object whose value will be copied * @param clazz the class of the new instance * * @return a new instance of the request class with value {@code number} */ public static Number castNumber(Number number, Class<? extends Number> clazz) { ClassInfo ci = ClassInfo.get(clazz); return newInstance(number, ci, true); }
/** * Return a new instance of class {@code clazz} taking its value from {@code number} * * @param number the number object whose value will be copied * @param clazz the class of the new instance * * @return a new instance of the request class with value {@code number} */ public static Number newInstance(Number number, Class<? extends Number> clazz) { ClassInfo ci = ClassInfo.get(clazz); return newInstance(number, ci, false); }