List of usage examples for com.google.common.collect Maps filterKeys
@CheckReturnValue public static <K, V> BiMap<K, V> filterKeys(BiMap<K, V> unfiltered, final Predicate<? super K> keyPredicate)
From source file:com.google.caliper.runner.instrument.Instrument.java
@VisibleForTesting @Inject/*from www.j a va2 s . c o m*/ public void setOptions(@InstrumentOptions ImmutableMap<String, String> options) { this.options = ImmutableMap.copyOf(Maps.filterKeys(options, Predicates.in(instrumentOptions()))); }
From source file:org.jclouds.ohai.functions.NestSlashKeys.java
@Override public Map<String, JsonBall> apply(Multimap<String, Supplier<JsonBall>> from) { Map<String, JsonBall> autoAttrs = mergeSameKeys(from); Map<String, JsonBall> modifiableFlatMap = Maps .newLinkedHashMap(Maps.filterKeys(autoAttrs, new Predicate<String>() { @Override//from w w w. ja v a2 s . c om public boolean apply(String input) { return input.indexOf('/') == -1; } })); Map<String, JsonBall> withSlashesMap = Maps.difference(autoAttrs, modifiableFlatMap).entriesOnlyOnLeft(); for (Entry<String, JsonBall> entry : withSlashesMap.entrySet()) { List<String> keyParts = Lists.newArrayList(Splitter.on('/').split(entry.getKey())); JsonBall toInsert = entry.getValue(); try { putUnderContext(keyParts, toInsert, modifiableFlatMap); } catch (IllegalArgumentException e) { throw new IllegalArgumentException("error inserting value in entry: " + entry.getKey(), e); } } return modifiableFlatMap; }
From source file:com.google.caliper.runner.worker.trial.VmDataCollectingVisitor.java
@Override public void visit(VmPropertiesLogMessage logMessage) { vmProperties = Optional.of(/*from ww w . ja v a 2 s. co m*/ ImmutableMap.copyOf(Maps.filterKeys(logMessage.properties(), target.vm().vmPropertiesToRetain()))); }
From source file:io.druid.server.lookup.cache.polling.OnHeapPollingCache.java
public OnHeapPollingCache(Iterable<Map.Entry<K, V>> entries) { if (entries == null) { immutableMap = ImmutableMap.of(); immutableReverseMap = ImmutableMap.of(); } else {// w w w . j a va2s . c o m ImmutableSet.Builder<V> setOfValuesBuilder = ImmutableSet.builder(); ImmutableMap.Builder<K, V> mapBuilder = ImmutableMap.builder(); for (Map.Entry<K, V> entry : entries) { setOfValuesBuilder.add(entry.getValue()); mapBuilder.put(entry.getKey(), entry.getValue()); } final Set<V> setOfValues = setOfValuesBuilder.build(); immutableMap = mapBuilder.build(); immutableReverseMap = ImmutableMap.copyOf(Maps.asMap(setOfValues, new Function<V, List<K>>() { @Override public List<K> apply(final V input) { return Lists.newArrayList(Maps.filterKeys(immutableMap, new Predicate<K>() { @Override public boolean apply(K key) { V retVal = immutableMap.get(key); if (retVal == null) { return false; } return retVal.equals(input); } }).keySet()); } })); } }
From source file:org.apache.accumulo.shell.commands.TablesCommand.java
@SuppressWarnings("unchecked") @Override//from w w w .j av a 2s . c o m public int execute(final String fullCommand, final CommandLine cl, final Shell shellState) throws AccumuloException, AccumuloSecurityException, IOException, NamespaceNotFoundException { final String namespace = cl.hasOption(OptUtil.namespaceOpt().getOpt()) ? OptUtil.getNamespaceOpt(cl, shellState) : null; Map<String, String> tables = shellState.getConnector().tableOperations().tableIdMap(); // filter only specified namespace tables = Maps.filterKeys(tables, tableName -> namespace == null || Tables.qualify(tableName).getFirst().equals(namespace)); final boolean sortByTableId = cl.hasOption(sortByTableIdOption.getOpt()); tables = new TreeMap<String, String>((sortByTableId ? MapUtils.invertMap(tables) : tables)); Iterator<String> it = Iterators.transform(tables.entrySet().iterator(), entry -> { String tableName = String.valueOf(sortByTableId ? entry.getValue() : entry.getKey()); String tableId = String.valueOf(sortByTableId ? entry.getKey() : entry.getValue()); if (namespace != null) tableName = Tables.qualify(tableName).getSecond(); if (cl.hasOption(tableIdOption.getOpt())) return String.format(NAME_AND_ID_FORMAT, tableName, tableId); else return tableName; }); shellState.printLines(it, !cl.hasOption(disablePaginationOpt.getOpt())); return 0; }
From source file:net.derquinse.common.orm.Entities.java
/** Clears the target map and inserts every non-null element from the source one. */ public static <K, V> void pushMap(Map<K, V> target, Map<? extends K, ? extends V> source) { checkNotNull(target);// w ww .jav a2 s. c o m target.clear(); if (source != null) { target.putAll(Maps.filterKeys(Maps.filterValues(source, Predicates.notNull()), Predicates.notNull())); } }
From source file:org.apache.cassandra.cql3.statements.SchemaAlteringStatement.java
private static Map<String, List<String>> describeSchemaVersions() { // unreachable hosts don't count towards disagreement return Maps.filterKeys(StorageProxy.describeSchemaVersions(), Predicates.not(Predicates.equalTo(StorageProxy.UNREACHABLE))); }
From source file:com.facebook.buck.core.cell.CellPathResolverView.java
@Override public ImmutableMap<String, Path> getCellPaths() { return ImmutableMap.copyOf(Maps.filterKeys(delegate.getCellPaths(), declaredCellNames::contains)); }
From source file:io.druid.indexing.overlord.setup.WorkerSelectUtils.java
/** * Helper for {@link WorkerSelectStrategy} implementations. * * @param allWorkers map of all workers, in the style provided to {@link WorkerSelectStrategy} * @param affinityConfig affinity config, or null * @param workerSelector function that receives a list of eligible workers: version is high enough, worker can run * the task, and worker satisfies the affinity config. may return null. * * @return selected worker from "allWorkers", or null. *//*from w ww .j a va 2s.c o m*/ @Nullable public static ImmutableWorkerInfo selectWorker(final Task task, final Map<String, ImmutableWorkerInfo> allWorkers, final WorkerTaskRunnerConfig workerTaskRunnerConfig, @Nullable final AffinityConfig affinityConfig, final Function<ImmutableMap<String, ImmutableWorkerInfo>, ImmutableWorkerInfo> workerSelector) { // Workers that could potentially run this task, ignoring affinityConfig. final Map<String, ImmutableWorkerInfo> runnableWorkers = allWorkers.values().stream() .filter(worker -> worker.canRunTask(task) && worker.isValidVersion(workerTaskRunnerConfig.getMinWorkerVersion())) .collect(Collectors.toMap(w -> w.getWorker().getHost(), Function.identity())); if (affinityConfig == null) { // All runnable workers are valid. return workerSelector.apply(ImmutableMap.copyOf(runnableWorkers)); } else { // Workers assigned to the affinity pool for our task. final Set<String> dataSourceWorkers = affinityConfig.getAffinity().get(task.getDataSource()); if (dataSourceWorkers == null) { // No affinity config for this dataSource; use non-affinity workers. return workerSelector.apply(getNonAffinityWorkers(affinityConfig, runnableWorkers)); } else { // Get runnable, affinity workers. final ImmutableMap<String, ImmutableWorkerInfo> dataSourceWorkerMap = ImmutableMap .copyOf(Maps.filterKeys(runnableWorkers, dataSourceWorkers::contains)); final ImmutableWorkerInfo selected = workerSelector.apply(dataSourceWorkerMap); if (selected != null) { return selected; } else if (affinityConfig.isStrong()) { return null; } else { // Weak affinity allows us to use nonAffinityWorkers for this dataSource, if no affinity workers // are available. return workerSelector.apply(getNonAffinityWorkers(affinityConfig, runnableWorkers)); } } } }
From source file:ru.codeinside.gses.activiti.behavior.TaskFields.java
public <T> Field<T> next(FieldType<T> type, String... aliases) { Collection<String> unifiedNames = unifyNames(type.getName(), aliases); validNames.addAll(unifiedNames);//from w ww.j ava 2 s . c om Collection<FieldDeclaration> declarations = Maps.filterKeys(fields, in(unifiedNames)).values(); if (declarations.size() > 1) { throw new IllegalArgumentException(" ? {" + Joiner.on(" | ").join(transform(declarations, new Names())) + "}"); } Collection<Expression> expressions = filter(transform(declarations, new Values()), notNull()); Expression expression; if (expressions.isEmpty()) { if (type.getUsage() == Usage.REQUIRED) { throw new IllegalArgumentException(" ? {" + type.getName() + "}, ? {" + Joiner.on(" | ").join(aliases) + "}"); } expression = null; } else { expression = expressions.iterator().next(); } return type.createField(expression); }