public void testSortedMapTransformEntries() { SortedMap<String, String> map = sortedNotNavigable(ImmutableSortedMap.of("a", "4", "b", "9")); EntryTransformer<String, String, String> concat = new EntryTransformer<String, String, String>() { @Override public String transformEntry(String key, String value) { return key + value; } }; SortedMap<String, String> transformed = transformEntries(map, concat); /* * We'd like to sanity check that we didn't get a NavigableMap out, but we * can't easily do so while maintaining GWT compatibility. */ assertEquals(ImmutableSortedMap.of("a", "a4", "b", "b9"), transformed); }
@Override public <ID> int updateFields(String tableName, ID id, Map<String, Object> valueMap) { StringBuilder sqlSb = new StringBuilder(); sqlSb.append("UPDATE `"); sqlSb.append(tableName); sqlSb.append("` SET "); EntryTransformer<String, Object, String> transformer = new EntryTransformer<String, Object, String>() { @Override public String transformEntry(String key, Object value) { String column = CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, key); return "`" + column + "`='" + value + "'"; } }; Map<String, String> transformed = Maps.transformEntries(valueMap, transformer); sqlSb.append(Joiner.on(",").join(transformed.values())); sqlSb.append(" WHERE `id`='" + id + "';"); return jdbcTemplate.update(sqlSb.toString()); }
TransformedEntries( final EntryTransformer<? super K, ? super V1, V2> transformer) { super(fromMultimap.entries(), new Function<Entry<K, V1>, Entry<K, V2>>() { @Override public Entry<K, V2> apply(final Entry<K, V1> entry) { return new AbstractMapEntry<K, V2>() { @Override public K getKey() { return entry.getKey(); } @Override public V2 getValue() { return transformer.transformEntry( entry.getKey(), entry.getValue()); } }; } }); }
@GwtIncompatible("NavigableMap") public void testTransformEntriesSecretlyNavigable() { Map<String, String> map = ImmutableSortedMap.of("a", "4", "b", "9"); EntryTransformer<String, String, String> concat = new EntryTransformer<String, String, String>() { @Override public String transformEntry(String key, String value) { return key + value; } }; Map<String, String> transformed; transformed = transformEntries(map, concat); assertEquals(ImmutableMap.of("a", "a4", "b", "b9"), transformed); assertTrue(transformed instanceof NavigableMap); transformed = transformEntries((SortedMap<String, String>) map, concat); assertEquals(ImmutableMap.of("a", "a4", "b", "b9"), transformed); assertTrue(transformed instanceof NavigableMap); }
Map<K, Collection<V>> createAsMap() { // Select the values that satisify the predicate. EntryTransformer<K, Collection<V>, Collection<V>> transformer = new EntryTransformer<K, Collection<V>, Collection<V>>() { @Override public Collection<V> transformEntry(K key, Collection<V> collection) { return filterCollection(collection, new ValuePredicate(key)); } }; Map<K, Collection<V>> transformed = Maps.transformEntries(unfiltered.asMap(), transformer); // Select the keys that have at least one value remaining. Map<K, Collection<V>> filtered = Maps.filterValues(transformed, NOT_EMPTY); // Override the removal methods, since removing a map entry should not // affect values that don't satisfy the filter. return new AsMap(filtered); }
/** * Creates a new vocabulary backed by the given {@link Enum} class and with * properties having the common URI stem <code>base</code> and prefix * <code>prefix</code> * * @param clazz * the enumeration backing this vocabulary. * @param base * the common stem URI of properties in this vocabulary. * @param prefix * the common prefix of properties in this vocabulary. */ public EnumVocab(final Class<P> clazz, final String base, final String prefix) { this.uri = Strings.nullToEmpty(base); this.index = ImmutableMap .copyOf(Maps.transformEntries(Maps.uniqueIndex(EnumSet.allOf(clazz), ENUM_TO_NAME), new EntryTransformer<String, P, Property>() { @Override public Property transformEntry(String name, P enumee) { return Property.newFrom(name, base, prefix, enumee); } })); }
/** * Scopes the given callable inside a request scope. This is not the same * as the HTTP request scope, but is used if no HTTP request scope is in * progress. In this way, keys can be scoped as @RequestScoped and exist * in non-HTTP requests (for example: RPC requests) as well as in HTTP * request threads. * * <p>The returned callable will throw a {@link ScopingException} when called * if there is a request scope already active on the current thread. * * @param callable code to be executed which depends on the request scope. * Typically in another thread, but not necessarily so. * @param seedMap the initial set of scoped instances for Guice to seed the * request scope with. To seed a key with null, use {@code null} as * the value. * @return a callable that when called will run inside the a request scope * that exposes the instances in the {@code seedMap} as scoped keys. * @since 3.0 */ public static <T> Callable<T> scopeRequest(final Callable<T> callable, Map<Key<?>, Object> seedMap) { Preconditions.checkArgument(null != seedMap, "Seed map cannot be null, try passing in Collections.emptyMap() instead."); // Copy the seed values into our local scope map. final Context context = new Context(); Map<Key<?>, Object> validatedAndCanonicalizedMap = Maps.transformEntries(seedMap, new EntryTransformer<Key<?>, Object, Object>() { @Override public Object transformEntry(Key<?> key, Object value) { return validateAndCanonicalizeValue(key, value); } }); context.map.putAll(validatedAndCanonicalizedMap); return new Callable<T>() { public T call() throws Exception { checkScopingState(null == GuiceFilter.localContext.get(), "An HTTP request is already in progress, cannot scope a new request in this thread."); checkScopingState(null == requestScopeContext.get(), "A request scope is already in progress, cannot scope a new request in this thread."); return context.call(callable); } }; }
@Override Map<K, Collection<V2>> createAsMap() { return Maps.transformEntries( fromMultimap.asMap(), new EntryTransformer<K, Collection<V1>, Collection<V2>>() { @Override public Collection<V2> transformEntry(K key, Collection<V1> value) { return transform(key, value); } }); }
public void testTransformEntries() { Map<String, String> map = ImmutableMap.of("a", "4", "b", "9"); EntryTransformer<String, String, String> concat = new EntryTransformer<String, String, String>() { @Override public String transformEntry(String key, String value) { return key + value; } }; Map<String, String> transformed = transformEntries(map, concat); assertEquals(ImmutableMap.of("a", "a4", "b", "b9"), transformed); }
public void testTransformEntriesExample() { Map<String, Boolean> options = ImmutableMap.of("verbose", true, "sort", false); EntryTransformer<String, Boolean, String> flagPrefixer = new EntryTransformer<String, Boolean, String>() { @Override public String transformEntry(String key, Boolean value) { return value ? key : "no" + key; } }; Map<String, String> transformed = transformEntries(options, flagPrefixer); assertEquals("{verbose=verbose, sort=nosort}", transformed.toString()); }
@GwtIncompatible // NavigableMap public void testNavigableMapTransformEntries() { NavigableMap<String, String> map = ImmutableSortedMap.of("a", "4", "b", "9"); EntryTransformer<String, String, String> concat = new EntryTransformer<String, String, String>() { @Override public String transformEntry(String key, String value) { return key + value; } }; NavigableMap<String, String> transformed = transformEntries(map, concat); assertEquals(ImmutableSortedMap.of("a", "a4", "b", "b9"), transformed); }
@Override Map<K, Collection<V2>> createAsMap() { return Maps.transformEntries(fromMultimap.asMap(), new EntryTransformer<K, Collection<V1>, Collection<V2>>() { @Override public Collection<V2> transformEntry(K key, Collection<V1> value) { return transform(key, value); } }); }
@Override protected RequestRouter createRequestRouter(final Registry registry, final GlobalConfiguration globalConfiguration) { final Dispatcher dispatcher = createDispatcher(globalConfiguration.getDispatcherClass()); return new RequestRouter(registry, globalConfiguration, dispatcher) { @Override public void processPollRequest(final Reader reader, final Writer writer, final String pathInfo) throws IOException { new PollRequestProcessor(registry, dispatcher, globalConfiguration) { @Override // HACK: we determine parameters from request not by reading request content as request content could had // been already read exactly for getting the params, case when request content is already empty protected Object[] getParameters() { if (reader instanceof RequestBoundReader) { ServletRequest request = ((RequestBoundReader) reader).getRequest(); Map<String, String[]> parameterMap = request.getParameterMap(); Map<String, String> parameters = Maps.newHashMap(); if (parameterMap != null) { parameters = Maps.transformEntries(parameterMap, new EntryTransformer<String, String[], String>() { @Override public String transformEntry(@Nullable final String key, @Nullable final String[] values) { return values == null || values.length == 0 ? null : values[0]; } }); } return new Object[]{parameters}; } return super.getParameters(); } }.process(reader, writer, pathInfo); } }; }
/** * @param brokers in multiple clusters, keyed by cluster id * @param topic * @return Get the partition metadata list for the specific topic via the brokers * null if topic is not found */ public static Map<String, List<PartitionMetadata>> getPartitionsForTopic(SetMultimap<String, String> brokers, final String topic) { return Maps.transformEntries(brokers.asMap(), new EntryTransformer<String, Collection<String>, List<PartitionMetadata>>() { @Override public List<PartitionMetadata> transformEntry(String key, Collection<String> bs) { return getPartitionsForTopic(new HashSet<String>(bs), topic); } }); }
private EntryTransformer<String, Object, PropertyValue> toPropertyValue() { return new EntryTransformer<String, Object, PropertyValue>() { @Override public PropertyValue transformEntry(String paramName, Object value) { return propertyValues.containsKey(paramName) ? propertyValues.get(paramName) : createPropertyValue(getParameterType(paramName), value); } }; }
@Override Map<K, Collection<V2>> createAsMap() { return Maps.transformEntries(fromMultimap.asMap(), new EntryTransformer<K, Collection<V1>, Collection<V2>>() { @Override public Collection<V2> transformEntry( K key, Collection<V1> value) { return transform(key, value); } }); }
static <K, V1, V2> SortedMap<K, V2> mapsTransformEntriesSortedMap( SortedMap<K, V1> fromMap, EntryTransformer<? super K, ? super V1, V2> transformer) { return (fromMap instanceof NavigableMap) ? Maps.transformEntries((NavigableMap<K, V1>) fromMap, transformer) : Maps.transformEntriesIgnoreNavigable(fromMap, transformer); }
@Override public Map<K, Collection<V2>> asMap() { if (asMap == null) { Map<K, Collection<V2>> aM = Maps.transformEntries(fromMultimap.asMap(), new EntryTransformer<K, Collection<V1>, Collection<V2>>() { @Override public Collection<V2> transformEntry( K key, Collection<V1> value) { return transform(key, value); } }); asMap = aM; return aM; } return asMap; }
public void testTransformEntriesSecretlySorted() { Map<String, String> map = ImmutableSortedMap.of("a", "4", "b", "9"); EntryTransformer<String, String, String> concat = new EntryTransformer<String, String, String>() { @Override public String transformEntry(String key, String value) { return key + value; } }; Map<String, String> transformed = transformEntries(map, concat); assertEquals(ImmutableMap.of("a", "a4", "b", "b9"), transformed); assertTrue(transformed instanceof SortedMap); }