/** * Calls the smallest subMap overload that filters out the extreme values. This method is * overridden in NavigableMapTestSuiteBuilder. */ SortedMap<K, V> createSubMap(SortedMap<K, V> map, K firstExclusive, K lastExclusive) { if (from == Bound.NO_BOUND && to == Bound.EXCLUSIVE) { return map.headMap(lastExclusive); } else if (from == Bound.INCLUSIVE && to == Bound.NO_BOUND) { return map.tailMap(firstInclusive); } else if (from == Bound.INCLUSIVE && to == Bound.EXCLUSIVE) { return map.subMap(firstInclusive, lastExclusive); } else { throw new IllegalArgumentException(); } }
@Override public SortedMap<K, V> tailMap(K fromKey) { return delegate().tailMap(fromKey); }
/** * Creates a sub map by using the given range (both ends inclusive). */ public static <V> SortedMap<Integer,V> filter(SortedMap<Integer,V> map, String from, String to) { if(from==null && to==null) return map; if(to==null) return map.headMap(Integer.parseInt(from)-1); if(from==null) return map.tailMap(Integer.parseInt(to)); return map.subMap(Integer.parseInt(to),Integer.parseInt(from)-1); }
/** * Creates a sub map by using the given range (upper end inclusive). */ @Restricted(NoExternalUse.class) public static <V> SortedMap<Integer,V> filterExcludingFrom(SortedMap<Integer,V> map, String from, String to) { if(from==null && to==null) return map; if(to==null) return map.headMap(Integer.parseInt(from)); if(from==null) return map.tailMap(Integer.parseInt(to)); return map.subMap(Integer.parseInt(to),Integer.parseInt(from)); }
@CollectionSize.Require(absent = ZERO) public void testTailMapInclusive() { assertTrue(navigableMap.tailMap(a.getKey()).containsKey(a.getKey())); }
@Override public SortedMap<K, V> tailMap(K fromKey) { synchronized (mutex) { return sortedMap(delegate().tailMap(fromKey), mutex); } }
@Override public SortedMap<K, V> tailMap(K fromKey) { return new FilteredEntrySortedMap<>(sortedMap().tailMap(fromKey), predicate); } }
@Override public SortedMap<K, V2> tailMap(K fromKey) { return transformEntries(fromMap().tailMap(fromKey), transformer); } }
@Override public SortedSet<K> tailSet(K fromElement) { return new SortedKeySet<>(map().tailMap(fromElement)); }
/** * A sensible definition of {@link #containsKey} in terms of the {@code firstKey()} method of * {@link #tailMap}. If you override {@link #tailMap}, you may wish to override {@link * #containsKey} to forward to this implementation. * * @since 7.0 */ @Override @Beta protected boolean standardContainsKey(@Nullable Object key) { try { // any CCE will be caught @SuppressWarnings("unchecked") SortedMap<Object, V> self = (SortedMap<Object, V>) this; Object ceilingKey = self.tailMap(key).firstKey(); return unsafeCompare(ceilingKey, key) == 0; } catch (ClassCastException | NoSuchElementException | NullPointerException e) { return false; } }
public void testTailMap() { List<Entry<K, V>> entries = Helpers.copyToList( getSubjectGenerator() .getSampleElements(getSubjectGenerator().getCollectionSize().getNumElements())); Collections.sort(entries, Helpers.<K, V>entryComparator(navigableMap.comparator())); for (int i = 0; i < entries.size(); i++) { assertEqualInOrder( entries.subList(i, entries.size()), navigableMap.tailMap(entries.get(i).getKey()).entrySet()); } }
public void testHeadSubTailMap_FilteredMap() { SortedMap<String, Integer> unfiltered = createUnfiltered(); unfiltered.put("apple", 2); unfiltered.put("banana", 6); unfiltered.put("cat", 4); unfiltered.put("dog", 3); SortedMap<String, Integer> filtered = Maps.filterEntries(unfiltered, CORRECT_LENGTH); assertEquals(ImmutableMap.of("banana", 6), filtered.headMap("dog")); assertEquals(ImmutableMap.of(), filtered.headMap("banana")); assertEquals(ImmutableMap.of("banana", 6, "dog", 3), filtered.headMap("emu")); assertEquals(ImmutableMap.of("banana", 6), filtered.subMap("banana", "dog")); assertEquals(ImmutableMap.of("dog", 3), filtered.subMap("cat", "emu")); assertEquals(ImmutableMap.of("dog", 3), filtered.tailMap("cat")); assertEquals(ImmutableMap.of("banana", 6, "dog", 3), filtered.tailMap("banana")); } }
public void testTreeMultimapAsMapSorted() { TreeMultimap<String, Integer> multimap = createPopulate(); SortedMap<String, Collection<Integer>> asMap = multimap.asMap(); assertEquals(Ordering.natural(), asMap.comparator()); assertEquals("foo", asMap.firstKey()); assertEquals("tree", asMap.lastKey()); Set<Integer> fooValues = ImmutableSet.of(1, 3, 7); Set<Integer> googleValues = ImmutableSet.of(2, 6); Set<Integer> treeValues = ImmutableSet.of(4, 0); assertEquals(ImmutableMap.of("google", googleValues, "tree", treeValues), asMap.tailMap("g")); assertEquals(ImmutableMap.of("google", googleValues, "foo", fooValues), asMap.headMap("h")); assertEquals(ImmutableMap.of("google", googleValues), asMap.subMap("g", "h")); }
@Override protected Map<String, Map<Integer, Character>> makeEmptyMap() { return makeTable().rowMap().tailMap("b"); }
public void testRowEntrySetContains() { table = sortedTable = create( "a", 2, 'X', "a", 2, 'X', "b", 3, 'X', "b", 2, 'X', "c", 10, 'X', "c", 10, 'X', "c", 20, 'X', "d", 15, 'X', "d", 20, 'X', "d", 1, 'X', "e", 5, 'X'); SortedMap<Integer, Character> row = sortedTable.row("c"); Set<Entry<Integer, Character>> entrySet = row.entrySet(); assertTrue(entrySet.contains(Maps.immutableEntry(10, 'X'))); assertTrue(entrySet.contains(Maps.immutableEntry(20, 'X'))); assertFalse(entrySet.contains(Maps.immutableEntry(15, 'X'))); entrySet = row.tailMap(15).entrySet(); assertFalse(entrySet.contains(Maps.immutableEntry(10, 'X'))); assertTrue(entrySet.contains(Maps.immutableEntry(20, 'X'))); assertFalse(entrySet.contains(Maps.immutableEntry(15, 'X'))); }
public void testRowEntrySetRemove() { table = sortedTable = create( "a", 2, 'X', "a", 2, 'X', "b", 3, 'X', "b", 2, 'X', "c", 10, 'X', "c", 10, 'X', "c", 20, 'X', "d", 15, 'X', "d", 20, 'X', "d", 1, 'X', "e", 5, 'X'); SortedMap<Integer, Character> row = sortedTable.row("c"); Set<Entry<Integer, Character>> entrySet = row.tailMap(15).entrySet(); assertFalse(entrySet.remove(Maps.immutableEntry(10, 'X'))); assertTrue(entrySet.remove(Maps.immutableEntry(20, 'X'))); assertFalse(entrySet.remove(Maps.immutableEntry(15, 'X'))); entrySet = row.entrySet(); assertTrue(entrySet.remove(Maps.immutableEntry(10, 'X'))); assertFalse(entrySet.remove(Maps.immutableEntry(20, 'X'))); assertFalse(entrySet.remove(Maps.immutableEntry(15, 'X'))); }
public void testRowSize() { table = sortedTable = create( "a", 2, 'X', "a", 2, 'X', "b", 3, 'X', "b", 2, 'X', "c", 10, 'X', "c", 10, 'X', "c", 20, 'X', "d", 15, 'X', "d", 20, 'X', "d", 1, 'X', "e", 5, 'X'); SortedMap<Integer, Character> row = sortedTable.row("c"); assertEquals(2, row.size()); assertEquals(1, row.tailMap(15).size()); }
public void testRowKeyMapTailMap() { sortedTable = create("foo", 1, 'a', "bar", 1, 'b', "foo", 3, 'c'); Map<String, Map<Integer, Character>> map = sortedTable.rowMap().tailMap("cat"); assertEquals(1, map.size()); assertEquals(ImmutableMap.of(1, 'a', 3, 'c'), map.get("foo")); map.clear(); assertTrue(map.isEmpty()); assertEquals(Collections.singleton("bar"), sortedTable.rowKeySet()); }
@Override protected Map<String, Map<Integer, Character>> makePopulatedMap() { TreeBasedTable<String, Integer, Character> table = makeTable(); populateTable(table); return table.rowMap().tailMap("b"); }
public void testAsMapSorted() { SortedSet<String> strings = new NonNavigableSortedSet(); Collections.addAll(strings, "one", "two", "three"); SortedMap<String, Integer> map = Maps.asMap(strings, LENGTH_FUNCTION); assertEquals(ImmutableMap.of("one", 3, "two", 3, "three", 5), map); assertEquals(Integer.valueOf(5), map.get("three")); assertNull(map.get("five")); assertThat(map.entrySet()) .containsExactly(mapEntry("one", 3), mapEntry("three", 5), mapEntry("two", 3)) .inOrder(); assertThat(map.tailMap("onea").entrySet()) .containsExactly(mapEntry("three", 5), mapEntry("two", 3)) .inOrder(); assertThat(map.subMap("one", "two").entrySet()) .containsExactly(mapEntry("one", 3), mapEntry("three", 5)) .inOrder(); }