SafeNumberOperationUtils { public static boolean safeContains(final Range<Comparable<?>> range, final Comparable<?> endpoint) { try { return range.contains(endpoint); } catch (final ClassCastException ex) { Comparable<?> rangeUpperEndpoint = range.hasUpperBound() ? range.upperEndpoint() : null; Comparable<?> rangeLowerEndpoint = range.hasLowerBound() ? range.lowerEndpoint() : null; Class<?> clazz = getTargetNumericType(Lists.newArrayList(rangeLowerEndpoint, rangeUpperEndpoint, endpoint)); if (clazz == null) { throw ex; } Range<Comparable<?>> newRange = createTargetNumericTypeRange(range, clazz); return newRange.contains(parseNumberByClazz(endpoint.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 assertSafeContainsForInteger() { Range<Comparable<?>> range = Range.closed(12, 100); assertFalse(SafeNumberOperationUtils.safeContains(range, 500)); }
@Test public void assertSafeContainsForLong() { Range<Comparable<?>> range = Range.closed(12L, 1000L); assertTrue(SafeNumberOperationUtils.safeContains(range, 500)); }
@Test public void assertSafeContainsForBigInteger() { Range<Comparable<?>> range = Range.closed(new BigInteger("123"), new BigInteger("1000")); assertTrue(SafeNumberOperationUtils.safeContains(range, 510)); }
@Test public void assertSafeContainsForFloat() { Range<Comparable<?>> range = Range.closed(123.11F, 9999.123F); assertTrue(SafeNumberOperationUtils.safeContains(range, 510.12)); }
@Test public void assertSafeContainsForDouble() { Range<Comparable<?>> range = Range.closed(11.11, 9999.99); assertTrue(SafeNumberOperationUtils.safeContains(range, new BigDecimal("510.12"))); }
@Test public void assertSafeContainsForBigDecimal() { Range<Comparable<?>> range = Range.closed(new BigDecimal("123.11"), new BigDecimal("9999.123")); assertTrue(SafeNumberOperationUtils.safeContains(range, 510.12)); }