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

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

Introduction

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

Prototype

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

Source Link

Document

Returns an unmodifiable view of the difference of two sets.

Usage

From source file:org.opendaylight.nic.compiler.Transform.java

public Collection<Policy> resolve(Policy p1, Policy p2) throws IntentCompilerException {

    Collection<Policy> policies = new LinkedList<>();
    Sets.SetView<Endpoint> src;
    Sets.SetView<Endpoint> dst;

    // All the possible cases below

    src = Sets.difference(p1.src(), p2.src());
    if (!src.isEmpty()) {

        // Case: S1 and not S2 , D1 and not D2
        dst = Sets.difference(p1.dst(), p2.dst());
        if (!dst.isEmpty()) {
            policies.add(new PolicyImpl(src, dst, p1.action()));
        }/*w w w.j  a va 2  s. co m*/

        // Case: S1 and not S2 , D1 and D2
        dst = Sets.intersection(p1.dst(), p2.dst());
        if (!dst.isEmpty()) {
            policies.add(new PolicyImpl(src, dst, p1.action()));
        }
    }
    src = Sets.intersection(p1.src(), p2.src());
    if (!src.isEmpty()) {

        // Case: S1 and S2 , D1 and D2
        dst = Sets.intersection(p1.dst(), p2.dst());
        if (!dst.isEmpty()) {
            Set<Action> mergedActions = merge(p1.action(), p2.action());
            if (mergedActions == null) {
                throw new IntentCompilerException("Unable to merge exclusive actions", Arrays.asList(p1, p2));
            }
            policies.add(new PolicyImpl(src, dst, mergedActions));
        }

        // Case: S1 and S2 , D1 and not D2
        dst = Sets.difference(p1.dst(), p2.dst());
        if (!dst.isEmpty()) {
            policies.add(new PolicyImpl(src, dst, p1.action()));
        }

        // Case: S1 and S2 , D2 and not D1
        dst = Sets.difference(p2.dst(), p1.dst());
        if (!dst.isEmpty()) {
            policies.add(new PolicyImpl(src, dst, p2.action()));
        }
    }
    src = Sets.difference(p2.src(), p1.src());
    if (!src.isEmpty()) {

        // Case: S2 and not S1 , D1 and D2
        dst = Sets.intersection(p1.dst(), p2.dst());
        if (!dst.isEmpty()) {
            policies.add(new PolicyImpl(src, dst, p2.action()));
        }

        // Case: S2 and not S1 , D2 and not D1
        dst = Sets.difference(p2.dst(), p1.dst());
        if (!dst.isEmpty()) {
            policies.add(new PolicyImpl(src, dst, p2.action()));
        }
    }
    return policies;
}

From source file:org.nuxeo.tools.esync.checker.DocumentTypeLister.java

@Override
void check() {//w  w w .jav a  2s.  co m
    Set<String> esIds = es.getDocumentIdsForType(primaryType);
    Set<String> dbIds = db.getDocumentIdsForType(primaryType);
    Sets.SetView<String> diff = Sets.difference(dbIds, esIds);
    if (diff.isEmpty()) {
        postMessage(String.format("All DB docIds found in ES for type %s", primaryType));
    } else {
        postMessage(String.format("Missing %d documents for type: %s", diff.size(), primaryType));
        for (String key : diff) {
            post(new MissingEvent(key, String.format("type %s", primaryType)));
        }
    }
    diff = Sets.difference(esIds, dbIds);
    if (diff.isEmpty()) {
        postMessage(String.format("All ES docIds found in DB for type %s", primaryType));
    } else {
        postMessage(String.format("%d spurious documents for type: %s", diff.size(), primaryType));
        for (String key : diff) {
            post(new TrailingEvent(key, String.format("type %s", primaryType)));
        }
    }
}

From source file:org.jgrades.admin.general.DaysMgntServiceImpl.java

@Override
public void setWorkingDays(WorkingDays newWorkingDays) {
    WorkingDays actualWorkingDays = getWorkingDays();

    for (DayOfWeek newDay : Sets.difference(newWorkingDays.getDays(), actualWorkingDays.getDays())) {
        SchoolDay schoolDay = new SchoolDay(newDay.getValue(), newDay);
        schoolDay.setSchool(generalDataService.getSchoolGeneralDetails());
        repository.save(schoolDay);//from  w w w  . j av  a2 s . co m
    }

    for (DayOfWeek toRemoveDay : Sets.difference(actualWorkingDays.getDays(), newWorkingDays.getDays())) {
        repository.delete(toRemoveDay.getValue());
    }
}

From source file:org.gradle.internal.nativeplatform.processenvironment.AbstractProcessEnvironment.java

public boolean maybeSetEnvironment(Map<String, String> source) {
    // need to take copy to prevent ConcurrentModificationException
    List<String> keysToRemove = Lists.newArrayList(Sets.difference(System.getenv().keySet(), source.keySet()));
    for (String key : keysToRemove) {
        removeEnvironmentVariable(key);/*ww w  .j  av  a2  s  . c  o m*/
    }
    for (Map.Entry<String, String> entry : source.entrySet()) {
        setEnvironmentVariable(entry.getKey(), entry.getValue());
    }
    return true;
}

From source file:com.facebook.buck.testutil.MoreAsserts.java

/**
 * Asserts that two sets have the same contents.
 * On failure, prints a readable diff of the two sets for easy debugging.
 *//*www  .j  a  va 2  s .  co  m*/
public static <E> void assertSetEquals(String userMessage, Set<E> expected, Set<E> actual) {
    Set<E> missing = Sets.difference(expected, actual);
    Set<E> extra = Sets.difference(actual, expected);
    boolean setsEqual = missing.isEmpty() && extra.isEmpty();
    Assert.assertTrue(
            String.format("%s %nMissing elements:%n%s%nExtraneous elements:%n%s", userMessage, missing, extra),
            setsEqual);
}

From source file:org.tzi.use.analysis.metrics.ShellMetricCommandProcessor.java

protected void dispatch() {
    Set<String> actualArgs = Sets.newHashSet(line.split(" "));
    Set<String> validArgs = Sets.newHashSet("metrics", "--run");
    Set<String> delta = Sets.difference(actualArgs, validArgs);

    if (delta.isEmpty()) {
        //Log.info("Arguments valid");

        // TODO Extract metric name.
        // TODO Extract configuration file name.
        if (actualArgs.contains("--run"))
            _measure("sg", "default");
    } else {//from   www. j a  va  2 s  .  c  om
        Log.error("Invalid arguments: ", delta);
    }
}

From source file:org.obiba.mica.core.repository.PersonAwareRepository.java

default void updateRemovedContacts(T obj) {
    List<Person> persons = findAllPersonsByParent(obj);
    Sets.SetView<Person> removedPersons = Sets.difference(Sets.newHashSet(persons),
            Sets.newHashSet(obj.getAllPersons()));
    removedPersons.forEach(c -> obj.removeFromPerson(c));
    getPersonRepository().save(removedPersons);
    removedPersons.forEach(p -> getEventBus().post(new PersonUpdatedEvent(p)));
}

From source file:springfox.documentation.spring.web.plugins.PathAndParametersEquivalence.java

@Override
protected boolean doEquivalent(RequestHandler a, RequestHandler b) {
    return a.getPatternsCondition().equals(b.getPatternsCondition())
            && !Sets.intersection(a.supportedMethods(), b.supportedMethods()).isEmpty()
            && a.params().equals(b.params())
            && Sets.difference(wrapped(a.getParameters()), wrapped(b.getParameters())).isEmpty();
}

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;/*from w  w  w.  j  a v a 2 s.  c o m*/
                } 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:io.druid.data.input.impl.MapInputRowParser.java

@Override
public InputRow parse(Map<String, Object> theMap) {
    final List<String> dimensions = parseSpec.getDimensionsSpec().hasCustomDimensions()
            ? parseSpec.getDimensionsSpec().getDimensionNames()
            : Lists.newArrayList(//from  w  ww .  ja  v  a 2  s.c  o  m
                    Sets.difference(theMap.keySet(), parseSpec.getDimensionsSpec().getDimensionExclusions()));

    final DateTime timestamp;
    try {
        timestamp = parseSpec.getTimestampSpec().extractTimestamp(theMap);
        if (timestamp == null) {
            final String input = theMap.toString();
            throw new NullPointerException(String.format("Null timestamp in input: %s",
                    input.length() < 100 ? input : input.substring(0, 100) + "..."));
        }
    } catch (Exception e) {
        throw new ParseException(e, "Unparseable timestamp found!");
    }

    return new MapBasedInputRow(timestamp.getMillis(), dimensions, theMap);
}