Example usage for com.google.common.collect ImmutableMultiset builder

List of usage examples for com.google.common.collect ImmutableMultiset builder

Introduction

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

Prototype

public static <E> Builder<E> builder() 

Source Link

Document

Returns a new builder.

Usage

From source file:org.sonar.db.organization.OrganizationMemberDao.java

public Multiset<String> countByOrganizationUuids(DbSession dbSession, List<String> organizationUuids) {
    ImmutableMultiset.Builder<String> counts = ImmutableMultiset.builder();
    executeLargeInputsWithoutOutput(organizationUuids,
            list -> mapper(dbSession).countByOrganizationUuids(list, result -> {
                OrganizationCount organizationUuidCount = (OrganizationCount) result.getResultObject();
                counts.setCount(organizationUuidCount.getOrganizationUuid(),
                        organizationUuidCount.getMemberCount());
            }));/*from w  w w.  j a v  a  2s  . c o  m*/

    return counts.build();
}

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

@VisibleForTesting
static AttributeAggregate create(Supplier<Iterable<IAttribute>> attributes) {
    Supplier<Multiset<Pair<String, String>>> aggregator = Suppliers.compose(
            attributes1 -> addAttributes(ImmutableMultiset.builder(), attributes1).build(), attributes);

    return new AttributeAggregate(aggregator);
}

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));
        }//w  w w. j ava2 s  . c  om
    }
    return builder;
}

From source file:ai.grakn.util.CommonUtil.java

public static <T> Collector<T, ?, ImmutableMultiset<T>> toImmutableMultiset() {
    return new Collector<T, ImmutableMultiset.Builder<T>, ImmutableMultiset<T>>() {
        @Override/*from  www. j  a  va2s  . com*/
        public Supplier<ImmutableMultiset.Builder<T>> supplier() {
            return ImmutableMultiset::builder;
        }

        @Override
        public BiConsumer<ImmutableMultiset.Builder<T>, T> accumulator() {
            return ImmutableMultiset.Builder::add;
        }

        @Override
        public BinaryOperator<ImmutableMultiset.Builder<T>> combiner() {
            return (b1, b2) -> b1.addAll(b2.build());
        }

        @Override
        public Function<ImmutableMultiset.Builder<T>, ImmutableMultiset<T>> finisher() {
            return ImmutableMultiset.Builder::build;
        }

        @Override
        public Set<Characteristics> characteristics() {
            return ImmutableSet.of();
        }
    };
}

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  w w w.ja v a2 s  .com
    }
    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.codingopus.collectors.CustomCollectors.java

/**
 * @return {@link ImmutableMultisetCollector}
 *//*from  w w  w  . j  a v  a2 s.c om*/
public static <T> Collector<T, ImmutableMultiset.Builder<T>, ImmutableMultiset<T>> toImmutableMultisetCollector() {
    return ImmutableMultisetCollector.toImmutableMultisetCollector();
}

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

public void updateAttributeAggregate(IHostAttributes attributes) {
    // If the aggregate supplier has not been populated there is no need to update it here.
    // All tasks attributes will be picked up by the wrapped task query if executed at a
    // later point in time.
    if (isInitialized) {
        final Supplier<Multiset<Pair<String, String>>> previous = aggregate;
        aggregate = Suppliers.memoize(() -> {
            ImmutableMultiset.Builder<Pair<String, String>> builder = new ImmutableMultiset.Builder<>();
            builder.addAll(previous.get());
            addAttributes(builder, attributes.getAttributes());
            return builder.build();
        });//w  w  w . ja  v  a  2 s  . co m
    }
}

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 ww  . j  ava 2s  . c  om*/
 * <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();
}

From source file:me.yanaga.guava.stream.MoreCollectors.java

private static <T, B extends ImmutableMultiset.Builder<T>, M extends ImmutableMultiset<T>> Collector<T, ?, M> toImmutableMultiset(
        Supplier<B> supplier, Collector.Characteristics... characteristics) {
    return Collector.of(supplier, new BiConsumer<B, T>() {
        @Override/*  w w w. ja va2s.c  o  m*/
        public void accept(B objectBuilder, T t) {
            objectBuilder.add(t);
        }
    }, new BinaryOperator<B>() {
        @SuppressWarnings("unchecked")
        @Override
        public B apply(B objectBuilder, B objectBuilder2) {
            return (B) objectBuilder.addAll(objectBuilder2.build());
        }
    }, new Function<B, M>() {
        @SuppressWarnings("unchecked")
        @Override
        public M apply(B tBuilder) {
            return (M) tBuilder.build();
        }
    }, characteristics);
}

From source file:com.opengamma.strata.collect.Guavate.java

/**
 * Collector used at the end of a stream to build an immutable multiset.
 * <p>/*  ww  w .  j  a va 2  s . com*/
 * A collector is used to gather data at the end of a stream operation.
 * This method returns a collector allowing streams to be gathered into
 * an {@link ImmutableMultiset}.
 * 
 * @param <T>  the type of element in the multiset
 * @return the immutable multiset collector
 */
public static <T> Collector<T, ImmutableMultiset.Builder<T>, ImmutableMultiset<T>> toImmutableMultiset() {
    return Collector.of(ImmutableMultiset.Builder<T>::new, ImmutableMultiset.Builder<T>::add,
            (l, r) -> l.addAll(r.build()), ImmutableMultiset.Builder<T>::build,
            Collector.Characteristics.UNORDERED);
}