Example usage for com.google.common.collect Maps filterKeys

List of usage examples for com.google.common.collect Maps filterKeys

Introduction

In this page you can find the example usage for com.google.common.collect Maps filterKeys.

Prototype

@CheckReturnValue
public static <K, V> BiMap<K, V> filterKeys(BiMap<K, V> unfiltered, final Predicate<? super K> keyPredicate) 

Source Link

Document

Returns a bimap containing the mappings in unfiltered whose keys satisfy a predicate.

Usage

From source file:org.openengsb.core.common.VirtualConnectorFactory.java

@Override
public Connector applyAttributes(Connector instance, Map<String, String> attributes) {
    VirtualType handler = handlers.get(instance);
    Collection<String> mixins = Maps.filterKeys(attributes, new Predicate<String>() {
        @Override/*from w w  w  .jav a 2  s  . c om*/
        public boolean apply(String s) {
            return s.startsWith("mixin.");
        }
    }).values();
    Collection<Class<?>> mixinClasses = Collections2.transform(mixins, new Function<String, Class<?>>() {
        @Override
        public Class<?> apply(String input) {
            try {
                return getClass().getClassLoader().loadClass(input);
            } catch (ClassNotFoundException e) {
                throw new ComputationException(e);
            }
        }
    });
    instance = createProxy(handler, mixinClasses);
    updateHandlerAttributes(handler, attributes);
    return instance;
}

From source file:com.feedzai.commons.sql.abstraction.engine.testconfig.DatabaseConfigurationUtil.java

/**
 * Filters the configurations./*from w  ww .ja  v  a 2 s.c o  m*/
 *
 * @param instances The instances to filter.
 * @return The configurations filtered.
 */
public Map<String, DatabaseConfiguration> filter(final Set<String> instances) {
    return Maps.filterKeys(configs, new Predicate<String>() {
        @Override
        public boolean apply(String input) {
            return instances.contains(input);
        }
    });
}

From source file:org.geogit.storage.memory.HeapRefDatabase.java

@Override
public Map<String, String> getAll(final String prefix) {

    Predicate<String> keyPredicate = new Predicate<String>() {

        @Override/*from  w  w  w.  j  a v a2  s  .  c  o  m*/
        public boolean apply(String refName) {
            return refName.startsWith(prefix);
        }
    };
    return Maps.filterKeys(ImmutableMap.copyOf(this.refs), keyPredicate);
}

From source file:com.siemens.sw360.importer.ComponentImportUtils.java

@NotNull
private static HashSet<Release> getUpdatedReleases(Map<String, Release> releasesByIdentifier,
        final Set<String> releasesIdentifiersToBeUpdated) {
    return Sets.newHashSet(Maps.filterKeys(releasesByIdentifier, new Predicate<String>() {
        @Override/*from www. j a  v  a  2  s  . c o m*/
        public boolean apply(String input) {
            return releasesIdentifiersToBeUpdated.contains(input);
        }
    }).values());
}

From source file:com.google.code.jgntp.internal.message.GntpMessageHeader.java

public String getValueInMap(Map<String, String> map) {
    Map<String, String> filteredMap = Maps.filterKeys(map, getPredicate());
    if (filteredMap.isEmpty()) {
        return null;
    }//from  www  .  j  a v a2s  .com
    return filteredMap.get(toString());
}

From source file:com.facebook.presto.tests.tpch.TpchIndexResolver.java

@Override
public ConnectorResolvedIndex resolveIndex(ConnectorSession session, ConnectorTableHandle tableHandle,
        Set<ColumnHandle> indexableColumns, TupleDomain<ColumnHandle> tupleDomain) {
    TpchTableHandle tpchTableHandle = checkType(tableHandle, TpchTableHandle.class, "tableHandle");

    // Keep the fixed values that don't overlap with the indexableColumns
    // Note: technically we could more efficiently utilize the overlapped columns, but this way is simpler for now

    Map<ColumnHandle, NullableValue> fixedValues = TupleDomain.extractFixedValues(tupleDomain)
            .orElse(ImmutableMap.of()).entrySet().stream()
            .filter(entry -> !indexableColumns.contains(entry.getKey()))
            .filter(entry -> !entry.getValue().isNull()) // strip nulls since meaningless in index join lookups
            .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));

    // determine all columns available for index lookup
    ImmutableSet.Builder<String> builder = ImmutableSet.builder();
    builder.addAll(transform(indexableColumns, columnNameGetter()));
    builder.addAll(transform(fixedValues.keySet(), columnNameGetter()));
    Set<String> lookupColumnNames = builder.build();

    // do we have an index?
    if (!indexedData.getIndexedTable(tpchTableHandle.getTableName(), tpchTableHandle.getScaleFactor(),
            lookupColumnNames).isPresent()) {
        return null;
    }/*from  ww  w .  ja  v a 2 s . co  m*/

    TupleDomain<ColumnHandle> filteredTupleDomain = tupleDomain;
    if (!tupleDomain.isNone()) {
        filteredTupleDomain = TupleDomain.withColumnDomains(
                Maps.filterKeys(tupleDomain.getDomains().get(), not(in(fixedValues.keySet()))));
    }
    return new ConnectorResolvedIndex(new TpchIndexHandle(connectorId, tpchTableHandle.getTableName(),
            tpchTableHandle.getScaleFactor(), lookupColumnNames, TupleDomain.fromFixedValues(fixedValues)),
            filteredTupleDomain);
}

From source file:com.foundationdb.server.types.service.TCastsRegistry.java

static Map<TClass, Map<TClass, TCast>> createStrongCastsMap(Map<TClass, Map<TClass, TCast>> castsBySource,
        final Set<TCastIdentifier> strongCasts) {
    Map<TClass, Map<TClass, TCast>> result = new HashMap<>();
    for (Map.Entry<TClass, Map<TClass, TCast>> origEntry : castsBySource.entrySet()) {
        final TClass source = origEntry.getKey();
        Map<TClass, TCast> filteredView = Maps.filterKeys(origEntry.getValue(), new Predicate<TClass>() {
            @Override//from   w w  w .  j  a va 2s.co  m
            public boolean apply(TClass target) {
                return (source == target) || strongCasts.contains(new TCastIdentifier(source, target));
            }
        });
        assert !filteredView.isEmpty() : "no strong casts (including self casts) found for " + source;
        result.put(source, new HashMap<>(filteredView));
    }
    return result;
}

From source file:org.locationtech.geogig.storage.memory.HeapRefDatabase.java

/**
 * @return all known references under the "refs" namespace (i.e. not top level ones like HEAD,
 *         etc), key'ed by ref name/*w w  w .  j  a va  2  s  .  c o m*/
 */
@Override
public Map<String, String> getAll() {

    Predicate<String> filter = Predicates.not(new RefPrefixPredicate(TRANSACTIONS_PREFIX));
    Map<String, String> allButTransactions = Maps.filterKeys(ImmutableMap.copyOf(this.refs), filter);
    return allButTransactions;
}

From source file:org.locationtech.geogig.remotes.pack.DiffRemoteRefsOp.java

@Override
protected List<RefDiff> _call() {
    checkState(remote != null, "no remote provided");
    // list of refs/remotes/<remote>/<refname> or refs/heads according to formatAsRemoteRefs
    Map<String, Ref> remotes;
    Map<String, Ref> locals;
    {/*from  ww  w.  j  av  a  2s.  c o  m*/
        // current live remote refs in the remote's local namespace (e.g. refs/heads/<branch>)
        Iterable<Ref> remoteRefs = getRemoteRefs();
        if (formatAsRemoteRefs) {
            // format refs returned by the remote in its local namespaces to our repository's
            // remotes namespace
            remoteRefs = command(MapRef.class)//
                    .setRemote(remote.getInfo())//
                    .convertToRemote()//
                    .addAll(remoteRefs)//
                    .call();
        }
        // current local local copy of the remote refs (e.g. refs/remotes/<remote>/<branch>
        List<Ref> remoteLocalRefs = Lists.newArrayList(getRemoteLocalRefs());
        if (!formatAsRemoteRefs) {
            // format local repository copies of the remote refs to the remote's local namespace
            remoteLocalRefs = command(MapRef.class)//
                    .setRemote(remote.getInfo())//
                    .convertToLocal()//
                    .addAll(remoteLocalRefs)//
                    .call();
        }
        if (this.getTags) {
            Map<String, RevTag> tags = Maps.uniqueIndex(command(TagListOp.class).call(), (t) -> t.getName());
            for (Ref rf : remoteRefs) {
                if (rf.getName().startsWith(Ref.TAGS_PREFIX) && tags.containsKey(rf.localName())) {
                    RevTag tag = tags.get(rf.localName());
                    remoteLocalRefs.add(new Ref(Ref.TAGS_PREFIX + tag.getName(), tag.getId()));
                }
            }
        }
        remotes = Maps.uniqueIndex(remoteRefs, (r) -> r.getName());
        locals = Maps.uniqueIndex(remoteLocalRefs, (r) -> r.getName());
    }
    final boolean mapped = remote.getInfo().getMapped();
    if (mapped) {
        // for a mapped remote, we are only interested in the branch we are mapped to
        final String mappedBranch = remote.getInfo().getMappedBranch();
        checkNotNull(mappedBranch);
        final String mappedBranchName = Ref.localName(mappedBranch);
        remotes = Maps.filterKeys(remotes, (name) -> Ref.localName(name).equals(mappedBranchName));
        locals = Maps.filterKeys(locals, (name) -> Ref.localName(name).equals(mappedBranchName));
    }
    MapDifference<String, Ref> difference = Maps.difference(remotes, locals);

    // refs existing on the remote and not on the local repo
    Collection<Ref> newRemoteRefs = difference.entriesOnlyOnLeft().values();

    // remote refs existing on the local repo and not existing on the remote anymore
    Collection<Ref> removedRemoteRefs = difference.entriesOnlyOnRight().values();

    // refs existing both in local and remote with different objectIds
    Collection<ValueDifference<Ref>> changes = difference.entriesDiffering().values();

    List<RefDiff> diffs = new ArrayList<>();
    newRemoteRefs.forEach((r) -> diffs.add(RefDiff.added(r)));
    removedRemoteRefs.forEach((r) -> diffs.add(RefDiff.removed(r)));
    // v.leftValue() == new (remote copy), v.rightValue() == old (local copy)
    changes.forEach((v) -> diffs.add(RefDiff.updated(v.rightValue(), v.leftValue())));

    return diffs;
}

From source file:com.facebook.buck.rules.modern.builders.MultiThreadedBlobUploader.java

/** Uploads missing items to the CAS. */
public void addMissing(ImmutableMap<Protocol.Digest, ThrowingSupplier<InputStream, IOException>> data)
        throws IOException {
    try {//from  w  w w .j a v a  2s  .c om
        data = ImmutableMap.copyOf(Maps.filterKeys(data, k -> !containedHashes.contains(k.getHash())));
        if (data.isEmpty()) {
            return;
        }
        enqueue(data).get();
    } catch (InterruptedException | ExecutionException e) {
        throw new IOException(e);
    }
}