@Test public void testFilter() { NavigableMap<Integer, Integer> map = new TreeMap<>(); map.put(3, 3); map.put(1, 1); map.put(2, 2); Map<Integer, Integer> subMap = RangeUtil.filter(map, Range.<Integer> all()); Assert.assertEquals(subMap.size(), 3); subMap = RangeUtil.filter(map, Range.atLeast(2)); Assert.assertEquals(subMap.size(), 2); subMap = RangeUtil.filter(map, Range.greaterThan(2)); Assert.assertEquals(subMap.size(), 1); subMap = RangeUtil.filter(map, Range.greaterThan(0)); Assert.assertEquals(subMap.size(), 3); subMap = RangeUtil.filter(map, Range.greaterThan(5)); Assert.assertEquals(subMap.size(), 0); subMap = RangeUtil.filter(map, Range.atMost(2)); Assert.assertEquals(subMap.size(), 2); subMap = RangeUtil.filter(map, Range.lessThan(2)); Assert.assertEquals(subMap.size(), 1); subMap = RangeUtil.filter(map, Range.lessThan(5)); Assert.assertEquals(subMap.size(), 3); subMap = RangeUtil.filter(map, Range.lessThan(0)); Assert.assertEquals(subMap.size(), 0); }
public static <C extends Comparable<?>, V> NavigableMap<C, V> filter(NavigableMap<C, V> values, Range<C> filterRange) { if (filterRange == null || filterRange.isEmpty()) { return Maps.newTreeMap(); } else if (filterRange.equals(Range.all())) { return values; } if (filterRange.hasUpperBound() && !filterRange.hasLowerBound()) { return values.headMap(filterRange.upperEndpoint(), upperBoundInclusive(filterRange)); } else if (filterRange.hasLowerBound() && !filterRange.hasUpperBound()) { return values.tailMap(filterRange.lowerEndpoint(), lowerBoundInclusive(filterRange)); } else { return values.subMap(filterRange.lowerEndpoint(), lowerBoundInclusive(filterRange), filterRange.upperEndpoint(), upperBoundInclusive(filterRange)); } }
RangeUtil { public static <C extends Comparable<?>, V> NavigableMap<C, V> filter(NavigableMap<C, V> values, Range<C> filterRange) { if (filterRange == null || filterRange.isEmpty()) { return Maps.newTreeMap(); } else if (filterRange.equals(Range.all())) { return values; } if (filterRange.hasUpperBound() && !filterRange.hasLowerBound()) { return values.headMap(filterRange.upperEndpoint(), upperBoundInclusive(filterRange)); } else if (filterRange.hasLowerBound() && !filterRange.hasUpperBound()) { return values.tailMap(filterRange.lowerEndpoint(), lowerBoundInclusive(filterRange)); } else { return values.subMap(filterRange.lowerEndpoint(), lowerBoundInclusive(filterRange), filterRange.upperEndpoint(), upperBoundInclusive(filterRange)); } } }
RangeUtil { public static <C extends Comparable<?>, V> NavigableMap<C, V> filter(NavigableMap<C, V> values, Range<C> filterRange) { if (filterRange == null || filterRange.isEmpty()) { return Maps.newTreeMap(); } else if (filterRange.equals(Range.all())) { return values; } if (filterRange.hasUpperBound() && !filterRange.hasLowerBound()) { return values.headMap(filterRange.upperEndpoint(), upperBoundInclusive(filterRange)); } else if (filterRange.hasLowerBound() && !filterRange.hasUpperBound()) { return values.tailMap(filterRange.lowerEndpoint(), lowerBoundInclusive(filterRange)); } else { return values.subMap(filterRange.lowerEndpoint(), lowerBoundInclusive(filterRange), filterRange.upperEndpoint(), upperBoundInclusive(filterRange)); } } private  RangeUtil(); }
RangeUtil { public static <C extends Comparable<?>, V> NavigableMap<C, V> filter(NavigableMap<C, V> values, Range<C> filterRange) { if (filterRange == null || filterRange.isEmpty()) { return Maps.newTreeMap(); } else if (filterRange.equals(Range.all())) { return values; } if (filterRange.hasUpperBound() && !filterRange.hasLowerBound()) { return values.headMap(filterRange.upperEndpoint(), upperBoundInclusive(filterRange)); } else if (filterRange.hasLowerBound() && !filterRange.hasUpperBound()) { return values.tailMap(filterRange.lowerEndpoint(), lowerBoundInclusive(filterRange)); } else { return values.subMap(filterRange.lowerEndpoint(), lowerBoundInclusive(filterRange), filterRange.upperEndpoint(), upperBoundInclusive(filterRange)); } } private  RangeUtil(); static NavigableMap<C, V> filter(NavigableMap<C, V> values, Range<C> filterRange); static boolean lowerBoundInclusive(Range<C> range); static boolean upperBoundInclusive(Range<C> range); static Range<C> merge(Range<C> a, Range<C> b); static List<Range<C>> remove(Range<C> self, Range<C> other); static String formatTsRange(Range<Long> tsRange); static ArrayList<Range<Integer>> buildRanges(SortedSet<Integer> values); }
RangeUtil { public static <C extends Comparable<?>, V> NavigableMap<C, V> filter(NavigableMap<C, V> values, Range<C> filterRange) { if (filterRange == null || filterRange.isEmpty()) { return Maps.newTreeMap(); } else if (filterRange.equals(Range.all())) { return values; } if (filterRange.hasUpperBound() && !filterRange.hasLowerBound()) { return values.headMap(filterRange.upperEndpoint(), upperBoundInclusive(filterRange)); } else if (filterRange.hasLowerBound() && !filterRange.hasUpperBound()) { return values.tailMap(filterRange.lowerEndpoint(), lowerBoundInclusive(filterRange)); } else { return values.subMap(filterRange.lowerEndpoint(), lowerBoundInclusive(filterRange), filterRange.upperEndpoint(), upperBoundInclusive(filterRange)); } } private  RangeUtil(); static NavigableMap<C, V> filter(NavigableMap<C, V> values, Range<C> filterRange); static boolean lowerBoundInclusive(Range<C> range); static boolean upperBoundInclusive(Range<C> range); static Range<C> merge(Range<C> a, Range<C> b); static List<Range<C>> remove(Range<C> self, Range<C> other); static String formatTsRange(Range<Long> tsRange); static ArrayList<Range<Integer>> buildRanges(SortedSet<Integer> values);  }