NumberInRange { @ArgumentsChecked @Throws(IllegalNullArgumentException.class) public static boolean isInRange(@Nonnull final Number number, @Nonnull final BigDecimal min, @Nonnull final BigDecimal max) { Check.notNull(number, "number"); Check.notNull(min, "min"); Check.notNull(max, "max"); BigDecimal bigDecimal = null; if (number instanceof Byte || number instanceof Short || number instanceof Integer || number instanceof Long) { bigDecimal = new BigDecimal(number.longValue()); } else if (number instanceof Float || number instanceof Double) { bigDecimal = new BigDecimal(number.doubleValue()); } else if (number instanceof BigInteger) { bigDecimal = new BigDecimal((BigInteger) number); } else if (number instanceof BigDecimal) { bigDecimal = (BigDecimal) number; } else { throw new IllegalNumberArgumentException("Return value is no known subclass of 'java.lang.Number': " + number.getClass().getName()); } return max.compareTo(bigDecimal) >= 0 && min.compareTo(bigDecimal) <= 0; } private  NumberInRange(); @ArgumentsChecked @Throws(IllegalNullArgumentException.class) static byte checkByte(@Nonnull final Number number); @ArgumentsChecked @Throws(IllegalNullArgumentException.class) static double checkDouble(@Nonnull final Number number); @ArgumentsChecked @Throws(IllegalNullArgumentException.class) static float checkFloat(@Nonnull final Number number); @ArgumentsChecked @Throws(IllegalNullArgumentException.class) static int checkInteger(@Nonnull final Number number); @ArgumentsChecked @Throws(IllegalNullArgumentException.class) static int checkLong(@Nonnull final Number number); @ArgumentsChecked @Throws(IllegalNullArgumentException.class) static short checkShort(@Nonnull final Number number); static boolean isInByteRange(@Nonnull final Number number); static boolean isInDoubleRange(@Nonnull final Number number); static boolean isInFloatRange(@Nonnull final Number number); static boolean isInIntegerRange(@Nonnull final Number number); static boolean isInLongRange(@Nonnull final Number number); @ArgumentsChecked @Throws(IllegalNullArgumentException.class) static boolean isInRange(@Nonnull final Number number, @Nonnull final BigDecimal min, @Nonnull final BigDecimal max); @ArgumentsChecked @Throws(IllegalNullArgumentException.class) static boolean isInRange(@Nonnull final Number number, @Nonnull final BigInteger min, @Nonnull final BigInteger max); static boolean isInShortRange(@Nonnull final Number number); static final BigInteger BYTE_MIN; static final BigInteger BYTE_MAX; static final BigInteger SHORT_MIN; static final BigInteger SHORT_MAX; static final BigInteger INTEGER_MIN; static final BigInteger INTEGER_MAX; static final BigInteger LONG_MIN; static final BigInteger LONG_MAX; static final BigDecimal FLOAT_MAX; static final BigDecimal FLOAT_MIN; static final BigDecimal DOUBLE_MAX; static final BigDecimal DOUBLE_MIN; }
@Test(expected=IllegalNumberArgumentException.class) public void testInRange_Integer_Fail() { NumberInRange.isInRange(new FakeNumber(), BigInteger.valueOf(Integer.MIN_VALUE), BigInteger.valueOf(Integer.MAX_VALUE)); }
@Test(expected=IllegalNumberArgumentException.class) public void testInRange_Decimal_Fail() { NumberInRange.isInRange(new FakeNumber(), new BigDecimal(Integer.MIN_VALUE), new BigDecimal(Integer.MAX_VALUE)); }
@Test public void testIsInRange_Byte_Decimal() { Assert.assertEquals(true, NumberInRange.isInRange(Byte.valueOf((byte)4), new BigDecimal(Byte.MIN_VALUE), new BigDecimal(Byte.MAX_VALUE))); }
@Test public void testIsInRange_Byte_Integer() { Assert.assertEquals(true, NumberInRange.isInRange(Byte.valueOf((byte)4), BigInteger.valueOf(Byte.MIN_VALUE), BigInteger.valueOf(Byte.MAX_VALUE))); }
@Test public void testIsInRange_Short_Decimal() { Assert.assertEquals(true, NumberInRange.isInRange(Short.valueOf((short)4), new BigDecimal(Short.MIN_VALUE), new BigDecimal(Short.MAX_VALUE))); }
@Test public void testIsInRange_Short_Decimal_Fail() { Assert.assertEquals(false, NumberInRange.isInRange(Short.valueOf((short)-400), new BigDecimal(Byte.MIN_VALUE), new BigDecimal(Byte.MAX_VALUE))); }
@Test public void testIsInRange_Short_Integer() { Assert.assertEquals(true, NumberInRange.isInRange(Short.valueOf((short)4), BigInteger.valueOf(Short.MIN_VALUE), BigInteger.valueOf(Short.MAX_VALUE))); }
@Test public void testIsInRange_Integer_Decimal() { Assert.assertEquals(true, NumberInRange.isInRange(Integer.valueOf(4), new BigDecimal(Integer.MIN_VALUE), new BigDecimal(Integer.MAX_VALUE))); }
@Test public void testIsInRange_Integer_Integer() { Assert.assertEquals(true, NumberInRange.isInRange(Integer.valueOf(4), BigInteger.valueOf(Integer.MIN_VALUE), BigInteger.valueOf(Integer.MAX_VALUE))); }
@Test public void testIsInRange_Long_Decimal() { Assert.assertEquals(true, NumberInRange.isInRange(Long.valueOf(4l), new BigDecimal(Long.MIN_VALUE), new BigDecimal(Long.MAX_VALUE))); }
@Test public void testIsInRange_Long_Integer() { Assert.assertEquals(true, NumberInRange.isInRange(Long.valueOf(4l), BigInteger.valueOf(Long.MIN_VALUE), BigInteger.valueOf(Long.MAX_VALUE))); }
@Test public void testIsInRange_Float_Decimal() { Assert.assertEquals(true, NumberInRange.isInRange(Float.valueOf(4f), new BigDecimal(Float.MIN_VALUE), new BigDecimal(Float.MAX_VALUE))); }
@Test public void testIsInRange_Float_Integer() { Assert.assertEquals(true, NumberInRange.isInRange(Float.valueOf(4f), BigInteger.valueOf((long)Float.MIN_VALUE), BigInteger.valueOf((long)Float.MAX_VALUE))); }
@Test public void testIsInRange_Double_Decimal() { Assert.assertEquals(true, NumberInRange.isInRange(Double.valueOf(4d), new BigDecimal(Double.MIN_VALUE), new BigDecimal(Double.MAX_VALUE))); }
@Test public void testIsInRange_Double_Integer() { Assert.assertEquals(true, NumberInRange.isInRange(Double.valueOf(4d), BigInteger.valueOf((long)Double.MIN_VALUE), BigInteger.valueOf((long)Double.MAX_VALUE))); }
@Test public void testIsInRange_BigDecimal_Decimal() { Assert.assertEquals(true, NumberInRange.isInRange(new BigDecimal("4"), new BigDecimal(Double.MIN_VALUE), new BigDecimal(Double.MAX_VALUE))); }
@Test public void testIsInRange_BigDecimal_Integer() { Assert.assertEquals(true, NumberInRange.isInRange(new BigDecimal("4"), BigInteger.valueOf((long)Double.MIN_VALUE), BigInteger.valueOf((long)Double.MAX_VALUE))); }
@Test public void testIsInRange_BigInteger_Decimal() { Assert.assertEquals(true, NumberInRange.isInRange(new BigInteger("4"), new BigDecimal(Double.MIN_VALUE), new BigDecimal(Double.MAX_VALUE))); }
@Test public void testIsInRange_BigInteger_Integer() { Assert.assertEquals(true, NumberInRange.isInRange(new BigInteger("4"), BigInteger.valueOf((long)Double.MIN_VALUE), BigInteger.valueOf((long)Double.MAX_VALUE))); }