Example usage for com.google.common.collect ImmutableMultiset.Builder add

List of usage examples for com.google.common.collect ImmutableMultiset.Builder add

Introduction

In this page you can find the example usage for com.google.common.collect ImmutableMultiset.Builder add.

Prototype

@Override
boolean add(E element);

Source Link

Document

Adds a single occurrence of the specified element to this multiset.

Usage

From source file:com.publictransitanalytics.scoregenerator.output.ComparativeSectorReachInformation.java

private static Map<SimplePath, Integer> getPathCounts(final Set<MovementPath> bestPaths)
        throws InterruptedException {
    final Map<SimplePath, Integer> pathCounts;
    final TreeMultimap<Integer, SimplePath> frequencyMap = TreeMultimap.create(Integer::compareTo,
            (p1, p2) -> p1.toString().compareTo(p2.toString()));

    if (bestPaths != null) {
        final ImmutableMultiset.Builder<SimplePath> bestSimplePathsBuilder = ImmutableMultiset.builder();
        for (final MovementPath bestPath : bestPaths) {
            bestSimplePathsBuilder.add(new SimplePath(bestPath));
        }/*  ww w .  j a  va 2  s.co m*/
        final ImmutableMultiset<SimplePath> bestSimplePaths = bestSimplePathsBuilder.build();

        for (final SimplePath path : bestSimplePaths.elementSet()) {
            frequencyMap.put(bestSimplePaths.count(path), path);
        }

        pathCounts = new LinkedHashMap<>();
        for (final Integer frequency : frequencyMap.keySet().descendingSet()) {
            final NavigableSet<SimplePath> pathsForFrequency = frequencyMap.get(frequency);
            for (final SimplePath pathForFrequency : pathsForFrequency) {
                pathCounts.put(pathForFrequency, frequency);
            }
        }
    } else {
        pathCounts = null;
    }
    return pathCounts;
}

From source file:org.apache.aurora.scheduler.filter.AttributeAggregate.java

private static ImmutableMultiset.Builder<Pair<String, String>> addAttributes(
        ImmutableMultiset.Builder<Pair<String, String>> builder, Iterable<IAttribute> attributes) {

    for (IAttribute attribute : attributes) {
        for (String value : attribute.getValues()) {
            builder.add(Pair.of(attribute.getName(), value));
        }//ww  w. j a  v  a 2s. c  om
    }
    return builder;
}

From source file:com.cloudera.science.ml.client.cmd.KMeansOutlierCommand.java

private void validate(List<Centers> centers, List<Integer> centerIds,
        Map<ClusterKey, MahalanobisDistance> distances) {
    ImmutableMultiset.Builder<Integer> imb = ImmutableMultiset.builder();
    for (ClusterKey ck : distances.keySet()) {
        imb.add(ck.getClusterId());
    }/*from  ww w  . j a  v  a  2s .c o  m*/
    Multiset<Integer> counts = imb.build();
    for (int i = 0; i < centers.size(); i++) {
        int idx = (centerIds == null || centerIds.isEmpty()) ? i : centerIds.get(i);
        if (counts.count(idx) != centers.get(i).size()) {
            throw new IllegalArgumentException("Covariance/cluster mismatch for cluster ID: " + idx);
        }
    }
}

From source file:com.publictransitanalytics.scoregenerator.output.SectorReachInformation.java

public SectorReachInformation(final Set<MovementPath> bestPaths, final int count,
        final Set<LocalDateTime> reachTimes) throws InterruptedException {

    reachCount = count;/*w  w w .j av  a  2s  .  com*/
    this.reachTimes = reachTimes.stream().map(time -> time.toLocalTime().toString())
            .collect(Collectors.toSet());

    final TreeMultimap<Integer, SimplePath> frequencyMap = TreeMultimap.create(Integer::compareTo,
            (p1, p2) -> p1.toString().compareTo(p2.toString()));

    if (bestPaths != null) {
        final ImmutableMultiset.Builder<SimplePath> bestSimplePathsBuilder = ImmutableMultiset.builder();
        for (final MovementPath bestPath : bestPaths) {
            bestSimplePathsBuilder.add(new SimplePath(bestPath));
        }
        final ImmutableMultiset<SimplePath> bestSimplePaths = bestSimplePathsBuilder.build();

        for (final SimplePath path : bestSimplePaths.elementSet()) {
            frequencyMap.put(bestSimplePaths.count(path), path);
        }

        pathCounts = new LinkedHashMap<>();
        for (final Integer frequency : frequencyMap.keySet().descendingSet()) {
            final NavigableSet<SimplePath> pathsForFrequency = frequencyMap.get(frequency);
            for (final SimplePath pathForFrequency : pathsForFrequency) {
                pathCounts.put(pathForFrequency, frequency);
            }
        }
    } else {
        pathCounts = null;
    }
}

From source file:edu.mit.streamjit.util.bytecode.Value.java

/**
 * Returns an immutable multiset of the users of this value. Note that a User may
 * use this value more than once, in which case it will appear that many times
 * in the multiset. To iterate over each user only once, call Multiset.elementSet()
 * on the returned multiset.//  w  w  w .j  a v a  2  s . com
 * <p/>
 * The returned set will not change even if uses are added to or removed
 * from this value, so it is safe to iterate over even if the loop body may
 * change this value's use set.
 * @return an immutable multiset of this value's users
 */
public ImmutableMultiset<User> users() {
    //If this method is called often, we can cache it in a field to avoid
    //building many copies.  Calls to add/removeUse() would set the field to
    //null to invalidate it and we'd rebuild in users() when required.
    ImmutableMultiset.Builder<User> users = ImmutableMultiset.builder();
    for (Use u : uses())
        users.add(u.getUser());
    return users.build();
}