@Override public V load(K key) { return computingFunction.apply(checkNotNull(key)); }
/** * Returns an immutable map whose keys are the distinct elements of {@code keys} and whose value * for each key was computed by {@code valueFunction}. The map's iteration order is the order of * the first appearance of each key in {@code keys}. * * <p>When there are multiple instances of a key in {@code keys}, it is unspecified whether {@code * valueFunction} will be applied to more than one instance of that key and, if it is, which * result will be mapped to that key in the returned map. * * @throws NullPointerException if any element of {@code keys} is {@code null}, or if {@code * valueFunction} produces {@code null} for any key * @since 14.0 */ public static <K, V> ImmutableMap<K, V> toMap( Iterator<K> keys, Function<? super K, V> valueFunction) { checkNotNull(valueFunction); // Using LHM instead of a builder so as not to fail on duplicate keys Map<K, V> builder = newLinkedHashMap(); while (keys.hasNext()) { K key = keys.next(); builder.put(key, valueFunction.apply(key)); } return ImmutableMap.copyOf(builder); }
Interval interval = intervalFunction.apply(object.getObject()); VersionType version = versionFunction.apply(object.getObject()); Map<VersionType, TimelineEntry> exists = allTimelineEntries.get(interval); TimelineEntry entry; TreeMap<VersionType, TimelineEntry> versionEntry = new TreeMap<>(versionComparator); versionEntry.put(version, entry); allTimelineEntries.put(interval, versionEntry); } else { entry = exists.get(version); exists.put(version, entry); } else { PartitionHolder<ObjectType> partitionHolder = entry.getPartitionHolder();
public static <K extends Comparable<K>, V> void printMap(final PrintStream out, final String name, final Function<V, String> transform, final Map<K, V> values) { out.print(name); boolean first = true; for (final K key : Ordering.natural().sortedCopy(values.keySet())) { if (!first) { out.print(Strings.repeat(" ", name.length())); } final V value = values.get(key); out.printf("%s=%s%n", key, transform.apply(value)); first = false; } if (first) { out.println(); } }
public void testForMapWildCardWithDefault() { Map<String, Integer> map = Maps.newHashMap(); map.put("One", 1); map.put("Three", 3); Number number = Double.valueOf(42); Function<String, Number> function = Functions.forMap(map, number); assertEquals(1, function.apply("One").intValue()); assertEquals(number, function.apply("Two")); assertEquals(3L, function.apply("Three").longValue()); }
@Nullable @Override public Row apply(Row input) { Row preRow = preCompute.apply(input); if (preRow instanceof MapBasedRow) { MapBasedRow preMapRow = (MapBasedRow) preRow; Map<String, Object> event = new HashMap<>(preMapRow.getEvent()); for (String dim : optimizedDims) { final Object eventVal = event.get(dim); event.put(dim, extractionFnMap.get(dim).apply(eventVal)); } return new MapBasedRow(preMapRow.getTimestamp(), event); } else { return preRow; } } };
@Nullable private <C extends ComponentInfo> C removeComponent( ComponentName componentName, filtersMap.remove(componentName); String packageName = componentName.getPackageName(); PackageInfo packageInfo = packageInfos.get(packageName); if (packageInfo == null) { return null; C[] components = componentArrayInPackage.apply(packageInfo); if (components == null) { return null;
@Nullable public File getDataFile(@Nonnull String url) { checkNotNull(url, "url"); String name = metadata.files.get(url); return name != null ? toFileInCache().apply(name) : null; }
/** Actual cache loading function. */ @Override public Object apply(final CacheKey input) { final Function<String, Object> propertyAccessFunction = classBasedHandlers.get(input.ofClass); Preconditions.checkNotNull(propertyAccessFunction); final Object result = propertyAccessFunction.apply(input.key); return result == null ? NULL_VALUE : result; } }));
@Before public void setUp() throws Exception { String className = getClass().getName(); Map<String, Pair<StorageAdapter, Closeable>> adaptersForClass = adapterCache.get().get(className); if (adaptersForClass == null) { adaptersForClass = new HashMap<>(); adapterCache.get().put(className, adaptersForClass); } Pair<StorageAdapter, Closeable> pair = adaptersForClass.get(testName); if (pair == null) { pair = finisher.apply( indexBuilder.tmpDir(temporaryFolder.newFolder()).rows(rows) ); adaptersForClass.put(testName, pair); } this.adapter = pair.lhs; }
throw new IllegalArgumentException("Component needs a name"); PackageInfo packageInfo = packageInfos.get(packageName); if (packageInfo == null) { packageInfo = new PackageInfo(); packageInfo.applicationInfo = newComponent.applicationInfo; installPackage(packageInfo); packageInfo = packageInfos.get(packageName); C[] components = componentArrayInPackage.apply(packageInfo); if (components == null) { @SuppressWarnings("unchecked")
final List<Object> nodeValue = Lists.newArrayListWithExpectedSize(node.size()); for (final JsonNode subnode : node) { final Object subnodeValue = valueFunction.apply(subnode); if (subnodeValue != null) { nodeValue.add(subnodeValue); map.put(StringUtils.toLowerCase(key), nodeValue); // difference from JSONParser parse() } else { final Object nodeValue = valueFunction.apply(node); if (nodeValue != null) { map.put(StringUtils.toLowerCase(key), nodeValue); // difference from JSONParser parse()
/** * Returns an immutable map whose keys are the distinct elements of {@code keys} and whose value * for each key was computed by {@code valueFunction}. The map's iteration order is the order of * the first appearance of each key in {@code keys}. * * <p>When there are multiple instances of a key in {@code keys}, it is unspecified whether {@code * valueFunction} will be applied to more than one instance of that key and, if it is, which * result will be mapped to that key in the returned map. * * @throws NullPointerException if any element of {@code keys} is {@code null}, or if {@code * valueFunction} produces {@code null} for any key * @since 14.0 */ public static <K, V> ImmutableMap<K, V> toMap( Iterator<K> keys, Function<? super K, V> valueFunction) { checkNotNull(valueFunction); // Using LHM instead of a builder so as not to fail on duplicate keys Map<K, V> builder = newLinkedHashMap(); while (keys.hasNext()) { K key = keys.next(); builder.put(key, valueFunction.apply(key)); } return ImmutableMap.copyOf(builder); }
@Override public void forEach(Consumer<? super T> action) { checkNotNull(action); fromIterable.forEach((F f) -> action.accept(function.apply(f))); }
@Override public Object compute(Map<String, Object> values) { // Maps.transformEntries is lazy, will only finalize values we actually read. final Map<String, Object> finalizedValues = Maps.transformEntries( values, (String k, Object v) -> { final Function<Object, Object> finalizer = finalizers.get(k); return finalizer != null ? finalizer.apply(v) : v; } ); return parsed.eval(Parser.withMap(finalizedValues)).value(); }