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

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

Introduction

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

Prototype

@GwtIncompatible("NavigableSet")
@SuppressWarnings("unchecked")
@CheckReturnValue
public static <E> NavigableSet<E> filter(NavigableSet<E> unfiltered, Predicate<? super E> predicate) 

Source Link

Document

Returns the elements of a NavigableSet , unfiltered , that satisfy a predicate.

Usage

From source file:org.apache.provisionr.cloudstack.core.VirtualMachines.java

public static List<String> destroyAllVirtualMachineByName(final CloudStackClient client, final String vmName) {
    checkNotEmpty(vmName);//from w ww . j a  v a 2s  . com

    Set<VirtualMachine> vms = Sets.filter(client.getVirtualMachineClient().listVirtualMachines(
            ListVirtualMachinesOptions.Builder.name(vmName)), new Predicate<VirtualMachine>() {
                @Override
                public boolean apply(VirtualMachine input) {
                    return input != null && vmName.equals(input.getDisplayName());
                }
            });

    List<String> jobIds = Lists.newArrayList();
    LOG.info("Deleting a total of {} virtual machine instances", vms.size());
    for (VirtualMachine vm : vms) {
        LOG.info("Deleting instance with id {}", vm.getId());
        jobIds.add(client.getVirtualMachineClient().destroyVirtualMachine(vm.getId()));
    }
    return ImmutableList.copyOf(jobIds);
}

From source file:co.freeside.betamax.tape.yaml.GroovyRepresenter.java

@Override
protected Set<Property> getProperties(Class<?> type) {
    try {/*from ww  w . j  a  va2s .  co m*/
        Set<Property> propertySet = super.getProperties(type);
        return Sets.filter(propertySet, new Predicate<Property>() {
            @Override
            public boolean apply(Property property) {
                return !property.getName().equals("metaClass");
            }
        });
    } catch (IntrospectionException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.spka.cursus.scoring.AbstractSPKAScorer.java

/**
 * Race scores include all pilots at the event
 *//*from w ww  .  jav  a 2 s  .com*/
@Override
public Scores scoreRace(Race race, Predicate<Pilot> fleetFilter) {
    return super.scoreRace(race, Sets.filter(race.getEvent().getAllPilots(), fleetFilter), fleetFilter);
}

From source file:org.gradle.plugins.ide.idea.internal.IdeaNameDeduper.java

public void configureRoot(Project rootProject) {
    Set<Project> projects = Sets.filter(rootProject.getAllprojects(), HAS_IDEA_PLUGIN);
    Iterable<IdeaModule> ideaModules = Iterables.transform(projects, GET_IDEA_MODULE);
    HierarchicalElementDeduplicator<IdeaModule> deduplicator = new HierarchicalElementDeduplicator<IdeaModule>(
            new IdeaDeduplicationAdapter());
    Map<IdeaModule, String> deduplicated = deduplicator.deduplicate(ideaModules);
    for (Map.Entry<IdeaModule, String> entry : deduplicated.entrySet()) {
        entry.getKey().setName(entry.getValue());
    }//from  w  w w  .j  a va2 s .co  m
}

From source file:com.google.enterprise.connector.pusher.StripAclDocumentFilter.java

@Override
public Set<String> getPropertyNames(Document source) throws RepositoryException {
    return Sets.filter(source.getPropertyNames(), predicate);
}

From source file:org.gradle.plugins.ide.eclipse.internal.EclipseNameDeduper.java

public void configureRoot(Project rootProject) {
    Set<Project> projects = Sets.filter(rootProject.getAllprojects(), HAS_ECLIPSE_PLUGIN);
    ImmutableMap<EclipseProject, Project> eclipseProjects = Maps.uniqueIndex(projects, GET_ECLIPSE_PROJECT);
    HierarchicalElementDeduplicator<EclipseProject> deduplicator = new HierarchicalElementDeduplicator<EclipseProject>(
            new EclipseDeduplicationAdapter(eclipseProjects));
    Map<EclipseProject, String> deduplicated = deduplicator.deduplicate(eclipseProjects.keySet());
    for (Map.Entry<EclipseProject, String> entry : deduplicated.entrySet()) {
        entry.getKey().setName(entry.getValue());
    }//  www . j  a va 2s .  c o  m
}

From source file:com.android.builder.files.RelativeFiles.java

/**
 * Loads all files in a directory recursively, filtering the results with a predicate.
 * Filtering is only done at the end so, even if a directory is excluded from the filter,
 * its files will be included if they are accepted by the filter.
 *
 * @param directory the directory, must exist and be a readable directory
 * @param filter a predicate to filter which files should be included in the result; only
 * files to whom the filter application results in {@code true} are included in the result
 * @return all files in the directory, sub-directories included
 *///from  w ww. ja va 2  s .c om
@NonNull
public static ImmutableSet<RelativeFile> fromDirectory(@NonNull File directory,
        @NonNull final Predicate<RelativeFile> filter) {
    return ImmutableSet.copyOf(Sets.filter(fromDirectory(directory, directory), filter));
}

From source file:eu.lp0.cursus.scoring.scorer.AbstractScorer.java

@Override
public Scores scoreSeries(Series series, Predicate<Pilot> fleetFilter) {
    return scoreSeries(series, Sets.filter(series.getPilots(), fleetFilter), fleetFilter);
}

From source file:com.splicemachine.impl.MockRegionUtils.java

public static HRegion getMockRegion() throws IOException {
    final Map<byte[], Set<Cell>> rowMap = Maps.newTreeMap(Bytes.BYTES_COMPARATOR);
    HRegion fakeRegion = mock(HRegion.class);
    HRegionInfo fakeInfo = mock(HRegionInfo.class);
    when(fakeInfo.getStartKey()).thenReturn(HConstants.EMPTY_BYTE_ARRAY);
    when(fakeInfo.getEndKey()).thenReturn(HConstants.EMPTY_BYTE_ARRAY);
    when(fakeRegion.getRegionInfo()).thenReturn(fakeInfo);
    when(fakeRegion.get(any(Get.class))).thenAnswer(new Answer<Result>() {
        @Override/* ww w . java 2 s  .  c  o  m*/
        public Result answer(InvocationOnMock invocationOnMock) throws Throwable {
            final Get get = (Get) invocationOnMock.getArguments()[0];
            Set<Cell> keyValues = rowMap.get(get.getRow());
            if (get.hasFamilies()) {
                Set<Cell> filtered = Sets.filter(keyValues, new Predicate<Cell>() {
                    @Override
                    public boolean apply(@Nullable Cell input) {
                        Map<byte[], NavigableSet<byte[]>> familyMap = get.getFamilyMap();
                        if (!familyMap.containsKey(input.getFamily()))
                            return false;
                        NavigableSet<byte[]> qualifiers = familyMap.get(input.getFamily());
                        return qualifiers.contains(input.getQualifier());
                    }
                });
                List<Cell> kvs = Lists.newArrayList(filtered);
                return Result.create(kvs);
            } else if (keyValues != null) {
                return Result.create(Lists.newArrayList(keyValues));
            } else
                return null;
        }
    });

    Answer<Void> putAnswer = new Answer<Void>() {
        @Override
        public Void answer(InvocationOnMock invocationOnMock) throws Throwable {
            Put put = (Put) invocationOnMock.getArguments()[0];
            Set<Cell> keyValues = rowMap.get(put.getRow());
            if (keyValues == null) {
                keyValues = Sets.newTreeSet(new KeyValue.KVComparator());
                rowMap.put(put.getRow(), keyValues);
            }
            Map<byte[], List<KeyValue>> familyMap = put.getFamilyMap();
            for (List<KeyValue> kvs : familyMap.values()) {
                for (KeyValue kv : kvs) {
                    boolean ts = !kv.isLatestTimestamp();
                    kv = ts ? kv
                            : new KeyValue(kv.getRow(), kv.getFamily(), kv.getQualifier(),
                                    System.currentTimeMillis(), kv.getValue());
                    if (keyValues.contains(kv)) {
                        keyValues.remove(kv);
                    }
                    keyValues.add(kv);
                }
            }
            return null;
        }
    };
    doAnswer(putAnswer).when(fakeRegion).put(any(Put.class));

    Answer<Void> deleteAnswer = new Answer<Void>() {
        @Override
        public Void answer(InvocationOnMock invocationOnMock) throws Throwable {
            Delete delete = (Delete) invocationOnMock.getArguments()[0];
            Set<Cell> keyValues = rowMap.get(delete.getRow());
            if (keyValues == null)
                return null; //nothing to do, it's already deleted

            long timestamp = delete.getTimeStamp();
            boolean isEmpty = delete.isEmpty();
            if (isEmpty) {
                Iterator<Cell> iter = keyValues.iterator();
                while (iter.hasNext()) {
                    Cell kv = iter.next();
                    if (kv.getTimestamp() == timestamp)
                        iter.remove();
                }
            } else {
                Map<byte[], List<KeyValue>> deleteFamilyMap = delete.getFamilyMap();
                Iterator<Cell> iter = keyValues.iterator();
                while (iter.hasNext()) {
                    Cell kv = iter.next();
                    if (!deleteFamilyMap.containsKey(kv.getFamily()))
                        continue;
                    List<KeyValue> toDelete = deleteFamilyMap.get(kv.getFamily());
                    if (toDelete.size() > 0) {
                        for (KeyValue toDeleteKv : toDelete) {
                            if (toDeleteKv.getQualifier().length <= 0) {
                                //delete everything
                                if (kv.getTimestamp() == toDeleteKv.getTimestamp()) {
                                    iter.remove();
                                    break;
                                }
                            } else if (Bytes.equals(kv.getQualifier(), toDeleteKv.getQualifier())) {
                                if (kv.getTimestamp() == toDeleteKv.getTimestamp()) {
                                    iter.remove();
                                    break;
                                }
                            }
                        }
                    } else {
                        if (kv.getTimestamp() == timestamp)
                            iter.remove();
                    }
                }
            }

            return null;
        }
    };
    doAnswer(deleteAnswer).when(fakeRegion).delete(any(Delete.class));

    when(fakeRegion.getScanner(any(Scan.class))).thenAnswer(new Answer<RegionScanner>() {

        @Override
        public RegionScanner answer(InvocationOnMock invocationOnMock) throws Throwable {
            Scan scan = (Scan) invocationOnMock.getArguments()[0];
            return new IteratorRegionScanner(rowMap.values().iterator(), scan);
        }
    });

    return fakeRegion;
}

From source file:com.android.builder.files.IncrementalRelativeFileSets.java

/**
 * Reads a directory and adds all files in the directory in a new incremental relative set.
 * The status of each file is set to {@link FileStatus#NEW}. This method is used to construct
 * an initial set of files and is, therefore, an incremental update from zero.
 *
 * @param directory the directory, must be an existing directory
 * @return the file set/* www .j av  a 2 s . c o m*/
 */
@NonNull
public static ImmutableMap<RelativeFile, FileStatus> fromDirectory(@NonNull File directory) {
    Preconditions.checkArgument(directory.isDirectory(), "!directory.isDirectory()");
    Set<RelativeFile> files = RelativeFiles.fromDirectory(directory);
    files = Sets.filter(files, Predicates.compose(Files.isFile(), RelativeFile.EXTRACT_FILE));
    Map<RelativeFile, FileStatus> map = Maps.asMap(files, Functions.constant(FileStatus.NEW));
    return ImmutableMap.copyOf(map);
}