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:brooklyn.location.waratek.WaratekResolver.java

protected Location newLocationFromString(String spec, brooklyn.location.LocationRegistry registry,
        Map properties, Map locationFlags) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Resolving location '" + spec + "' with flags "
                + Joiner.on(",").withKeyValueSeparator("=").join(locationFlags));
    }/*from   w  ww  .ja v  a 2 s .c o m*/
    String namedLocation = (String) locationFlags.get(LocationInternal.NAMED_SPEC_NAME.getName());

    Matcher matcher = PATTERN.matcher(spec);
    if (!matcher.matches()) {
        throw new IllegalArgumentException("Invalid location '" + spec
                + "'; must specify something like waratek:entityId or waratek:entityId:(name=abc)");
    }

    String argsPart = matcher.group(6);
    Map<String, String> argsMap = (argsPart != null) ? KeyValueParser.parseMap(argsPart)
            : Collections.<String, String>emptyMap();
    String displayNamePart = argsMap.get("displayName");
    String namePart = argsMap.get("name");

    if (!ACCEPTABLE_ARGS.containsAll(argsMap.keySet())) {
        Set<String> illegalArgs = Sets.difference(argsMap.keySet(), ACCEPTABLE_ARGS);
        throw new IllegalArgumentException("Invalid location '" + spec + "'; illegal args " + illegalArgs
                + "; acceptable args are " + ACCEPTABLE_ARGS);
    }
    if (argsMap.containsKey("displayName") && Strings.isEmpty(displayNamePart)) {
        throw new IllegalArgumentException(
                "Invalid location '" + spec + "'; if displayName supplied then value must be non-empty");
    }
    if (argsMap.containsKey("name") && Strings.isEmpty(namePart)) {
        throw new IllegalArgumentException(
                "Invalid location '" + spec + "'; if name supplied then value must be non-empty");
    }

    Map<String, Object> filteredProperties = new LocationPropertiesFromBrooklynProperties()
            .getLocationProperties(WARATEK, namedLocation, properties);
    MutableMap<String, Object> flags = MutableMap.<String, Object>builder().putAll(filteredProperties)
            .putAll(locationFlags).build();

    String infrastructureId = matcher.group(2);
    if (Strings.isBlank(infrastructureId)) {
        throw new IllegalArgumentException(
                "Invalid location '" + spec + "'; infrastructure entity id must be non-empty");
    }
    String jvmId = matcher.group(4);

    // Build the display name
    StringBuilder name = new StringBuilder();
    if (displayNamePart != null) {
        name.append(displayNamePart);
    } else {
        name.append("Waratek ");
        if (jvmId == null) {
            name.append("Infrastructure ").append(infrastructureId);
        } else {
            name.append("JVM ").append(jvmId);
        }
    }
    final String displayName = name.toString();

    // Build the location name
    name = new StringBuilder();
    if (namePart != null) {
        name.append(namePart);
    } else {
        name.append("waratek-");
        name.append(infrastructureId);
        if (jvmId != null) {
            name.append("-").append(jvmId);
        }
    }
    final String locationName = name.toString();
    WaratekInfrastructure infrastructure = (WaratekInfrastructure) managementContext.getEntityManager()
            .getEntity(infrastructureId);

    if (jvmId == null) {
        LocationSpec<WaratekLocation> locationSpec = LocationSpec.create(WaratekLocation.class).configure(flags)
                .configure(DynamicLocation.OWNER, infrastructure)
                .configure(LocationInternal.NAMED_SPEC_NAME, locationName).displayName(displayName);
        return managementContext.getLocationManager().createLocation(locationSpec);
    } else {
        JavaVirtualMachine jvm = (JavaVirtualMachine) managementContext.getEntityManager().getEntity(jvmId);

        LocationSpec<WaratekMachineLocation> locationSpec = LocationSpec.create(WaratekMachineLocation.class)
                .parent(infrastructure.getDynamicLocation()).configure(flags)
                .configure(DynamicLocation.OWNER, jvm).configure(LocationInternal.NAMED_SPEC_NAME, locationName)
                .displayName(displayName);
        return managementContext.getLocationManager().createLocation(locationSpec);
    }
}

From source file:brooklyn.location.docker.DockerResolver.java

protected Location newLocationFromString(String spec, LocationRegistry registry, Map properties,
        Map locationFlags) {/*from   w w  w  . java  2 s  . c o m*/
    if (LOG.isDebugEnabled()) {
        LOG.debug("Resolving location '" + spec + "' with flags "
                + Joiner.on(",").withKeyValueSeparator("=").join(locationFlags));
    }
    String namedLocation = (String) locationFlags.get(LocationInternal.NAMED_SPEC_NAME.getName());

    Matcher matcher = PATTERN.matcher(spec);
    if (!matcher.matches()) {
        throw new IllegalArgumentException("Invalid location '" + spec
                + "'; must specify something like docker:entityId or docker:entityId:(name=abc)");
    }

    String argsPart = matcher.group(6);
    Map<String, String> argsMap = (argsPart != null) ? KeyValueParser.parseMap(argsPart)
            : Collections.<String, String>emptyMap();
    String displayNamePart = argsMap.get("displayName");
    String namePart = argsMap.get("name");

    if (!ACCEPTABLE_ARGS.containsAll(argsMap.keySet())) {
        Set<String> illegalArgs = Sets.difference(argsMap.keySet(), ACCEPTABLE_ARGS);
        throw new IllegalArgumentException("Invalid location '" + spec + "'; illegal args " + illegalArgs
                + "; acceptable args are " + ACCEPTABLE_ARGS);
    }
    if (argsMap.containsKey("displayName") && Strings.isEmpty(displayNamePart)) {
        throw new IllegalArgumentException(
                "Invalid location '" + spec + "'; if displayName supplied then value must be non-empty");
    }
    if (argsMap.containsKey("name") && Strings.isEmpty(namePart)) {
        throw new IllegalArgumentException(
                "Invalid location '" + spec + "'; if name supplied then value must be non-empty");
    }

    Map<String, Object> filteredProperties = new LocationPropertiesFromBrooklynProperties()
            .getLocationProperties(DOCKER, namedLocation, properties);
    MutableMap<String, Object> flags = MutableMap.<String, Object>builder().putAll(filteredProperties)
            .putAll(locationFlags).build();

    String infrastructureId = matcher.group(2);
    if (Strings.isBlank(infrastructureId)) {
        throw new IllegalArgumentException(
                "Invalid location '" + spec + "'; infrastructure entity id must be non-empty");
    }
    String dockerHostId = matcher.group(4);

    // Build the display name
    StringBuilder name = new StringBuilder();
    if (displayNamePart != null) {
        name.append(displayNamePart);
    } else {
        name.append("Docker ");
        if (dockerHostId == null) {
            name.append("Infrastructure ").append(infrastructureId);
        } else {
            name.append("Host ").append(dockerHostId);
        }
    }
    final String displayName = name.toString();

    // Build the location name
    name = new StringBuilder();
    if (namePart != null) {
        name.append(namePart);
    } else {
        name.append("docker-");
        name.append(infrastructureId);
        if (dockerHostId != null) {
            name.append("-").append(dockerHostId);
        }
    }
    final String locationName = name.toString();
    DockerInfrastructure infrastructure = (DockerInfrastructure) managementContext.getEntityManager()
            .getEntity(infrastructureId);
    Iterable<Location> managedLocations = managementContext.getLocationManager().getLocations();

    if (dockerHostId == null) {
        for (Location location : managedLocations) {
            if (location instanceof DockerLocation) {
                if (((DockerLocation) location).getOwner().getId().equals(infrastructureId)) {
                    return location;
                }
            }
        }
        LocationSpec<DockerLocation> locationSpec = LocationSpec.create(DockerLocation.class).configure(flags)
                .configure(DynamicLocation.OWNER, infrastructure)
                .configure(LocationInternal.NAMED_SPEC_NAME, locationName).displayName(displayName);
        return managementContext.getLocationManager().createLocation(locationSpec);
    } else {
        DockerHost dockerHost = (DockerHost) managementContext.getEntityManager().getEntity(dockerHostId);
        for (Location location : managedLocations) {
            if (location instanceof DockerHostLocation) {
                if (((DockerHostLocation) location).getOwner().getId().equals(dockerHostId)) {
                    return location;
                }
            }
        }

        LocationSpec<DockerHostLocation> locationSpec = LocationSpec.create(DockerHostLocation.class)
                .parent(infrastructure.getDynamicLocation()).configure(flags)
                .configure(DynamicLocation.OWNER, dockerHost)
                .configure(LocationInternal.NAMED_SPEC_NAME, locationName).displayName(displayName);
        return managementContext.getLocationManager().createLocation(locationSpec);
    }
}

From source file:org.apache.storm.scheduler.EvenScheduler.java

private static Map<ExecutorDetails, WorkerSlot> scheduleTopology(TopologyDetails topology, Cluster cluster) {
    List<WorkerSlot> availableSlots = cluster.getAvailableSlots();
    Set<ExecutorDetails> allExecutors = topology.getExecutors();
    Map<WorkerSlot, List<ExecutorDetails>> aliveAssigned = getAliveAssignedWorkerSlotExecutors(cluster,
            topology.getId());//from w w  w  .j  ava 2s.  c  o m
    int totalSlotsToUse = Math.min(topology.getNumWorkers(), availableSlots.size() + aliveAssigned.size());

    List<WorkerSlot> sortedList = sortSlots(availableSlots);
    if (sortedList == null) {
        LOG.error("No available slots for topology: {}", topology.getName());
        return new HashMap<ExecutorDetails, WorkerSlot>();
    }

    //allow requesting slots number bigger than available slots
    int toIndex = (totalSlotsToUse - aliveAssigned.size()) > sortedList.size() ? sortedList.size()
            : (totalSlotsToUse - aliveAssigned.size());
    List<WorkerSlot> reassignSlots = sortedList.subList(0, toIndex);

    Set<ExecutorDetails> aliveExecutors = new HashSet<ExecutorDetails>();
    for (List<ExecutorDetails> list : aliveAssigned.values()) {
        aliveExecutors.addAll(list);
    }
    Set<ExecutorDetails> reassignExecutors = Sets.difference(allExecutors, aliveExecutors);

    Map<ExecutorDetails, WorkerSlot> reassignment = new HashMap<ExecutorDetails, WorkerSlot>();
    if (reassignSlots.size() == 0) {
        return reassignment;
    }

    List<ExecutorDetails> executors = new ArrayList<ExecutorDetails>(reassignExecutors);
    Collections.sort(executors, new Comparator<ExecutorDetails>() {
        @Override
        public int compare(ExecutorDetails o1, ExecutorDetails o2) {
            return o1.getStartTask() - o2.getStartTask();
        }
    });

    for (int i = 0; i < executors.size(); i++) {
        reassignment.put(executors.get(i), reassignSlots.get(i % reassignSlots.size()));
    }

    if (reassignment.size() != 0) {
        LOG.info("Available slots: {}", availableSlots.toString());
    }
    return reassignment;
}

From source file:org.apache.storm.command.AdminCommands.java

private static Iterator<String> listCorruptTopologies() {
    Set<String> blobStoreTopologyIds = nimbusBlobStore.filterAndListKeys(new KeyFilter<String>() {
        @Override//www. ja v a  2  s. c o  m
        public String filter(String key) {
            return ConfigUtils.getIdFromBlobKey(key);
        }
    });
    Set<String> activeTopologyIds = new HashSet<>(stormClusterState.activeStorms());
    Sets.SetView<String> diffTopology = Sets.difference(activeTopologyIds, blobStoreTopologyIds);
    LOG.info("active-topology-ids [{}] blob-topology-ids [{}] diff-topology [{}]", activeTopologyIds.toString(),
            blobStoreTopologyIds.toString(), diffTopology.toString());
    return diffTopology.iterator();
}

From source file:org.apache.zeppelin.mysql.SqlCompleter.java

public void updateDataModelMetaData(Connection connection) {

    try {/*ww w  . j  a  v a  2 s .co  m*/
        Set<String> newModelCompletions = getDataModelMetadataCompletions(connection);
        logger.debug("New model metadata is:" + Joiner.on(',').join(newModelCompletions));

        // Sets.difference(set1, set2) - returned set contains all elements that are contained by set1
        // and not contained by set2. set2 may also contain elements not present in set1; these are
        // simply ignored.
        SetView<String> removedCompletions = Sets.difference(modelCompletions, newModelCompletions);
        logger.debug("Removed Model Completions: " + Joiner.on(',').join(removedCompletions));
        this.getStrings().removeAll(removedCompletions);

        SetView<String> newCompletions = Sets.difference(newModelCompletions, modelCompletions);
        logger.debug("New Completions: " + Joiner.on(',').join(newCompletions));
        this.getStrings().addAll(newCompletions);

        modelCompletions = newModelCompletions;

    } catch (SQLException e) {
        logger.error("Failed to update the metadata completions", e);
    }
}

From source file:de.unipassau.isl.evs.ssh.slave.handler.SlaveModuleHandler.java

/**
 * Updates the SlaveContainer. Registers new modules and unregisters unused modules.
 *
 * @param components a list of modules to add. All modules in the given list are registered in
 *                   the SaveContainer. All modules that are in the container but not in the
 *                   list are unregistered.
 *//*from   w  w  w  .  j  a  v  a  2  s  .  c  o m*/
private void updateModule(List<Module> components) throws WrongAccessPointException, EvsIoException {
    if (this.components.size() < 1) {
        final Set<Module> newComponents = Sets.newHashSet(components);
        registerModules(newComponents);
    } else {
        final Set<Module> oldComponents = Sets.newHashSet(this.components);
        final Set<Module> newComponents = Sets.newHashSet(components);
        final Set<Module> componentsToRemove = Sets.difference(oldComponents, newComponents);
        final Set<Module> componentsToAdd = Sets.difference(newComponents, oldComponents);

        unregisterModule(componentsToRemove);
        registerModules(componentsToAdd);
    }
    this.components.clear();
    this.components.addAll(components);
}

From source file:co.cask.cdap.security.authorization.DefaultAuthorizationEnforcementService.java

private boolean doEnforce(EntityId entity, Principal principal, Set<Action> actions, boolean exceptionOnFailure)
        throws Exception {
    if (entity instanceof ParentedId) {
        if (doEnforce(((ParentedId) entity).getParent(), principal, actions, false)) {
            return true;
        }//w  w w  . ja  v  a 2 s.  c  o  m
    }

    Set<Action> allowedActions = getPrivileges(principal).get(entity);
    LOG.trace("Enforcing actions {} on {} for {}. Allowed actions are {}", actions, entity, principal,
            allowedActions);
    if (allowedActions == null) {
        if (exceptionOnFailure) {
            throw new UnauthorizedException(principal, actions, entity);
        }
        return false;
    }

    // Check for the specific actions requested
    if (allowedActions.containsAll(actions)) {
        return true;
    }
    if (exceptionOnFailure) {
        throw new UnauthorizedException(principal, Sets.difference(actions, allowedActions), entity);
    }
    return false;
}

From source file:com.palantir.atlasdb.keyvalue.impl.AbstractTableMappingService.java

@Override
public Set<TableReference> mapToFullTableNames(Set<String> tableNames) {
    Set<TableReference> newSet = Sets.newHashSet();
    Set<String> tablesToReload = Sets.newHashSet();
    for (String name : tableNames) {
        if (name.contains(PERIOD)) {
            newSet.add(TableReference.createFromFullyQualifiedName(name));
        } else if (tableMap.get().containsValue(name)) {
            newSet.add(getFullTableName(name));
        } else if (unmappedTables.containsKey(name)) {
            newSet.add(TableReference.createWithEmptyNamespace(name));
        } else {//from   w w w  . j  a  v a2  s .c  om
            tablesToReload.add(name);
        }
    }
    if (!tablesToReload.isEmpty()) {
        updateTableMap();
        for (String tableName : Sets.difference(tablesToReload, tableMap.get().values())) {
            unmappedTables.put(tableName, true);
            newSet.add(TableReference.createWithEmptyNamespace(tableName));
        }
        for (String tableName : Sets.intersection(tablesToReload, tableMap.get().values())) {
            newSet.add(getFullTableName(tableName));
        }
    }
    return newSet;
}

From source file:games.stendhal.server.core.engine.db.StendhalSearchIndexDAO.java

/**
 * dumps the search index//w  ww  . j av  a2 s .  co m
 *
 * @param transaction DBTransaction
 * @param entries required entries
 * @throws SQLException in case of an database error
 */
public void updateSearchIndex(DBTransaction transaction, Set<SearchIndexEntry> entries) throws SQLException {
    Set<SearchIndexEntry> oldEntries = readExistingEntries(transaction);

    Set<SearchIndexEntry> toDelete = Sets.difference(oldEntries, entries);
    Set<SearchIndexEntry> toAdd = Sets.difference(entries, oldEntries);

    deleteObsoleteEntries(transaction, toDelete);
    addNewEntries(transaction, toAdd);
}

From source file:io.druid.cli.InsertSegment.java

private void insertSegments(final Set<DataSegment> segments) throws IOException {
    final Set<DataSegment> segmentsInserted = indexerMetadataStorageCoordinator
            .announceHistoricalSegments(segments);
    for (DataSegment dataSegment : segmentsInserted) {
        log.info("Sucessfully inserted Segment [%s] into metadata storage", dataSegment.getIdentifier());
    }//  w ww. ja  v  a2 s. c  o  m
    final Set<DataSegment> segmentsAlreadyExist = Sets.difference(segments, segmentsInserted);
    if (!segmentsAlreadyExist.isEmpty()) {
        for (DataSegment dataSegment : segmentsAlreadyExist) {
            log.info("Segment [%s] already exists in metadata storage, updating the payload",
                    dataSegment.getIdentifier());
        }
        indexerMetadataStorageCoordinator.updateSegmentMetadata(segmentsAlreadyExist);
    }
}