@Test public void testGetNumbericValueForType() { try { StreamClassUtil.getNumbericValueForType(null, Integer.class); fail(); } catch (IllegalArgumentException e) { Assert.assertTrue(true); } try { StreamClassUtil.getNumbericValueForType(new Integer(1), null); fail(); } catch (IllegalArgumentException e) { Assert.assertTrue(true); } try { Assert.assertEquals(new Integer(1), StreamClassUtil.getNumbericValueForType(new Integer(1), Integer.class)); Assert.assertEquals(new Long(1), StreamClassUtil.getNumbericValueForType(new Integer(1), Long.class)); Assert.assertEquals(new Double(1), StreamClassUtil.getNumbericValueForType(new Integer(1), Double.class)); Assert.assertEquals(new Float(1), StreamClassUtil.getNumbericValueForType(new Integer(1), Float.class)); } catch (IllegalArgumentException e) { fail(); } }
public static Number getNumbericValueForType(Number result, Class< ? > resultType) { if (result == null || resultType == null) { String msg = "Parameter must not null."; LOG.error(msg); throw new IllegalArgumentException(msg); } if (result.getClass() == resultType) { return result; } if (resultType == BigDecimal.class) { if (isFloatingPointNumber(result)) { return new BigDecimal(result.doubleValue()); } return new BigDecimal(result.longValue()); } if (resultType == Double.class) { return result.doubleValue(); } if (resultType == Float.class) { return result.floatValue(); } if (resultType == Long.class) { return result.longValue(); } if (resultType == Integer.class) { return result.intValue(); } LOG.error("The resultType to numberic value error."); throw new StreamingRuntimeException("The resultType to numberic value error."); }
StreamClassUtil { public static Number getNumbericValueForType(Number result, Class< ? > resultType) { if (result == null || resultType == null) { String msg = "Parameter must not null."; LOG.error(msg); throw new IllegalArgumentException(msg); } if (result.getClass() == resultType) { return result; } if (resultType == BigDecimal.class) { if (isFloatingPointNumber(result)) { return new BigDecimal(result.doubleValue()); } return new BigDecimal(result.longValue()); } if (resultType == Double.class) { return result.doubleValue(); } if (resultType == Float.class) { return result.floatValue(); } if (resultType == Long.class) { return result.longValue(); } if (resultType == Integer.class) { return result.intValue(); } LOG.error("The resultType to numberic value error."); throw new StreamingRuntimeException("The resultType to numberic value error."); } }
StreamClassUtil { public static Number getNumbericValueForType(Number result, Class< ? > resultType) { if (result == null || resultType == null) { String msg = "Parameter must not null."; LOG.error(msg); throw new IllegalArgumentException(msg); } if (result.getClass() == resultType) { return result; } if (resultType == BigDecimal.class) { if (isFloatingPointNumber(result)) { return new BigDecimal(result.doubleValue()); } return new BigDecimal(result.longValue()); } if (resultType == Double.class) { return result.doubleValue(); } if (resultType == Float.class) { return result.floatValue(); } if (resultType == Long.class) { return result.longValue(); } if (resultType == Integer.class) { return result.intValue(); } LOG.error("The resultType to numberic value error."); throw new StreamingRuntimeException("The resultType to numberic value error."); }  }
StreamClassUtil { public static Number getNumbericValueForType(Number result, Class< ? > resultType) { if (result == null || resultType == null) { String msg = "Parameter must not null."; LOG.error(msg); throw new IllegalArgumentException(msg); } if (result.getClass() == resultType) { return result; } if (resultType == BigDecimal.class) { if (isFloatingPointNumber(result)) { return new BigDecimal(result.doubleValue()); } return new BigDecimal(result.longValue()); } if (resultType == Double.class) { return result.doubleValue(); } if (resultType == Float.class) { return result.floatValue(); } if (resultType == Long.class) { return result.longValue(); } if (resultType == Integer.class) { return result.intValue(); } LOG.error("The resultType to numberic value error."); throw new StreamingRuntimeException("The resultType to numberic value error."); }  static Class< ? > getWrapType(Class< ? > type); static boolean isNumberic(Class< ? > type); static Class< ? > getArithmaticType(Class< ? > leftType, Class< ? > rightType); static Class< ? > getCommonType(List<Class< ? >> childTypes); static Number getNumbericValueForType(Number result, Class< ? > resultType); static BigDecimal getBigDecimalValue(Number result); static boolean isFloatingPointNumber(Number value); static Class< ? > getCompareType(Class< ? > typeOne, Class< ? > typeTwo); static boolean isBoolean(Class< ? > type); static boolean isDateOrTimestamp(Class< ? > type); }
StreamClassUtil { public static Number getNumbericValueForType(Number result, Class< ? > resultType) { if (result == null || resultType == null) { String msg = "Parameter must not null."; LOG.error(msg); throw new IllegalArgumentException(msg); } if (result.getClass() == resultType) { return result; } if (resultType == BigDecimal.class) { if (isFloatingPointNumber(result)) { return new BigDecimal(result.doubleValue()); } return new BigDecimal(result.longValue()); } if (resultType == Double.class) { return result.doubleValue(); } if (resultType == Float.class) { return result.floatValue(); } if (resultType == Long.class) { return result.longValue(); } if (resultType == Integer.class) { return result.intValue(); } LOG.error("The resultType to numberic value error."); throw new StreamingRuntimeException("The resultType to numberic value error."); }  static Class< ? > getWrapType(Class< ? > type); static boolean isNumberic(Class< ? > type); static Class< ? > getArithmaticType(Class< ? > leftType, Class< ? > rightType); static Class< ? > getCommonType(List<Class< ? >> childTypes); static Number getNumbericValueForType(Number result, Class< ? > resultType); static BigDecimal getBigDecimalValue(Number result); static boolean isFloatingPointNumber(Number value); static Class< ? > getCompareType(Class< ? > typeOne, Class< ? > typeTwo); static boolean isBoolean(Class< ? > type); static boolean isDateOrTimestamp(Class< ? > type);  }