/** * 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(); } }
/** * 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); }
@Override public SortedMap<K, V> headMap(K toKey) { return delegate().headMap(toKey); }
/** * 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 testHeadMapExclusive() { assertFalse(navigableMap.headMap(a.getKey()).containsKey(a.getKey())); }
/** * A sensible default implementation of {@link #subMap(Object, Object)} in terms of {@link * #headMap(Object)} and {@link #tailMap(Object)}. In some situations, you may wish to override * {@link #subMap(Object, Object)} to forward to this implementation. * * @since 7.0 */ @Beta protected SortedMap<K, V> standardSubMap(K fromKey, K toKey) { checkArgument(unsafeCompare(fromKey, toKey) <= 0, "fromKey must be <= toKey"); return tailMap(fromKey).headMap(toKey); } }
/** * increases q(t) by n for t in [start,end). * * @return peak value of q(t) in this range as a result of addition. */ int insert(long start, long end, int n) { splitAt(start); splitAt(end); int peak = 0; for (Map.Entry<Long, int[]> e : data.tailMap(start).headMap(end).entrySet()) { peak = max(peak, e.getValue()[0] += n); } return peak; }
@Override public SortedMap<K, V> headMap(K toKey) { synchronized (mutex) { return sortedMap(delegate().headMap(toKey), mutex); } }
@Override public K lastKey() { SortedMap<K, V> headMap = sortedMap(); while (true) { // correctly throws NoSuchElementException when filtered map is empty. K key = headMap.lastKey(); if (apply(key, unfiltered.get(key))) { return key; } headMap = sortedMap().headMap(key); } }
@Override public SortedMap<K, V2> headMap(K toKey) { return transformEntries(fromMap().headMap(toKey), transformer); }
@Override public SortedSet<K> headSet(K toElement) { return new SortedKeySet<>(map().headMap(toElement)); }
@Override public SortedMap<K, V> headMap(K toKey) { return new FilteredEntrySortedMap<>(sortedMap().headMap(toKey), predicate); }
public void testHeadMap() { 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(0, i), navigableMap.headMap(entries.get(i).getKey()).entrySet()); } }
@Override public K lastKey() { SortedMap<K, V> headMap = sortedMap(); while (true) { // correctly throws NoSuchElementException when filtered map is empty. K key = headMap.lastKey(); if (apply(key, unfiltered.get(key))) { return key; } headMap = sortedMap().headMap(key); } }
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().headMap("x"); }
public void testRowKeyMapHeadMap() { sortedTable = create("foo", 1, 'a', "bar", 1, 'b', "foo", 3, 'c'); Map<String, Map<Integer, Character>> map = sortedTable.rowMap().headMap("cat"); assertEquals(1, map.size()); assertEquals(ImmutableMap.of(1, 'b'), map.get("bar")); map.clear(); assertTrue(map.isEmpty()); assertEquals(Collections.singleton("foo"), sortedTable.rowKeySet()); }
@Override protected Map<String, Map<Integer, Character>> makePopulatedMap() { TreeBasedTable<String, Integer, Character> table = makeTable(); populateTable(table); return table.rowMap().headMap("x"); }
public void testAsMapSortedReadsThrough() { SortedSet<String> strings = new NonNavigableSortedSet(); Collections.addAll(strings, "one", "two", "three"); SortedMap<String, Integer> map = Maps.asMap(strings, LENGTH_FUNCTION); assertNull(map.comparator()); assertEquals(ImmutableSortedMap.of("one", 3, "two", 3, "three", 5), map); assertNull(map.get("four")); strings.add("four"); assertEquals(ImmutableSortedMap.of("one", 3, "two", 3, "three", 5, "four", 4), map); assertEquals(Integer.valueOf(4), map.get("four")); SortedMap<String, Integer> headMap = map.headMap("two"); assertEquals(ImmutableSortedMap.of("four", 4, "one", 3, "three", 5), headMap); strings.add("five"); strings.remove("one"); assertEquals(ImmutableSortedMap.of("five", 4, "four", 4, "three", 5), headMap); assertThat(map.entrySet()) .containsExactly( mapEntry("five", 4), mapEntry("four", 4), mapEntry("three", 5), mapEntry("two", 3)) .inOrder(); }