private static Map<ConfigElement.PathIdentifier, Object> getManagedSubset(Map<ConfigElement.PathIdentifier, Object> m) { return Maps.filterEntries(m, new Predicate<Map.Entry<ConfigElement.PathIdentifier, Object>>() { @Override public boolean apply(@Nullable Map.Entry<ConfigElement.PathIdentifier, Object> entry) { assert entry.getKey().element.isOption(); return ((ConfigOption)entry.getKey().element).isManaged(); } }); }
@Override public NavigableMap<K, V> subMap( K fromKey, boolean fromInclusive, K toKey, boolean toInclusive) { return filterEntries( unfiltered.subMap(fromKey, fromInclusive, toKey, toInclusive), entryPredicate); }
@Override public NavigableMap<K, V> tailMap(K fromKey, boolean inclusive) { return filterEntries(unfiltered.tailMap(fromKey, inclusive), entryPredicate); } }
@Override public NavigableMap<K, V> headMap(K toKey, boolean inclusive) { return filterEntries(unfiltered.headMap(toKey, inclusive), entryPredicate); }
@Override public NavigableMap<K, V> descendingMap() { return filterEntries(unfiltered.descendingMap(), entryPredicate); }
/** * Returns a map containing the mappings in {@code unfiltered} whose values satisfy a predicate. * The returned map is a live view of {@code unfiltered}; changes to one affect the other. * * <p>The resulting map's {@code keySet()}, {@code entrySet()}, and {@code values()} views have * iterators that don't support {@code remove()}, but all other methods are supported by the map * and its views. When given a value that doesn't satisfy the predicate, the map's {@code put()}, * {@code putAll()}, and {@link Entry#setValue} methods throw an {@link IllegalArgumentException}. * * <p>When methods such as {@code removeAll()} and {@code clear()} are called on the filtered map * or its views, only mappings whose values satisfy the filter will be removed from the underlying * map. * * <p>The returned map isn't threadsafe or serializable, even if {@code unfiltered} is. * * <p>Many of the filtered map's methods, such as {@code size()}, iterate across every key/value * mapping in the underlying map and determine which satisfy the filter. When a live view is * <i>not</i> needed, it may be faster to copy the filtered map and use the copy. * * <p><b>Warning:</b> {@code valuePredicate} must be <i>consistent with equals</i>, as documented * at {@link Predicate#apply}. Do not provide a predicate such as {@code * Predicates.instanceOf(ArrayList.class)}, which is inconsistent with equals. */ public static <K, V> Map<K, V> filterValues( Map<K, V> unfiltered, final Predicate<? super V> valuePredicate) { return filterEntries(unfiltered, Maps.<V>valuePredicateOnEntries(valuePredicate)); }
/** * Returns a bimap containing the mappings in {@code unfiltered} whose keys satisfy a predicate. * The returned bimap is a live view of {@code unfiltered}; changes to one affect the other. * * <p>The resulting bimap's {@code keySet()}, {@code entrySet()}, and {@code values()} views have * iterators that don't support {@code remove()}, but all other methods are supported by the bimap * and its views. When given a key that doesn't satisfy the predicate, the bimap's {@code put()}, * {@code forcePut()} and {@code putAll()} methods throw an {@link IllegalArgumentException}. * * <p>When methods such as {@code removeAll()} and {@code clear()} are called on the filtered * bimap or its views, only mappings that satisfy the filter will be removed from the underlying * bimap. * * <p>The returned bimap isn't threadsafe or serializable, even if {@code unfiltered} is. * * <p>Many of the filtered bimap's methods, such as {@code size()}, iterate across every key in * the underlying bimap and determine which satisfy the filter. When a live view is <i>not</i> * needed, it may be faster to copy the filtered bimap and use the copy. * * <p><b>Warning:</b> {@code entryPredicate} must be <i>consistent with equals </i>, as documented * at {@link Predicate#apply}. * * @since 14.0 */ public static <K, V> BiMap<K, V> filterKeys( BiMap<K, V> unfiltered, final Predicate<? super K> keyPredicate) { checkNotNull(keyPredicate); return filterEntries(unfiltered, Maps.<K>keyPredicateOnEntries(keyPredicate)); }
/** * Returns a sorted map containing the mappings in {@code unfiltered} whose values satisfy a * predicate. The returned map is a live view of {@code unfiltered}; changes to one affect the * other. * * <p>The resulting map's {@code keySet()}, {@code entrySet()}, and {@code values()} views have * iterators that don't support {@code remove()}, but all other methods are supported by the map * and its views. When given a value that doesn't satisfy the predicate, the map's {@code put()}, * {@code putAll()}, and {@link Entry#setValue} methods throw an {@link IllegalArgumentException}. * * <p>When methods such as {@code removeAll()} and {@code clear()} are called on the filtered map * or its views, only mappings whose values satisfy the filter will be removed from the underlying * map. * * <p>The returned map isn't threadsafe or serializable, even if {@code unfiltered} is. * * <p>Many of the filtered map's methods, such as {@code size()}, iterate across every key/value * mapping in the underlying map and determine which satisfy the filter. When a live view is * <i>not</i> needed, it may be faster to copy the filtered map and use the copy. * * <p><b>Warning:</b> {@code valuePredicate} must be <i>consistent with equals</i>, as documented * at {@link Predicate#apply}. Do not provide a predicate such as {@code * Predicates.instanceOf(ArrayList.class)}, which is inconsistent with equals. * * @since 11.0 */ public static <K, V> SortedMap<K, V> filterValues( SortedMap<K, V> unfiltered, final Predicate<? super V> valuePredicate) { return filterEntries(unfiltered, Maps.<V>valuePredicateOnEntries(valuePredicate)); }
@Override protected Map<String, String> create(Entry<String, String>[] entries) { Map<String, String> map = Maps.newHashMap(); putEntries(map, entries); map.putAll(ENTRIES_TO_FILTER); map = Maps.filterEntries(map, FILTER_ENTRIES_1); return Maps.filterEntries(map, FILTER_ENTRIES_2); } })
@GET @Path("/properties") @ResourceFilters(ConfigResourceFilter.class) @Produces(MediaType.APPLICATION_JSON) public Map<String, String> getProperties() { Map<String, String> allProperties = Maps.fromProperties(properties); Set<String> hidderProperties = druidServerConfig.getHiddenProperties(); return Maps.filterEntries(allProperties, (entry) -> !hidderProperties.contains(entry.getKey())); }
@Override protected Map<String, String> create(Entry<String, String>[] entries) { Map<String, String> map = Maps.newHashMap(); putEntries(map, entries); map.putAll(ENTRIES_TO_FILTER); return Maps.filterEntries(map, FILTER_ENTRIES); } })
@Override protected NavigableMap<String, String> create(Entry<String, String>[] entries) { NavigableMap<String, String> map = new SafeTreeMap<>(); putEntries(map, entries); map.put("banana", "toast"); map.put("eggplant", "spam"); return Maps.filterEntries(map, FILTER_ENTRIES); } })
public void testFirstAndLastKeyFilteredMap() { SortedMap<String, Integer> unfiltered = createUnfiltered(); unfiltered.put("apple", 2); unfiltered.put("banana", 6); unfiltered.put("cat", 3); unfiltered.put("dog", 5); SortedMap<String, Integer> filtered = Maps.filterEntries(unfiltered, CORRECT_LENGTH); assertEquals("banana", filtered.firstKey()); assertEquals("cat", filtered.lastKey()); }
public void testFilteredEntriesWildCardEntryPredicate() { Map<String, Integer> unfiltered = createUnfiltered(); unfiltered.put("cat", 3); unfiltered.put("dog", 2); unfiltered.put("horse", 5); Predicate<Entry<?, ?>> predicate = new Predicate<Entry<?, ?>>() { @Override public boolean apply(Entry<?, ?> input) { return "cat".equals(input.getKey()) || Integer.valueOf(2) == input.getValue(); } }; Map<String, Integer> filtered = Maps.filterEntries(unfiltered, predicate); assertEquals(ImmutableMap.of("cat", 3, "dog", 2), filtered); } }
public void testFilteredEntriesObjectPredicate() { Map<String, Integer> unfiltered = createUnfiltered(); unfiltered.put("cat", 3); unfiltered.put("dog", 2); unfiltered.put("horse", 5); Predicate<Object> predicate = Predicates.alwaysFalse(); Map<String, Integer> filtered = Maps.filterEntries(unfiltered, predicate); assertTrue(filtered.isEmpty()); }
@Override protected BiMap<String, String> create(Entry<String, String>[] entries) { BiMap<String, String> map = HashBiMap.create(); putEntries(map, entries); map.putAll(ENTRIES_TO_FILTER); return Maps.filterEntries(map, FILTER_ENTRIES); } })
@Override protected SortedMap<String, String> create(Entry<String, String>[] entries) { SortedMap<String, String> map = new NonNavigableSortedMap(); putEntries(map, entries); map.putAll(ENTRIES_TO_FILTER); return Maps.filterEntries(map, FILTER_ENTRIES); } })
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 testFilteredEntriesIllegalPutAll() { Map<String, Integer> unfiltered = createUnfiltered(); unfiltered.put("cat", 3); unfiltered.put("dog", 2); unfiltered.put("horse", 5); Map<String, Integer> filtered = Maps.filterEntries(unfiltered, CORRECT_LENGTH); assertEquals(ImmutableMap.of("cat", 3, "horse", 5), filtered); filtered.put("chicken", 7); assertEquals(ImmutableMap.of("cat", 3, "horse", 5, "chicken", 7), filtered); try { filtered.putAll(ImmutableMap.of("sheep", 5, "cow", 7)); fail(); } catch (IllegalArgumentException expected) { } assertEquals(ImmutableMap.of("cat", 3, "horse", 5, "chicken", 7), filtered); }
public void testFilteredEntriesIllegalPut() { Map<String, Integer> unfiltered = createUnfiltered(); unfiltered.put("cat", 3); unfiltered.put("dog", 2); unfiltered.put("horse", 5); Map<String, Integer> filtered = Maps.filterEntries(unfiltered, CORRECT_LENGTH); assertEquals(ImmutableMap.of("cat", 3, "horse", 5), filtered); filtered.put("chicken", 7); assertEquals(ImmutableMap.of("cat", 3, "horse", 5, "chicken", 7), filtered); try { filtered.put("cow", 7); fail(); } catch (IllegalArgumentException expected) { } assertEquals(ImmutableMap.of("cat", 3, "horse", 5, "chicken", 7), filtered); }