SafeNumberOperationUtils { public static Range<Comparable<?>> safeClosed(final Comparable<?> lowerEndpoint, final Comparable<?> upperEndpoint) { try { return Range.closed(lowerEndpoint, upperEndpoint); } catch (final ClassCastException ex) { Class<?> clazz = getTargetNumericType(Lists.newArrayList(lowerEndpoint, upperEndpoint)); if (clazz == null) { throw ex; } return Range.closed(parseNumberByClazz(lowerEndpoint.toString(), clazz), parseNumberByClazz(upperEndpoint.toString(), clazz)); } }  static Range<Comparable<?>> safeIntersection(final Range<Comparable<?>> range, final Range<Comparable<?>> connectedRange); static Range<Comparable<?>> safeClosed(final Comparable<?> lowerEndpoint, final Comparable<?> upperEndpoint); static boolean safeContains(final Range<Comparable<?>> range, final Comparable<?> endpoint); static boolean safeEquals(final Collection<Comparable<?>> sourceCollection, final Collection<Comparable<?>> targetCollection);  }
@Test public void assertSafeClosedForInteger() { Range<Comparable<?>> range = SafeNumberOperationUtils.safeClosed(12, 500); assertThat(range.lowerEndpoint(), is(12)); assertThat(range.upperEndpoint(), is(500)); }
@Test public void assertSafeClosedForLong() { Range<Comparable<?>> range = SafeNumberOperationUtils.safeClosed(12, 5001L); assertThat(range.lowerEndpoint(), is(12L)); assertThat(range.upperEndpoint(), is(5001L)); }
@Test public void assertSafeClosedForBigInteger() { Range<Comparable<?>> range = SafeNumberOperationUtils.safeClosed(12L, new BigInteger("12344")); assertThat(range.lowerEndpoint(), is(new BigInteger("12"))); assertThat(range.upperEndpoint(), is(new BigInteger("12344"))); }
@Test public void assertSafeClosedForFloat() { Range<Comparable<?>> range = SafeNumberOperationUtils.safeClosed(4.5F, 11.13F); assertThat(range.lowerEndpoint(), is(4.5F)); assertThat(range.upperEndpoint(), is(11.13F)); }
@Test public void assertSafeClosedForDouble() { Range<Comparable<?>> range = SafeNumberOperationUtils.safeClosed(5.12F, 13.75); assertThat(range.lowerEndpoint(), is(5.12)); assertThat(range.upperEndpoint(), is(13.75)); }
@Test public void assertSafeClosedForBigDecimal() { Range<Comparable<?>> range = SafeNumberOperationUtils.safeClosed(5.1F, new BigDecimal("17.666")); assertThat(range.lowerEndpoint(), is(new BigDecimal("5.1"))); assertThat(range.upperEndpoint(), is(new BigDecimal("17.666"))); }