NumberTypeImpl implements NumberType<NUMBER> { @Override @SuppressWarnings("unchecked") public NUMBER valueOf(Number number, boolean failIfUnprecise) throws NumberConversionException { if (number == null) { return null; } if (getNumberClass().equals(number.getClass())) { return (NUMBER) number; } NUMBER converted = convert(number); if (failIfUnprecise) { double delta = number.doubleValue() - converted.doubleValue(); if (delta == 0) { delta = number.longValue() - converted.longValue(); } if (delta < 0) { delta = -delta; } if (delta > REQUIRED_PRECISION) { throw new NumberConversionException(number, getNumberClass()); } } return converted; } NumberTypeImpl(int exactness, NUMBER min, NUMBER max); @Override @SuppressWarnings("unchecked") NUMBER valueOf(Number number, boolean failIfUnprecise); @Override NUMBER valueOf(String number); @Override int getExactnessDifference(NumberType<?> otherType); @Override NUMBER getMinimumValue(); @Override NUMBER getMaximumValue(); static final NumberTypeImpl<Byte> BYTE; static final NumberTypeImpl<Short> SHORT; static final NumberTypeImpl<Integer> INTEGER; static final NumberTypeImpl<Long> LONG; static final NumberTypeImpl<Float> FLOAT; static final NumberTypeImpl<Double> DOUBLE; static final NumberTypeImpl<BigInteger> BIG_INTEGER; static final NumberTypeImpl<BigDecimal> BIG_DECIMAL; }
@Test public void testByte() { NumberType<Byte> numberType = NumberTypeImpl.BYTE; assertEquals(Byte.class, numberType.getNumberClass()); assertFalse(numberType.isDecimal()); byte b = 123; Byte value = Byte.valueOf(b); checkNumberType(numberType, value); checkNonDecimalNumberType(numberType); }
@Test public void testShort() { NumberType<Short> numberType = NumberTypeImpl.SHORT; assertEquals(Short.class, numberType.getNumberClass()); assertFalse(numberType.isDecimal()); short s = 12345; Short value = Short.valueOf(s); checkNumberType(numberType, value); checkNonDecimalNumberType(numberType); }
@Test public void testInteger() { NumberType<Integer> numberType = NumberTypeImpl.INTEGER; assertEquals(Integer.class, numberType.getNumberClass()); assertFalse(numberType.isDecimal()); Integer value = Integer.valueOf(1234567890); checkNumberType(numberType, value); checkNonDecimalNumberType(numberType); }
@Test public void testAtomicInteger() { NumberType<AtomicInteger> numberType = MathUtilImpl.ATOMIC_INTEGER; assertEquals(AtomicInteger.class, numberType.getNumberClass()); assertFalse(numberType.isDecimal()); AtomicInteger value = new AtomicInteger(1234567890); AtomicInteger parsed = numberType.valueOf(value.toString()); assertEquals(value.intValue(), parsed.intValue()); Double d = Double.valueOf(value.doubleValue()); AtomicInteger converted = numberType.valueOf(d, true); assertEquals(value.intValue(), converted.intValue()); AtomicInteger same = numberType.valueOf(value, true); assertSame(value, same); checkNonDecimalNumberType(numberType); }
@Test public void testLong() { NumberType<Long> numberType = NumberTypeImpl.LONG; assertEquals(Long.class, numberType.getNumberClass()); assertFalse(numberType.isDecimal()); Long value = Long.valueOf(1234567890123456L); checkNumberType(numberType, value); checkNonDecimalNumberType(numberType); }
@Test public void testAtomicLong() { NumberType<AtomicLong> numberType = MathUtilImpl.ATOMIC_LONG; assertEquals(AtomicLong.class, numberType.getNumberClass()); assertFalse(numberType.isDecimal()); AtomicLong value = new AtomicLong(1234567890123456L); AtomicLong parsed = numberType.valueOf(value.toString()); assertEquals(value.longValue(), parsed.longValue()); Double d = Double.valueOf(value.doubleValue()); AtomicLong converted = numberType.valueOf(d, true); assertEquals(value.longValue(), converted.longValue()); AtomicLong same = numberType.valueOf(value, true); assertSame(value, same); checkNonDecimalNumberType(numberType); }
@Test public void testBigInteger() { NumberType<BigInteger> numberType = NumberTypeImpl.BIG_INTEGER; assertEquals(BigInteger.class, numberType.getNumberClass()); assertFalse(numberType.isDecimal()); BigInteger value = BigInteger.valueOf(1234567890123456L); checkNumberType(numberType, value); checkNonDecimalNumberType(numberType); value = new BigInteger("1234567890123456789012345678901234567890"); BigInteger parsed = numberType.valueOf(value.toString()); assertEquals(value, parsed); NumberType<Long> longType = NumberTypeImpl.LONG; try { longType.valueOf(value, true); fail("exception expected"); } catch (NumberConversionException e) { } }
@Test public void testFloat() { NumberType<Float> numberType = NumberTypeImpl.FLOAT; assertEquals(Float.class, numberType.getNumberClass()); assertTrue(numberType.isDecimal()); Float value = Float.valueOf(123456789.123456789F); checkNumberType(numberType, value); Double d = Double.valueOf(42.00001); Float converted = numberType.valueOf(d, false); assertEquals(42, converted.longValue()); try { converted = numberType.valueOf(d, true); fail("exception expected"); } catch (NumberConversionException e) { } d = Double.valueOf(0.00001); converted = numberType.valueOf(d, true); assertEquals(d.floatValue(), converted.floatValue(), 0); }
@Test public void testDouble() { NumberType<Double> numberType = NumberTypeImpl.DOUBLE; assertEquals(Double.class, numberType.getNumberClass()); assertTrue(numberType.isDecimal()); Double value = Double.valueOf(123456789.123456789); checkNumberType(numberType, value); Float f = Float.valueOf(42.00001F); Double converted = numberType.valueOf(f, true); assertEquals(f.doubleValue(), converted.doubleValue(), 0); }
@Test public void testBigDecimal() { NumberType<BigDecimal> numberType = NumberTypeImpl.BIG_DECIMAL; assertEquals(BigDecimal.class, numberType.getNumberClass()); assertTrue(numberType.isDecimal()); BigDecimal value = BigDecimal.valueOf(123456789.123456789); checkNumberType(numberType, value); Double d = Double.valueOf(42.00001F); BigDecimal converted = numberType.valueOf(d, true); assertEquals(d.doubleValue(), converted.doubleValue(), 0); value = new BigDecimal("1234567890123456789.012345678901234567890"); BigDecimal parsed = numberType.valueOf(value.toString()); assertEquals(value, parsed); NumberType<Double> doubleType = NumberTypeImpl.DOUBLE; Double convertedDouble = doubleType.valueOf(value, false); assertEquals(value.doubleValue(), convertedDouble.doubleValue(), 0); try { doubleType.valueOf(value, true); } catch (NumberConversionException e) { } }