Example usage for com.google.common.collect Sets intersection

List of usage examples for com.google.common.collect Sets intersection

Introduction

In this page you can find the example usage for com.google.common.collect Sets intersection.

Prototype

public static <E> SetView<E> intersection(final Set<E> set1, final Set<?> set2) 

Source Link

Document

Returns an unmodifiable view of the intersection of two sets.

Usage

From source file:com.thoughtworks.go.server.newsecurity.helpers.AuthorityVerifier.java

public boolean hasAnyAuthorityMatching(Set<GrantedAuthority> userAuthorities) {
    return !Sets.intersection(grantedAuthorities, userAuthorities).isEmpty();
}

From source file:com.github.gdfm.shobaidogu.StatsUtils.java

/**
 * Computes the fraction of source elements that are also in target.
 * /*from  w w w.j av a  2  s.  com*/
 * @param source
 *          the source set.
 * @param target
 *          the target set.
 * @return the fraction.
 */
public static <T> double hitPercent(Set<T> source, Set<T> target) {
    checkNotNull(source);
    checkNotNull(target);
    if (source.isEmpty() || target.isEmpty())
        return 0;
    double intersectSize = Sets.intersection(source, target).size();
    return intersectSize / source.size();
}

From source file:org.crypto.sse.Partition.java

public static Multimap<Integer, String> partitioning(Multimap<String, String> lookup) {

    // Partitions Creation
    Set<String> keys = lookup.keySet();

    int partitionId = 0;
    Multimap<Integer, String> partitions = ArrayListMultimap.create();
    int counter2 = 0;

    for (String key : keys) {
        Set<Integer> keys2 = partitions.keySet();
        List<String> inter = (List<String>) lookup.get(key);
        List<String> interTMP = new ArrayList<String>(inter);

        Printer.debugln("Step number: " + counter2++ + "Number of keywords " + keys.size());

        Set<String> set = new HashSet<String>(interTMP);
        Multimap<Integer, String> partitionsTMP = ArrayListMultimap.create();

        for (Integer key2 : keys2) {

            if (!set.isEmpty()) {
                Set<String> tmp = new HashSet<String>(partitions.get(key2));

                Set<String> intersection = Sets.intersection(tmp, set);

                Set<String> difference;

                if (intersection.isEmpty()) {
                    difference = tmp;/* w w w.j a  v  a  2 s  . c  om*/
                } else {
                    difference = Sets.difference(tmp, intersection);
                    set = Sets.difference(set, intersection);

                }

                if (!difference.isEmpty()) {
                    partitionId = partitionId + 1;
                    partitionsTMP.putAll(partitionId, difference);
                }

                if (!intersection.isEmpty()) {
                    partitionId = partitionId + 1;
                    partitionsTMP.putAll(partitionId, intersection);
                }

            } else {
                partitionId = partitionId + 1;
                partitionsTMP.putAll(partitionId, new HashSet<String>(partitions.get(key2)));
            }

        }

        interTMP = new ArrayList<String>(set);

        if (!interTMP.isEmpty()) {

            partitionId = partitionId + 1;
            partitionsTMP.putAll(partitionId, interTMP);

        }

        partitions = ArrayListMultimap.create(partitionsTMP);
        partitionsTMP.clear();
        interTMP.clear();

    }

    Printer.debugln("Partitions size " + partitions.keySet().size());
    Printer.debugln("\n");

    return partitions;
}

From source file:org.eclipse.emf.compare.match.eobject.PairCharDistance.java

/**
 * Return a metric result about name similarity. It compares 2 strings and return a double comprised
 * between 0 and 1. The greater this metric, the more equal the strings are.
 * /*from   ww w  . j  a  va  2  s.  c  om*/
 * @param str1
 *            First of the two {@link String}s to compare.
 * @param str2
 *            Second of the two {@link String}s to compare.
 * @return A metric result about name similarity (0 &lt;= value &lt;= 1).
 */
public int distance(String str1, String str2) {
    Set<String> a = pairs(str1);
    Set<String> b = pairs(str2);
    Set<String> pairsInCommon = Sets.intersection(a, b);
    double similarity = pairsInCommon.size() * 2d / (a.size() + b.size());
    double result = 10 * similarity;
    return 10 - (int) result;
}

From source file:gov.va.maveric.uima.post.CombineRule.java

@Override
public String getResult(Set<String> values) {
    if (wanted.size() == Sets.intersection(wanted, values).size())
        return result;
    return null;//  ww w . j  a v  a  2s  .  com
}

From source file:com.textocat.textokit.morph.commons.TagUtils.java

public static boolean isClosedClassTag(String tag) {
    return closedClassPunctuationTags.contains(tag)
            || !Sets.intersection(GramModelBasedTagMapper.parseTag(tag), closedPosSet).isEmpty();
}

From source file:org.elasticsearch.test.cache.recycler.MockPageCacheRecycler.java

public static void ensureAllPagesAreReleased() throws Exception {
    final Map<Object, Throwable> masterCopy = Maps.newHashMap(ACQUIRED_PAGES);
    if (!masterCopy.isEmpty()) {
        // not empty, we might be executing on a shared cluster that keeps on obtaining
        // and releasing pages, lets make sure that after a reasonable timeout, all master
        // copy (snapshot) have been released
        boolean success = ElasticsearchTestCase.awaitBusy(new Predicate<Object>() {
            @Override//from  ww  w  . j  a  v a2  s . com
            public boolean apply(Object input) {
                return Sets.intersection(masterCopy.keySet(), ACQUIRED_PAGES.keySet()).isEmpty();
            }
        });
        if (!success) {
            masterCopy.keySet().retainAll(ACQUIRED_PAGES.keySet());
            ACQUIRED_PAGES.keySet().removeAll(masterCopy.keySet()); // remove all existing master copy we will report on
            if (!masterCopy.isEmpty()) {
                final Throwable t = masterCopy.entrySet().iterator().next().getValue();
                throw new RuntimeException(masterCopy.size() + " pages have not been released", t);
            }
        }
    }
}

From source file:com.facebook.buck.apple.Flavors.java

/**
 * Propagate flavors represented by the given {@link FlavorDomain} objects from a parent target to
 * its dependencies.//from   w  ww  . ja v a 2 s  . c  o  m
 */
public static ImmutableSortedSet<BuildTarget> propagateFlavorDomains(BuildTarget target,
        Iterable<FlavorDomain<?>> domains, Iterable<BuildTarget> deps) {

    Set<Flavor> flavors = new HashSet<>();

    // For each flavor domain, extract the corresponding flavor from the parent target and
    // verify that each dependency hasn't already set this flavor.
    for (FlavorDomain<?> domain : domains) {

        // Now extract all relevant domain flavors from our parent target.
        ImmutableSet<Flavor> flavorSet = Sets.intersection(domain.getFlavors(), target.getFlavors())
                .immutableCopy();

        if (flavorSet.isEmpty()) {
            throw new HumanReadableException("%s: no flavor for \"%s\"", target, domain.getName());
        }
        flavors.addAll(flavorSet);

        // First verify that our deps are not already flavored for our given domains.
        for (BuildTarget dep : deps) {
            if (domain.getFlavor(dep).isPresent()) {
                throw new HumanReadableException("%s: dep %s already has flavor for \"%s\" : %s", target, dep,
                        domain.getName(), flavorSet.toString());
            }
        }
    }

    ImmutableSortedSet.Builder<BuildTarget> flavoredDeps = ImmutableSortedSet.naturalOrder();

    // Now flavor each dependency with the relevant flavors.
    for (BuildTarget dep : deps) {
        flavoredDeps.add(dep.withAppendedFlavors(flavors));
    }

    return flavoredDeps.build();
}

From source file:io.crate.metadata.table.Operation.java

public static EnumSet<Operation> buildFromIndexSettings(Settings settings) {
    Set<Operation> operations = ALL;
    for (Map.Entry<String, EnumSet<Operation>> entry : BLOCK_SETTING_TO_OPERATIONS_MAP.entrySet()) {
        if (!settings.getAsBoolean(entry.getKey(), false)) {
            continue;
        }//w w w  . jav  a2s.  c  o  m
        operations = Sets.intersection(entry.getValue(), operations);
    }
    return EnumSet.copyOf(operations);
}

From source file:fr.webmarket.backend.features.search.criteria.ItemTagCriterion.java

@Override
public boolean apply(Item item) {

    switch (strategy) {
    case ALL_CRITERIA:
        return item.getTags().containsAll(tags);
    case ONE_OF_CRITERION:
        return Sets.intersection(item.getTags(), tags).size() != 0;
    case NONE://ww w .  ja  va  2 s.  c  o m
        return true;
    }

    return false;
}