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

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

Introduction

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

Prototype

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

Source Link

Document

Returns an unmodifiable view of the union of two sets.

Usage

From source file:com.palantir.atlasdb.keyvalue.cassandra.ManyClientPoolingContainer.java

public synchronized void setNewHosts(CassandraKeyValueServiceConfig config) {
    String keyspace = config.keyspace();
    int poolSize = config.poolSize();
    boolean isSsl = config.ssl();
    int socketTimeoutMillis = config.socketTimeoutMillis();
    int socketQueryTimeoutMillis = config.socketQueryTimeoutMillis();

    Set<InetSocketAddress> toRemove = Sets.difference(containerMap.keySet(), config.servers()).immutableCopy();
    Set<InetSocketAddress> toAdd = Sets.difference(config.servers(), containerMap.keySet()).immutableCopy();
    for (InetSocketAddress addr : toRemove) {
        PoolingContainer<Client> pool = containerMap.remove(addr);
        Preconditions.checkNotNull(pool);
        log.warn("Shutting down client pool for {}", addr);
        pool.shutdownPooling();/* w w w  .jav  a  2 s . com*/
    }

    if (!toAdd.isEmpty()) {
        CassandraVerifier.sanityCheckRingConsistency(Sets.union(containerMap.keySet(), toAdd), keyspace, isSsl,
                safetyDisabled, socketTimeoutMillis, socketQueryTimeoutMillis);
    }

    for (InetSocketAddress addr : toAdd) {
        if (isShutdown) {
            log.warn("client Pool is shutdown, cannot add hosts:{}", toAdd);
            break;
        }
        PoolingContainer<Client> newPool = createPool(addr, keyspace, poolSize, isSsl, socketTimeoutMillis,
                socketQueryTimeoutMillis);
        containerMap.put(addr, newPool);
        log.info("Created pool {} for host {}", newPool, addr);
    }
    containers = ImmutableList.copyOf(containerMap.values());
}

From source file:org.apache.drill.exec.expr.fn.HiveFunctionRegistry.java

@Override
public void register(DrillOperatorTable operatorTable) {
    for (String name : Sets.union(methodsGenericUDF.asMap().keySet(), methodsUDF.asMap().keySet())) {
        operatorTable.add(name, new HiveUDFOperator(name.toUpperCase()));
    }/*  w  ww  . java 2s.co m*/
}

From source file:io.druid.query.select.SelectBinaryFn.java

private Set<String> mergeColumns(final Set<String> arg1, final Set<String> arg2) {
    if (arg1.isEmpty()) {
        return arg2;
    }//from w  w  w  . j a  va  2 s  .co m

    if (arg2.isEmpty()) {
        return arg1;
    }

    if (arg1.equals(arg2)) {
        return arg1;
    }

    return Sets.union(arg1, arg2);
}

From source file:com.github.rinde.rinsim.central.rt.ScheduleUtil.java

static List<List<Parcel>> fixSchedule(ImmutableList<ImmutableList<Parcel>> schedule, GlobalStateObject state) {

    checkArgument(schedule.size() == state.getVehicles().size(),
            "The number of routes (%s) and the number of vehicles (%s) must " + "be equal.", schedule.size(),
            state.getVehicles().size());
    checkArgument(!state.getVehicles().get(0).getRoute().isPresent(),
            "A state object without routes is expected.");

    // only parcels in this set may occur in the schedule
    final Set<Parcel> undeliveredParcels = new HashSet<>();
    undeliveredParcels.addAll(state.getAvailableParcels());

    // for each vehicle, we create a multiset that is a representation of the
    // number of times the occurrence of a parcel is REQUIRED to be in the
    // route of the vehicle
    final List<Multiset<Parcel>> expectedRoutes = new ArrayList<>();
    for (int i = 0; i < state.getVehicles().size(); i++) {
        expectedRoutes.add(HashMultiset.<Parcel>create());
        final VehicleStateObject vehicle = state.getVehicles().get(i);
        expectedRoutes.get(i).addAll(vehicle.getContents());
        if (vehicle.getDestination().isPresent()
                && !vehicle.getContents().contains(vehicle.getDestination().get())) {
            expectedRoutes.get(i).add(vehicle.getDestination().get(), 2);
        }// ww w . j  a v a2  s.  co  m
        undeliveredParcels.addAll(vehicle.getContents());
    }

    // create map of parcel -> vehicle index
    final Multimap<Parcel, Integer> parcelOwner = LinkedHashMultimap.create();
    for (int i = 0; i < schedule.size(); i++) {
        final List<Parcel> route = schedule.get(i);
        final Set<Parcel> routeSet = ImmutableSet.copyOf(route);
        for (final Parcel p : routeSet) {
            parcelOwner.put(p, i);
        }
    }

    // copy schedule into a modifiable structure
    final List<List<Parcel>> newSchedule = new ArrayList<>();
    for (final ImmutableList<Parcel> route : schedule) {
        newSchedule.add(new ArrayList<>(route));
    }
    // compare with current vehicle cargo
    for (int i = 0; i < state.getVehicles().size(); i++) {
        final VehicleStateObject vehicle = state.getVehicles().get(i);
        final Multiset<Parcel> routeSet = ImmutableMultiset.copyOf(schedule.get(i));

        final Set<Parcel> test = Sets.union(routeSet.elementSet(), expectedRoutes.get(i).elementSet());

        for (final Parcel p : test) {
            final int actualOccurences = routeSet.count(p);
            checkState(actualOccurences <= 2);
            final int expectedOccurrences = expectedRoutes.get(i).count(p);

            if (!undeliveredParcels.contains(p)) {
                // it is already delivered, remove all occurrences
                newSchedule.get(i).removeAll(Collections.singleton(p));
            } else if (actualOccurences != expectedOccurrences && expectedOccurrences > 0) {
                if (expectedOccurrences == 1 && actualOccurences == 2) {
                    newSchedule.get(i).remove(p);
                } else {
                    // expected occurr = 1 or 2
                    final boolean destinationIsCurrent = vehicle.getDestination().asSet().contains(p);

                    int toAdd = expectedOccurrences - actualOccurences;

                    // add it once at the front of the route
                    if (destinationIsCurrent) {
                        newSchedule.get(i).add(0, p);
                        toAdd--;
                    }

                    // add it once to the end of the route
                    if (toAdd > 0) {
                        newSchedule.get(i).add(p);
                    }
                }
            }

            // if the parcel is expected in the current vehicle, but it also appears
            // in (an) other vehicle(s), we have to remove it there
            if (expectedOccurrences > 0 && parcelOwner.containsKey(p)) {
                for (final Integer v : parcelOwner.get(p)) {
                    if (!v.equals(i)) {
                        newSchedule.get(v).removeAll(Collections.singleton(p));
                    }
                }
            }
        }

        if (vehicle.getDestination().isPresent()
                && !newSchedule.get(i).get(0).equals(vehicle.getDestination().get())) {
            newSchedule.get(i).remove(vehicle.getDestination().get());
            newSchedule.get(i).add(0, vehicle.getDestination().get());
        }
    }
    return newSchedule;
}

From source file:com.opengamma.strata.market.curve.CurveGroupEntry.java

/**
 * Merges the specified entry with this entry, returning a new entry.
 * <p>//ww  w  .  j  av a 2  s .  co  m
 * The two entries must have the same curve name.
 * 
 * @param newEntry  the new entry
 * @return the merged entry
 */
CurveGroupEntry merge(CurveGroupEntry newEntry) {
    if (!curveName.equals(newEntry.curveName)) {
        throw new IllegalArgumentException(Messages.format(
                "A CurveGroupEntry can only be merged with an entry with the same curve name. name: {}, other name: {}",
                curveName, newEntry.curveName));
    }
    return CurveGroupEntry.builder().curveName(curveName)
            .discountCurrencies(Sets.union(discountCurrencies, newEntry.discountCurrencies))
            .indices(Sets.union(indices, newEntry.indices)).build();
}

From source file:com.google.devtools.moe.client.svn.SvnWriter.java

private DraftRevision putCodebase(Codebase c) {
    c.checkProjectSpace(config.getProjectSpace());

    // Filter out files that either start with .svn or have .svn after a slash, plus the repo
    // config's ignore_file_res.
    List<String> ignoreFilePatterns = ImmutableList.<String>builder().addAll(config.getIgnoreFilePatterns())
            .add("(^|.*/)\\.svn(/.*|$)").build();

    Set<String> codebaseFiles = c.getRelativeFilenames();
    Set<String> writerFiles = Utils.filterByRegEx(
            Utils.makeFilenamesRelative(Injector.INSTANCE.fileSystem().findFiles(rootDirectory), rootDirectory),
            ignoreFilePatterns);//from www . j  av a2 s .com
    Set<String> union = Sets.union(codebaseFiles, writerFiles);

    for (String filename : union) {
        putFile(filename, c);
    }

    return new SvnDraftRevision(rootDirectory);
}

From source file:grakn.core.graql.reasoner.cache.VariableDefinition.java

public VariableDefinition merge(VariableDefinition def) {
    if (!var().equals(def.var())) {
        throw new IllegalStateException("Illegal variable definition merge between:\n" + this + "and\n" + def);
    }/*from   w ww.  java2s . c  o  m*/
    return new VariableDefinition(var, def.type() != null ? def.type() : this.type(),
            def.role() != null ? def.role() : this.role(), Sets.union(def.playedRoles(), this.playedRoles()),
            Sets.union(def.valuePredicates(), this.valuePredicates()));
}

From source file:org.dishevelled.venn.model.BinaryVennModelImpl.java

/**
 * Create a new binary venn model with the specified sets.
 *
 * @param first first set, must not be null
 * @param second second set, must not be null
 *///w ww  .ja v a  2s. c om
public BinaryVennModelImpl(final Set<? extends E> first, final Set<? extends E> second) {
    if (first == null) {
        throw new IllegalArgumentException("first must not be null");
    }
    if (second == null) {
        throw new IllegalArgumentException("second must not be null");
    }

    // todo  defensive copy?
    this.first = new ObservableSetImpl(first);
    this.second = new ObservableSetImpl(second);
    firstOnly = Sets.difference(this.first, this.second);
    secondOnly = Sets.difference(this.second, this.first);
    intersection = Sets.intersection(this.first, this.second);
    union = Sets.union(this.first, this.second);
    selection = new SelectionView<E>(union, this.first, this.second);

    exclusives = new HashMap<ImmutableBitSet, Set<E>>(3);

    exclusives.put(toImmutableBitSet(0), firstOnly);
    exclusives.put(toImmutableBitSet(1), secondOnly);

    exclusives.put(toImmutableBitSet(0, 1), intersection);
    // copy to immutable map?
}

From source file:org.sosy_lab.cpachecker.cfa.ast.c.CIdExpressionCollectingVisitor.java

@Override
public Set<CIdExpression> visit(CDesignatedInitializer pI) throws RuntimeException {
    Set<CIdExpression> result = Collections.emptySet();
    for (CDesignator d : pI.getDesignators()) {
        result = Sets.union(result, d.accept(this));
    }//w  w w  . java2  s  . c o m
    if (pI.getRightHandSide() != null) {
        result = Sets.union(result, pI.getRightHandSide().accept(this));
    }
    return result;
}

From source file:org.apache.lens.server.query.constraint.DefaultQueryLaunchingConstraintsChecker.java

@VisibleForTesting
Set<QueryLaunchingConstraint> prepareAllConstraints(final QueryContext candidateQuery) {

    ImmutableSet<QueryLaunchingConstraint> driverConstraints = candidateQuery
            .getSelectedDriverQueryConstraints();
    BackOffRetryHandler<QueryContext> retryPolicy = candidateQuery.getRetryPolicy();
    Sets.SetView<QueryLaunchingConstraint> constraints = Sets.union(this.lensQueryConstraints,
            driverConstraints);//from w  ww.  j av a2  s  . co  m
    if (retryPolicy == null) {
        return constraints;
    } else {
        return Sets.union(Collections.singleton(new RetryPolicyToConstraingAdapter(retryPolicy)), constraints);
    }
}