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.apache.james.mailetcontainer.impl.matchers.Xor.java

private Collection<MailAddress> performXor(Collection<MailAddress> collection1,
        Collection<MailAddress> collection2) {
    ImmutableSet<MailAddress> set1 = ImmutableSet.copyOf(collection1);
    ImmutableSet<MailAddress> set2 = ImmutableSet.copyOf(collection2);
    return Sets.difference(Sets.union(set1, set2), Sets.intersection(set1, set2)).immutableCopy();
}

From source file:org.apache.druid.client.cache.HybridCache.java

@Override
public Map<NamedKey, byte[]> getBulk(Iterable<NamedKey> keys) {
    Set<NamedKey> remaining = Sets.newHashSet(keys);
    Map<NamedKey, byte[]> res = level1.getBulk(keys);
    hitCount.addAndGet(res.size());//from w ww  .  j a  v  a 2s  .  co  m

    remaining = Sets.difference(remaining, res.keySet());

    if (!remaining.isEmpty()) {
        Map<NamedKey, byte[]> res2 = getBulkL2(remaining);
        for (Map.Entry<NamedKey, byte[]> entry : res2.entrySet()) {
            level1.put(entry.getKey(), entry.getValue());
        }

        int size = res2.size();
        hitCount.addAndGet(size);
        missCount.addAndGet(remaining.size() - size);

        if (size != 0) {
            res = Maps.newHashMap(res);
            res.putAll(res2);
        }
    }
    return res;
}

From source file:org.graylog2.decorators.DecoratorProcessorImpl.java

@Override
public SearchResponse decorate(SearchResponse searchResponse, Optional<String> streamId) {
    try {/*from www  .ja v  a2  s  .co  m*/
        final List<SearchResponseDecorator> searchResponseDecorators = streamId.isPresent()
                ? decoratorResolver.searchResponseDecoratorsForStream(streamId.get())
                : decoratorResolver.searchResponseDecoratorsForGlobal();
        final Optional<SearchResponseDecorator> metaDecorator = searchResponseDecorators.stream()
                .reduce((f, g) -> (v) -> g.apply(f.apply(v)));
        if (metaDecorator.isPresent()) {
            final Map<String, ResultMessageSummary> originalMessages = searchResponse.messages().stream()
                    .collect(Collectors.toMap(message -> message.message().get("_id").toString(),
                            Function.identity()));
            final SearchResponse newSearchResponse = metaDecorator.get().apply(searchResponse);
            final Set<String> newFields = extractFields(newSearchResponse.messages());
            final Set<String> addedFields = Sets.difference(newFields, searchResponse.fields()).stream()
                    .filter(field -> !Message.RESERVED_FIELDS.contains(field) && !field.equals("streams"))
                    .collect(Collectors.toSet());

            final List<ResultMessageSummary> decoratedMessages = newSearchResponse.messages().stream()
                    .map(resultMessage -> {
                        final ResultMessageSummary originalMessage = originalMessages
                                .get(resultMessage.message().get("_id").toString());
                        if (originalMessage != null) {
                            return resultMessage
                                    .toBuilder().decorationStats(DecorationStats
                                            .create(originalMessage.message(), resultMessage.message()))
                                    .build();
                        }
                        return resultMessage;
                    }).collect(Collectors.toList());
            return newSearchResponse.toBuilder().messages(decoratedMessages).fields(newFields)
                    .decorationStats(SearchDecorationStats.create(addedFields)).build();
        }
    } catch (Exception e) {
        LOG.error("Error decorating search response", e);
    }

    return searchResponse;
}

From source file:org.apache.aurora.scheduler.http.api.security.ShiroIniParser.java

@Override
public Ini doParse(String raw) throws IllegalArgumentException {
    Ini ini;/*www. jav  a2 s . c o m*/
    try {
        ini = Ini.fromResourcePath(raw);
    } catch (ConfigurationException e) {
        throw new ShiroConfigurationException(e);
    }

    Set<String> presentSections = ImmutableSortedSet.copyOf(ini.getSectionNames());
    if (presentSections.isEmpty()) {
        throw new MissingSectionsException();
    }

    Set<String> extraSections = Sets.difference(presentSections, ALLOWED_SECTION_NAMES);
    if (!extraSections.isEmpty()) {
        throw new ExtraSectionsException(extraSections);
    }

    return ini;
}

From source file:com.google.errorprone.bugpatterns.RequiredModifiersChecker.java

@Override
public Description matchAnnotation(AnnotationTree tree, VisitorState state) {
    RequiredModifiers annotation = ASTHelpers.getAnnotation(tree, RequiredModifiers.class);
    if (annotation == null) {
        return Description.NO_MATCH;
    }/* ww  w  .  j  a  v a  2s  .co m*/
    Set<Modifier> requiredModifiers = ImmutableSet.copyOf(annotation.value());
    if (requiredModifiers.isEmpty()) {
        return Description.NO_MATCH;
    }

    Tree parent = state.getPath().getParentPath().getLeaf();
    if (!(parent instanceof ModifiersTree)) {
        // e.g. An annotated package name
        return Description.NO_MATCH;
    }

    Set<Modifier> missing = Sets.difference(requiredModifiers, ((ModifiersTree) parent).getFlags());

    if (missing.isEmpty()) {
        return Description.NO_MATCH;
    }

    String annotationName = ASTHelpers.getAnnotationName(tree);
    String nameString = annotationName != null ? String.format("The annotation '@%s'", annotationName)
            : "This annotation";
    String customMessage = String.format(MESSAGE_TEMPLATE, nameString, missing.toString());
    return buildDescription(tree).setMessage(customMessage).build();
}

From source file:com.google.copybara.transform.TemplateTokens.java

TemplateTokens(Location location, String template, Map<String, Pattern> regexGroups, boolean repeatedGroups)
        throws EvalException {
    this.location = location;
    this.template = Preconditions.checkNotNull(template);

    Builder builder = new Builder();
    builder.location = location;/*  ww w.  j a  va  2s.c o m*/
    builder.parse(template);
    this.before = builder.buildBefore(regexGroups, repeatedGroups);
    this.groupIndexes = ArrayListMultimap.create(builder.groupIndexes);
    this.tokens = ImmutableList.copyOf(builder.tokens);
    this.unusedGroups = Sets.difference(regexGroups.keySet(), groupIndexes.keySet());
}

From source file:org.onosproject.store.resource.impl.UnifiedDiscreteResources.java

@Override
public DiscreteResources difference(DiscreteResources other) {
    if (other instanceof UnifiedDiscreteResources) {
        UnifiedDiscreteResources cast = (UnifiedDiscreteResources) other;
        return new UnifiedDiscreteResources(this.generics.difference(cast.generics),
                this.encodables.difference(cast.encodables));
    } else if (other instanceof EmptyDiscreteResources) {
        return this;
    }/*from w ww  .  jav  a2s  .  c  o m*/

    return of(Sets.difference(this.values(), other.values()));
}

From source file:com.tesora.dve.groupmanager.SimpleMembershipView.java

@Override
public Set<InetSocketAddress> inactiveQuorumMembers() {
    return Sets.difference(catalogRegistered, clusterReachable);
}

From source file:no.ssb.vtl.script.operations.union.UnionOperation.java

private void checkDataStructures(DataStructure baseDataStructure, DataStructure nextDataStructure) {
    // Identifiers and attribute should be equals in name, role and type.
    Set<String> requiredNames = nonAttributeNames(baseDataStructure);
    Set<String> providedNames = nonAttributeNames(nextDataStructure);

    checkArgument(requiredNames.equals(providedNames),
            "dataset was incompatible with the required data structure, missing: %s, unexpected %s",
            Sets.difference(requiredNames, providedNames), Sets.difference(providedNames, requiredNames));

    Map<String, Component.Role> requiredRoles = Maps.filterKeys(baseDataStructure.getRoles(),
            requiredNames::contains);/*ww w. ja  v  a 2s . c  om*/
    Map<String, Component.Role> providedRoles = Maps.filterKeys(nextDataStructure.getRoles(),
            requiredNames::contains);

    checkArgument(requiredRoles.equals(providedRoles),
            "dataset was incompatible with the required data structure, missing: %s, unexpected %s",
            Sets.difference(requiredRoles.entrySet(), providedRoles.entrySet()),
            Sets.difference(providedRoles.entrySet(), requiredRoles.entrySet()));

    Map<String, Class<?>> requiredTypes = Maps.filterKeys(baseDataStructure.getTypes(),
            requiredNames::contains);
    Map<String, Class<?>> providedTypes = Maps.filterKeys(nextDataStructure.getTypes(),
            requiredNames::contains);

    checkArgument(requiredTypes.equals(providedTypes),
            "dataset was incompatible with the required data structure, missing: %s, unexpected %s",
            Sets.difference(requiredTypes.entrySet(), providedTypes.entrySet()),
            Sets.difference(providedTypes.entrySet(), requiredTypes.entrySet()));

}

From source file:org.caleydo.view.domino.api.model.typed.TypedGroups.java

/**
 * @param a/*from  w w w. j a v a2  s.  co  m*/
 * @param b
 * @return
 */
public static TypedGroupSet union(ITypedGroupCollection a, ITypedGroupCollection b) {
    if (a.isEmpty())
        return b.asSet();
    if (b.isEmpty())
        return a.asSet();
    if (a.getIdType() == b.getIdType()) {
        TypedGroupSet base = a.asSet();
        Set<Integer> acc = new BitSetSet();
        acc.addAll(base);
        List<TypedSetGroup> groups = new ArrayList<>(base.getGroups());

        for (ITypedGroup g : b.getGroups()) {
            Set<Integer> ids = g.asSet();
            ids = ImmutableSet.copyOf(Sets.difference(ids, acc));
            if (ids.isEmpty())
                continue;
            acc.addAll(ids);
            groups.add(new TypedSetGroup(ids, a.getIdType(), g.getLabel(), g.getColor()));
        }
        return asSet(groups, a.getIdType());
    } else {
        // map both to primary to ensure both can be represented
        final IDType target = a.getIdType().getIDCategory().getPrimaryMappingType();

        Set<Integer> acc = new BitSetSet();
        List<TypedSetGroup> groups = new ArrayList<>();

        IIDTypeMapper<Integer, Integer> mapper = MappingCaches.findMapper(a.getIdType(), target);
        for (ITypedGroup g : a.getGroups()) {
            Set<Integer> ids = mapper.apply(g);
            ids = acc.isEmpty() ? ids : ImmutableSet.copyOf(Sets.difference(ids, acc));
            if (ids.isEmpty())
                continue;
            acc.addAll(ids);
            groups.add(new TypedSetGroup(ids, target, g.getLabel(), g.getColor()));
        }
        mapper = MappingCaches.findMapper(b.getIdType(), target);
        for (ITypedGroup g : b.getGroups()) {
            Set<Integer> ids = mapper.apply(g);
            ids = acc.isEmpty() ? ids : ImmutableSet.copyOf(Sets.difference(ids, acc));
            if (ids.isEmpty())
                continue;
            acc.addAll(ids);
            groups.add(new TypedSetGroup(ids, target, g.getLabel(), g.getColor()));
        }
        return asSet(groups, target);
    }
}