Example usage for com.google.common.collect Iterables limit

List of usage examples for com.google.common.collect Iterables limit

Introduction

In this page you can find the example usage for com.google.common.collect Iterables limit.

Prototype

public static <T> Iterable<T> limit(final Iterable<T> iterable, final int limitSize) 

Source Link

Document

Creates an iterable with the first limitSize elements of the given iterable.

Usage

From source file:google.registry.rdap.RdapNameserverSearchAction.java

/** Output JSON for a list of hosts. */
private RdapSearchResults makeSearchResults(List<HostResource> hosts, DateTime now) {
    OutputDataType outputDataType = (hosts.size() > 1) ? OutputDataType.SUMMARY : OutputDataType.FULL;
    ImmutableList.Builder<ImmutableMap<String, Object>> jsonListBuilder = new ImmutableList.Builder<>();
    for (HostResource host : Iterables.limit(hosts, rdapResultSetMaxSize)) {
        jsonListBuilder.add(rdapJsonFormatter.makeRdapJsonForHost(host, false, rdapLinkBase, rdapWhoisServer,
                now, outputDataType));/*from ww  w.  jav  a2 s. c o  m*/
    }
    ImmutableList<ImmutableMap<String, Object>> jsonList = jsonListBuilder.build();
    return RdapSearchResults.create(jsonList, jsonList.size() < hosts.size());
}

From source file:com.google.gerrit.httpd.rpc.SuggestServiceImpl.java

private List<GroupReference> suggestAccountGroup(@Nullable final ProjectControl projectControl,
        final String query, final int limit) {
    return Lists.newArrayList(Iterables.limit(groupBackend.suggest(query, projectControl),
            limit <= 0 ? 10 : Math.min(limit, 10)));
}

From source file:com.twitter.aurora.scheduler.http.SchedulerzJob.java

private static <T> Iterable<T> offsetAndLimit(Iterable<T> iterable, int offset) {
    return ImmutableList.copyOf(Iterables.limit(Iterables.skip(iterable, offset), PAGE_SIZE));
}

From source file:org.openscience.cdk.isomorphism.Mappings.java

/**
 * Limit the number of mappings - only this number of mappings will be
 * generate.//from w  w w . ja v  a 2 s  .  c  o  m
 *
 * @param limit the number of mappings
 * @return fluent-api instance
 */
public Mappings limit(int limit) {
    return new Mappings(query, target, Iterables.limit(iterable, limit));
}

From source file:org.robotninjas.barge.log.DefaultRaftLog.java

@Nonnull
public GetEntriesResult getEntriesFrom(@Nonnegative long beginningIndex, @Nonnegative int max) {
    checkArgument(beginningIndex >= 0);
    Set<Long> indices = entryIndex.tailMap(beginningIndex).keySet();
    Iterable<Entry> values = Iterables.transform(Iterables.limit(indices, max), entryCache);
    Entry previousEntry;/*from w  w  w  .j a  v a2 s.c om*/
    if (beginningIndex - 1 <= 0) {
        previousEntry = SENTINEL_ENTRY;
    } else {
        previousEntry = entryCache.getUnchecked(beginningIndex - 1);
    }
    GetEntriesResult result = new GetEntriesResult(previousEntry.getTerm(), beginningIndex - 1,
            newArrayList(values));
    LOGGER.debug("{}", result);
    return result;
}

From source file:org.apache.jackrabbit.oak.remote.content.ContentRemoteTree.java

private Iterable<Tree> getFilteredChildren() {
    Iterable<Tree> result = tree.getChildren();

    if (filters.getChildrenStart() > 0) {
        result = Iterables.skip(result, filters.getChildrenStart());
    }/*from   ww w.  ja  va  2s .c o m*/

    if (filters.getChildrenCount() >= 0) {
        result = Iterables.limit(result, filters.getChildrenCount());
    }

    return Iterables.filter(result, getNodeFilters());
}

From source file:io.prestosql.tests.QueryAssertions.java

public static void assertContains(MaterializedResult all, MaterializedResult expectedSubset) {
    for (MaterializedRow row : expectedSubset.getMaterializedRows()) {
        if (!all.getMaterializedRows().contains(row)) {
            fail(format("expected row missing: %s\nAll %s rows:\n    %s\nExpected subset %s rows:\n    %s\n",
                    row, all.getMaterializedRows().size(), Joiner.on("\n    ").join(Iterables.limit(all, 100)),
                    expectedSubset.getMaterializedRows().size(),
                    Joiner.on("\n    ").join(Iterables.limit(expectedSubset, 100))));
        }//from ww  w  .j  a  va2 s.  com
    }
}

From source file:com.google.gerrit.server.change.SuggestReviewers.java

private List<GroupReference> suggestAccountGroup(ProjectControl ctl) {
    return Lists.newArrayList(Iterables.limit(groupBackend.suggest(query, ctl), limit));
}

From source file:com.google.devtools.build.lib.analysis.LocationExpander.java

/**
 * Returns all possible target location(s) of the given label
 * @param message Original message, for error reporting purposes only
 * @param hasMultipleTargets Describes whether the label has multiple target locations
 * @return The collection of all path strings
 *//*from  w w w. jav  a2 s .c om*/
private Collection<String> resolveLabel(Label unresolved, String message, boolean hasMultipleTargets)
        throws IllegalStateException {
    // replace with singleton artifact, iff unique.
    Collection<Artifact> artifacts = getLocationMap().get(unresolved);

    if (artifacts == null) {
        throw new IllegalStateException(
                "label '" + unresolved + "'" + message + " is not a declared prerequisite of this rule");
    }

    Set<String> paths = getPaths(artifacts, options.contains(Options.EXEC_PATHS));

    if (paths.isEmpty()) {
        throw new IllegalStateException(
                "label '" + unresolved + "'" + message + " expression expands to no files");
    }

    if (!hasMultipleTargets && paths.size() > 1) {
        throw new IllegalStateException(String.format(
                "label '%s'%s expands to more than one file, "
                        + "please use $(locations %s) instead.  Files (at most %d shown) are: %s",
                unresolved, message, unresolved, MAX_PATHS_SHOWN, Iterables.limit(paths, MAX_PATHS_SHOWN)));
    }

    return paths;
}

From source file:com.opengamma.engine.view.worker.MarketDataManager.java

private Runnable createSubscriptionMonitorLogging() {
    return new Runnable() {

        @Override//ww  w  . j  a v  a2s . c o  m
        public void run() {
            _subscriptionsLock.lock();
            try {
                s_logger.info("Pending subscriptions for {}: {}", MarketDataManager.this,
                        _pendingSubscriptions.size());
                if (!_pendingSubscriptions.isEmpty()) {
                    s_logger.info(_pendingSubscriptions.size() > 20 ? "First 20 pending: " : "All {} pending: ",
                            _pendingSubscriptions.size());
                    for (Map.Entry<ValueSpecification, ZonedDateTime> entry : Iterables
                            .limit(_pendingSubscriptions.entrySet(), 20)) {
                        s_logger.info(" - {} in cache since: {}", entry.getKey(), entry.getValue());
                    }
                }
            } finally {
                _subscriptionsLock.unlock();
            }
        }
    };
}