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:uk.ac.ebi.spot.rdf.model.Profile.java

public boolean isExpressedOnAnyOf(Set<K> conditions) {
    checkArgument(CollectionUtils.isNotEmpty(conditions));
    return Sets.intersection(expressionsByCondition.keySet(), conditions).size() > 0;
}

From source file:edu.usc.ee599.viz.BalancedTrangleCountWithViz.java

public void calculate(Edge e) {

    if (e.operation == Edge.ADD) {

        if (edgeSet.contains(e)) {
            return;
        } else {//from  ww w .  jav  a2 s.c om

            if (edgeMap.get(e.source) != null && edgeMap.get(e.sink) != null) {

                Sets.SetView<Integer> setView = Sets.intersection(edgeMap.get(e.source), edgeMap.get(e.sink));

                for (int common : setView) {

                    Triangle t = new Triangle(e.source, e.sink, common);

                    if (!allPlusT.contains(t) && !allNegT.contains(t) && !onePlusT.contains(t)
                            && !twoPlusT.contains(t)) {

                        Edge e1 = edges.get("" + e.source + "-" + common);

                        if (e1 == null) {
                            e1 = edges.get("" + common + "-" + e.source);
                        }

                        Edge e2 = edges.get("" + e.sink + "-" + common);

                        if (e2 == null) {
                            e2 = edges.get("" + common + "-" + e.sink);
                        }

                        int switchKey = e.sign + e1.sign + e2.sign;

                        switch (switchKey) {

                        case 3: {
                            allPlusT.add(t);
                            view.updateBalanced(allPlusT.size() + allNegT.size() + onePlusT.size());
                            view.updateUnBalanced(twoPlusT.size());
                            break;
                        }

                        case -3: {
                            allNegT.add(t);
                            view.updateBalanced(allPlusT.size() + allNegT.size() + onePlusT.size());
                            view.updateUnBalanced(twoPlusT.size());
                            break;
                        }

                        case 1: {
                            twoPlusT.add(t);
                            view.updateBalanced(allPlusT.size() + allNegT.size() + onePlusT.size());
                            view.updateUnBalanced(twoPlusT.size());
                            break;
                        }

                        case -1: {
                            onePlusT.add(t);
                            view.updateBalanced(allPlusT.size() + allNegT.size() + onePlusT.size());
                            view.updateUnBalanced(twoPlusT.size());
                            break;
                        }
                        }

                    }

                }

            }

            edgeSet.add(e);
            edges.put("" + e.source + "-" + e.sink, e);

            if (edgeMap.containsKey(e.source)) {

                edgeMap.get(e.source).add(e.sink);

            } else {
                Set<Integer> set = new HashSet<Integer>();
                set.add(e.sink);

                edgeMap.put(e.source, set);
            }

            if (edgeMap.containsKey(e.sink)) {

                edgeMap.get(e.sink).add(e.source);

            } else {
                Set<Integer> set = new HashSet<Integer>();
                set.add(e.source);

                edgeMap.put(e.sink, set);
            }

        }

    } else if (e.operation == Edge.REMOVE) {

        if (!edgeSet.contains(e)) {
            System.out.println("Invalid edge operation, Edge does not exist! : " + e);
            return;
        }

        if (edgeMap.get(e.source) != null && edgeMap.get(e.sink) != null) {

            Sets.SetView<Integer> setView = Sets.intersection(edgeMap.get(e.source), edgeMap.get(e.sink));

            for (int common : setView) {

                Triangle t = new Triangle(e.source, e.sink, common);

                if (allPlusT.contains(t)) {
                    allPlusT.remove(t);
                    view.updateBalanced(allPlusT.size() + allNegT.size() + onePlusT.size());
                    view.updateUnBalanced(twoPlusT.size());
                }

                if (allNegT.contains(t)) {
                    allNegT.remove(t);
                    view.updateBalanced(allPlusT.size() + allNegT.size() + onePlusT.size());
                    view.updateUnBalanced(twoPlusT.size());
                }

                if (onePlusT.contains(t)) {
                    onePlusT.remove(t);
                    view.updateBalanced(allPlusT.size() + allNegT.size() + onePlusT.size());
                    view.updateUnBalanced(twoPlusT.size());
                }

                if (twoPlusT.contains(t)) {
                    twoPlusT.remove(t);
                    view.updateBalanced(allPlusT.size() + allNegT.size() + onePlusT.size());
                    view.updateUnBalanced(twoPlusT.size());
                }

            }

        }

        edgeSet.remove(e);

        if (edgeMap.containsKey(e.source)) {
            edgeMap.get(e.source).remove(e.sink);
        }

        if (edgeMap.containsKey(e.sink)) {
            edgeMap.get(e.sink).remove(e.source);
        }

    } else if (e.operation == Edge.UPDATE) {

        if (!edgeSet.contains(e)) {
            System.out.println("Invalid edge operation, Edge does not exist! : " + e);
            return;
        }

        Set<Integer> sourceSet = edgeMap.get(e.source);
        Set<Integer> sinkSet = edgeMap.get(e.sink);

        if (sourceSet != null && sinkSet != null) {

            Sets.SetView<Integer> setView = null;
            if (sourceSet.size() <= sourceSet.size()) {
                setView = Sets.intersection(sourceSet, sinkSet);
            } else {
                setView = Sets.intersection(sinkSet, sourceSet);
            }

            for (int common : setView) {

                Triangle t = new Triangle(e.source, e.sink, common);

                int switchKey = -1;

                if (allPlusT.contains(t)) {
                    switchKey = 1; // All + -> two +
                    allPlusT.remove(t);

                    view.updateBalanced(allPlusT.size() + allNegT.size() + onePlusT.size());
                    view.updateUnBalanced(twoPlusT.size());
                }

                if (allNegT.contains(t)) {
                    switchKey = 2; // All - -> one +
                    allNegT.remove(t);
                    view.updateBalanced(allPlusT.size() + allNegT.size() + onePlusT.size());
                    view.updateUnBalanced(twoPlusT.size());
                }

                if (onePlusT.contains(t)) {
                    if (e.sign == Edge.MINUS) {
                        switchKey = 3; // One + -> All -
                    } else {
                        switchKey = 1; // One + -> Two +
                    }
                    onePlusT.remove(t);
                    view.updateBalanced(allPlusT.size() + allNegT.size() + onePlusT.size());
                    view.updateUnBalanced(twoPlusT.size());
                }

                if (twoPlusT.contains(t)) {

                    if (e.sign == Edge.MINUS) {
                        switchKey = 2; // Two + -> One +
                    } else {
                        switchKey = 4; //Two + -> All +
                    }

                    twoPlusT.remove(t);
                }

                switch (switchKey) {
                case 1: {
                    twoPlusT.add(t);
                    view.updateBalanced(allPlusT.size() + allNegT.size() + onePlusT.size());
                    view.updateUnBalanced(twoPlusT.size());
                    break;
                }
                case 2: {
                    onePlusT.add(t);
                    view.updateBalanced(allPlusT.size() + allNegT.size() + onePlusT.size());
                    view.updateUnBalanced(twoPlusT.size());
                    break;
                }
                case 3: {
                    allNegT.add(t);
                    view.updateBalanced(allPlusT.size() + allNegT.size() + onePlusT.size());
                    view.updateUnBalanced(twoPlusT.size());
                    break;
                }
                case 4: {
                    allPlusT.add(t);
                    view.updateBalanced(allPlusT.size() + allNegT.size() + onePlusT.size());
                    view.updateUnBalanced(twoPlusT.size());
                    break;
                }
                }

            }

        }

    }

}

From source file:org.tendiwa.inflectible.GmWithSimilarity.java

/**
 * Tells many grammemes this grammatical meaning has in common with
 * another one.//from   www  .ja  va2 s.c o m
 * @param another Another grammatial meaning
 * @return The number of grammemes this grammatical meaning has in common
 *  with another one
 * @throws Exception If couldn't compute similarity
 */
public int similarity(final GrammaticalMeaning another) throws Exception {
    return Sets.intersection(this.wrapped.grammemes(), another.grammemes()).size();
}

From source file:es.upm.dit.xsdinferencer.merge.mergerimpl.enumeration.MinIntersectionBidirectionalEnumComparator.java

/**
 * @see EnumComparator#compare(SimpleType, SimpleType)
 */// w w w.j  a  v  a  2 s . com
@Override
public boolean compare(SimpleType simpleType1, SimpleType simpleType2) {
    if (!simpleType1.isEnum() || !simpleType2.isEnum())
        return false;
    if (simpleType1.isEmpty() && simpleType2.isEmpty())
        return true;
    Set<String> valuesOfSimpleType1 = Sets.newHashSet(simpleType1);
    Set<String> valuesOfSimpleType2 = Sets.newHashSet(simpleType2);
    Set<String> valuesOfIntersection = Sets.intersection(valuesOfSimpleType1, valuesOfSimpleType2);
    float ratio1 = ((float) valuesOfIntersection.size()) / ((float) valuesOfSimpleType1.size());
    float ratio2 = ((float) valuesOfIntersection.size()) / ((float) valuesOfSimpleType2.size());
    return ratio1 >= threshold && ratio2 >= threshold;
}

From source file:org.tensorics.core.tensor.Positions.java

/**
 * Constructs a position, whose coordinates are the union of the coordinates of the two individual positions. This
 * is only correctly defined, if the two given positions do not have any dimensional overlap (i.e. Any coordinate of
 * a certain type (class) is only present in either the left or the right position.)
 *
 * @param left the first position to use construct the union position
 * @param right the second position to construct the union position
 * @return a position, which contains the union of the coordinates of the two input positions.
 * @throws NullPointerException if one of the arguments is {@code null}
 * @throws IllegalArgumentException if the given aguments have an overlap of dimensions and therefor the union of
 *             the position is not well defined
 *///from  w w w.j  ava  2 s  .  c  o  m
public static Position union(Position left, Position right) {
    checkNotNull(left, "left position must not be null.");
    checkNotNull(right, "right position must not be null.");
    checkArgument(Sets.intersection(left.dimensionSet(), right.dimensionSet()).isEmpty(),
            "Positions have overlapping dimensions. It is not possible to create the union of them.");
    SetView<Object> coordinates = Sets.union(left.coordinates(), right.coordinates());
    return Position.of(coordinates);
}

From source file:edu.byu.mpn.rest.BaseController.java

protected boolean personHasProxyRole() {
    return !Sets.intersection(proxyRoles, getLoggedInUserRoles()).isEmpty();
}

From source file:org.caleydo.view.relationshipexplorer.ui.column.operation.ESetOperation.java

public Set<Object> apply(Set<Object> set1, Set<Object> set2) {
    switch (this) {
    case REMOVE://w  ww.j av  a2 s.c o  m
        Set<Object> result = new HashSet<>(set2);
        result.removeAll(set1);
        return result;
    // case REPLACE:
    // return set1;
    case INTERSECTION:
        return Sets.intersection(set1, set2);
    case UNION:
        return Sets.union(set1, set2);
    default:
        return null;
    }
}

From source file:cz.afri.smg.graphs.CLangSMGConsistencyVerifier.java

/**
 * Verifies that heap and stack object sets are disjunct
 *
 * @param pLogger Logger to log the message
 * @param pSmg SMG to check//from   www.j  a  va 2  s  .  c o  m
 * @return True if {@link pSmg} is consistent w.r.t. this criteria. False otherwise.
 */
private static boolean verifyDisjunctHeapAndStack(final ReadableSMG pSmg) {
    ArrayDeque<CLangStackFrame> stackFrames = pSmg.getStackFrames();
    Set<SMGObject> stack = new HashSet<>();

    for (CLangStackFrame frame : stackFrames) {
        stack.addAll(frame.getAllObjects());
    }
    Set<SMGObject> heap = pSmg.getHeapObjects();

    boolean toReturn = Collections.disjoint(stack, heap);

    if (!toReturn) {
        SetView<SMGObject> intersection = Sets.intersection(stack, heap);
        String message = "CLangSMG inconsistent, heap and stack objects are not disjoint: " + intersection;
        throw new IllegalStateException(message);
    }

    return toReturn;
}

From source file:com.visural.stereotyped.ui.Privilege.java

public static IPrivilege currentUserHasWriteAccess(final UUID id) {
    return new IPrivilege() {

        public boolean isGrantedToClient(IClient arg0) {
            if (id == null) {
                return true;
            }// w w  w.  j  a v  a 2s.  c o m
            StereotypeUIMetaData meta = ServiceProvider.getServiceInstance(StereotypeService.class)
                    .readUIMeta(id);
            User u = (User) arg0;
            return ADMINISTRATOR.isGrantedToClient(arg0) || meta.getOwner().equals(u.getId())
                    || meta.getEditUsers().contains(u.getId())
                    || !Sets.intersection(meta.getEditGroups(), u.getGroups()).isEmpty();
        }

    };
}

From source file:org.elasticsearch.common.util.MockBigArrays.java

public static void ensureAllArraysAreReleased() throws Exception {
    final Map<Object, Object> masterCopy = Maps.newHashMap(ACQUIRED_ARRAYS);
    if (!masterCopy.isEmpty()) {
        // not empty, we might be executing on a shared cluster that keeps on obtaining
        // and releasing arrays, lets make sure that after a reasonable timeout, all master
        // copy (snapshot) have been released
        boolean success = ESTestCase.awaitBusy(new Predicate<Object>() {
            @Override/*from  www .  j ava 2s  .  c  o m*/
            public boolean apply(Object input) {
                return Sets.intersection(masterCopy.keySet(), ACQUIRED_ARRAYS.keySet()).isEmpty();
            }
        });
        if (!success) {
            masterCopy.keySet().retainAll(ACQUIRED_ARRAYS.keySet());
            ACQUIRED_ARRAYS.keySet().removeAll(masterCopy.keySet()); // remove all existing master copy we will report on
            if (!masterCopy.isEmpty()) {
                final Object cause = masterCopy.entrySet().iterator().next().getValue();
                throw new RuntimeException(masterCopy.size() + " arrays have not been released",
                        cause instanceof Throwable ? (Throwable) cause : null);
            }
        }
    }
}