SafeNumberOperationUtils { public static Range<Comparable<?>> safeIntersection(final Range<Comparable<?>> range, final Range<Comparable<?>> connectedRange) { try { return range.intersection(connectedRange); } catch (final ClassCastException ex) { Comparable<?> rangeLowerEndpoint = range.hasLowerBound() ? range.lowerEndpoint() : null; Comparable<?> rangeUpperEndpoint = range.hasUpperBound() ? range.upperEndpoint() : null; Comparable<?> connectedRangeLowerEndpoint = connectedRange.hasLowerBound() ? connectedRange.lowerEndpoint() : null; Comparable<?> connectedRangeUpperEndpoint = connectedRange.hasUpperBound() ? connectedRange.upperEndpoint() : null; Class<?> clazz = getTargetNumericType(Lists.newArrayList(rangeLowerEndpoint, rangeUpperEndpoint, connectedRangeLowerEndpoint, connectedRangeUpperEndpoint)); if (clazz == null) { throw ex; } Range<Comparable<?>> newRange = createTargetNumericTypeRange(range, clazz); Range<Comparable<?>> newConnectedRange = createTargetNumericTypeRange(connectedRange, clazz); return newRange.intersection(newConnectedRange); } }  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 assertSafeIntersectionForInteger() { Range<Comparable<?>> range = Range.closed(10, 2000); Range<Comparable<?>> connectedRange = Range.closed(1500, 4000); Range<Comparable<?>> newRange = SafeNumberOperationUtils.safeIntersection(range, connectedRange); assertThat(newRange.lowerEndpoint(), is(1500)); assertThat(newRange.lowerBoundType(), is(BoundType.CLOSED)); assertThat(newRange.upperEndpoint(), is(2000)); assertThat(newRange.upperBoundType(), is(BoundType.CLOSED)); }
@Test public void assertSafeIntersectionForLong() { Range<Comparable<?>> range = Range.upTo(3147483647L, BoundType.OPEN); Range<Comparable<?>> connectedRange = Range.downTo(3, BoundType.OPEN); Range<Comparable<?>> newRange = SafeNumberOperationUtils.safeIntersection(range, connectedRange); assertThat(newRange.lowerEndpoint(), is(3L)); assertThat(newRange.lowerBoundType(), is(BoundType.OPEN)); assertThat(newRange.upperEndpoint(), is(3147483647L)); assertThat(newRange.upperBoundType(), is(BoundType.OPEN)); }
@Test public void assertSafeIntersectionForBigInteger() { Range<Comparable<?>> range = Range.upTo(new BigInteger("131323233123211"), BoundType.CLOSED); Range<Comparable<?>> connectedRange = Range.downTo(35, BoundType.OPEN); Range<Comparable<?>> newRange = SafeNumberOperationUtils.safeIntersection(range, connectedRange); assertThat(newRange.lowerEndpoint(), is(new BigInteger("35"))); assertThat(newRange.lowerBoundType(), is(BoundType.OPEN)); assertThat(newRange.upperEndpoint(), is(new BigInteger("131323233123211"))); assertThat(newRange.upperBoundType(), is(BoundType.CLOSED)); }
@Test public void assertSafeIntersectionForFloat() { Range<Comparable<?>> range = Range.closed(5.5F, 13.8F); Range<Comparable<?>> connectedRange = Range.closed(7.14F, 11.3F); Range<Comparable<?>> newRange = SafeNumberOperationUtils.safeIntersection(range, connectedRange); assertThat(newRange.lowerEndpoint(), is(7.14F)); assertThat(newRange.lowerBoundType(), is(BoundType.CLOSED)); assertThat(newRange.upperEndpoint(), is(11.3F)); assertThat(newRange.upperBoundType(), is(BoundType.CLOSED)); }
@Test public void assertSafeIntersectionForDouble() { Range<Comparable<?>> range = Range.closed(1242.114, 31474836.12); Range<Comparable<?>> connectedRange = Range.downTo(567.34F, BoundType.OPEN); Range<Comparable<?>> newRange = SafeNumberOperationUtils.safeIntersection(range, connectedRange); assertThat(newRange.lowerEndpoint(), is(1242.114)); assertThat(newRange.lowerBoundType(), is(BoundType.CLOSED)); assertThat(newRange.upperEndpoint(), is(31474836.12)); assertThat(newRange.upperBoundType(), is(BoundType.CLOSED)); }
@Test public void assertSafeIntersectionForBigDecimal() { Range<Comparable<?>> range = Range.upTo(new BigDecimal("2331.23211"), BoundType.CLOSED); Range<Comparable<?>> connectedRange = Range.open(135.13F, 45343.23F); Range<Comparable<?>> newRange = SafeNumberOperationUtils.safeIntersection(range, connectedRange); assertThat(newRange.lowerEndpoint(), is(new BigDecimal("135.13"))); assertThat(newRange.lowerBoundType(), is(BoundType.OPEN)); assertThat(newRange.upperEndpoint(), is(new BigDecimal("2331.23211"))); assertThat(newRange.upperBoundType(), is(BoundType.CLOSED)); }