Ranges { public static <C extends Comparable> C center(final Range<C> range) { checkNotNull(range); if (!range.hasLowerBound() && !range.hasUpperBound()) { throw new IllegalStateException("cannot find the center of a range without bounds"); } if (!range.hasLowerBound()) { return range.upperEndpoint(); } if (!range.hasUpperBound()) { return range.lowerEndpoint(); } C lowerEndpoint = range.lowerEndpoint(); C upperEndpoint = range.upperEndpoint(); if (upperEndpoint instanceof Integer) { Integer upper = (Integer) upperEndpoint; Integer lower = (Integer) lowerEndpoint; return (C) Integer.valueOf((upper.intValue() + lower.intValue()) / 2); } if (upperEndpoint instanceof Long) { Long upper = (Long) upperEndpoint; Long lower = (Long) lowerEndpoint; return (C) Long.valueOf((upper.longValue() + lower.longValue()) / 2L); } if (upperEndpoint instanceof BigInteger) { BigInteger upper = (BigInteger) upperEndpoint; BigInteger lower = (BigInteger) lowerEndpoint; BigInteger two = BigInteger.valueOf(2L); return (C) upper.subtract(lower).divide(two); } throw new IllegalStateException("cannot find the center of a range whose endpoint type is not Integer, Long, or BigInteger"); } private  Ranges(); static C center(final Range<C> range); static boolean intersect(final Range<C> range0, final Range<C> range1); static boolean isLessThan(final Range<C> range, final C value); static boolean isGreaterThan(final Range<C> range, final C value); static Ordering<Range<C>> orderingByLowerEndpoint(); static Ordering<Range<C>> reverseOrderingByLowerEndpoint(); static Ordering<Range<C>> orderingByUpperEndpoint(); static Ordering<Range<C>> reverseOrderingByUpperEndpoint();  }
@Test public void testCenterNoUpperBound() { assertEquals(Long.valueOf(42L), center(Range.downTo(42L, BoundType.CLOSED))); }
@Test public void testCenterInteger() { assertEquals(Integer.valueOf(10), center(Range.closed(0, 20))); }
@Test public void testCenterLong() { assertEquals(Long.valueOf(10L), center(Range.closed(0L, 20L))); }
@Test public void testCenterBigInteger() { assertEquals(BigInteger.valueOf(10L), center(Range.closed(BigInteger.valueOf(0L), BigInteger.valueOf(20L)))); }
@Test(expected=NullPointerException.class) public void testCenterNull() { center(null); }
@Test(expected=IllegalStateException.class) public void testCenterNoBounds() { center(Range.<Integer>all()); }
@Test public void testCenterNoLowerBound() { assertEquals(Long.valueOf(42L), center(Range.upTo(42L, BoundType.CLOSED))); }