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:org.diqube.execution.steps.OrderStep.java

/**
 * Find those rowIDs that were in activeRowIdsSet, but which will be cut-off when using the given cut-off-point.
 * //from ww  w. j  a v a 2  s . c  om
 * <p>
 * This needs to be executed before executing a cutOff (=adjusting {@link #sortedRowIdsLength}), as this method needs
 * that value.
 * 
 * @param activeRowIdsSet
 *          The source set of row IDs.
 * @param cutOffPoint
 *          The first index in {@link #sortedRowIds} that will be cut-off.
 * @return the row IDs to be reported as newly added. These will be all the longs in activeRowIdsSet which are NOT cut
 *         off.
 */
private Long[] findRowIdsToBeOutputOnCutOff(Set<Long> activeRowIdsSet, int cutOffPoint) {
    Set<Long> rowIdsBeingCutOff = new HashSet<>();
    for (int i = cutOffPoint; i < sortedRowIdsLength; i++)
        rowIdsBeingCutOff.add(sortedRowIds[i]);
    logger.trace("Cutting off {} results because of (soft) limit clause: (limt) {}",
            sortedRowIdsLength - cutOffPoint, Iterables.limit(rowIdsBeingCutOff, 100));

    return Sets.difference(activeRowIdsSet, rowIdsBeingCutOff).stream().toArray(l -> new Long[l]);
}

From source file:org.onos.yangtools.yang.model.util.SchemaContextUtil.java

/**
 * @throws IllegalArgumentException//from ww  w.  j  ava  2s. co  m
 *
 * @param context
 *            Schema Context
 * @param module
 *            Yang Module
 * @param relativeXPath
 *            Non conditional Revision Aware Relative XPath
 * @param actualSchemaNode
 *            actual schema node
 * @return list of QName
 */
private static Iterable<QName> resolveRelativeXPath(final SchemaContext context, final Module module,
        final RevisionAwareXPath relativeXPath, final SchemaNode actualSchemaNode) {
    Preconditions.checkArgument(context != null, "Schema Context reference cannot be NULL");
    Preconditions.checkArgument(module != null, "Module reference cannot be NULL");
    Preconditions.checkArgument(relativeXPath != null, "Non Conditional Revision Aware XPath cannot be NULL");
    Preconditions.checkState(!relativeXPath.isAbsolute(),
            "Revision Aware XPath MUST be relative i.e. MUST contains ../, "
                    + "for non relative Revision Aware XPath use findDataSchemaNode method");
    Preconditions.checkState(actualSchemaNode.getPath() != null,
            "Schema Path reference for Leafref cannot be NULL");

    final Iterable<String> xpaths = SLASH_SPLITTER.split(relativeXPath.toString());

    // Find out how many "parent" components there are
    // FIXME: is .contains() the right check here?
    // FIXME: case ../../node1/node2/../node3/../node4
    int colCount = 0;
    for (final Iterator<String> it = xpaths.iterator(); it.hasNext() && it.next().contains("..");) {
        ++colCount;
    }

    final Iterable<QName> schemaNodePath = actualSchemaNode.getPath().getPathFromRoot();

    if (Iterables.size(schemaNodePath) - colCount >= 0) {
        return Iterables.concat(Iterables.limit(schemaNodePath, Iterables.size(schemaNodePath) - colCount),
                Iterables.transform(Iterables.skip(xpaths, colCount), new Function<String, QName>() {
                    @Override
                    public QName apply(final String input) {
                        return stringPathPartToQName(context, module, input);
                    }
                }));
    }
    return Iterables.concat(schemaNodePath,
            Iterables.transform(Iterables.skip(xpaths, colCount), new Function<String, QName>() {
                @Override
                public QName apply(final String input) {
                    return stringPathPartToQName(context, module, input);
                }
            }));
}

From source file:org.opendaylight.yangtools.yang.model.util.SchemaContextUtil.java

/**
 * @param context/*  ww w .j  ava2  s.c  o  m*/
 *            Schema Context
 * @param module
 *            Yang Module
 * @param relativeXPath
 *            Non conditional Revision Aware Relative XPath
 * @param actualSchemaNode
 *            actual schema node
 * @return list of QName
 *
 * @throws IllegalArgumentException
 */
private static Iterable<QName> resolveRelativeXPath(final SchemaContext context, final Module module,
        final RevisionAwareXPath relativeXPath, final SchemaNode actualSchemaNode) {
    Preconditions.checkArgument(context != null, "Schema Context reference cannot be NULL");
    Preconditions.checkArgument(module != null, "Module reference cannot be NULL");
    Preconditions.checkArgument(relativeXPath != null, "Non Conditional Revision Aware XPath cannot be NULL");
    Preconditions.checkState(!relativeXPath.isAbsolute(),
            "Revision Aware XPath MUST be relative i.e. MUST contains ../, "
                    + "for non relative Revision Aware XPath use findDataSchemaNode method");
    Preconditions.checkState(actualSchemaNode.getPath() != null,
            "Schema Path reference for Leafref cannot be NULL");

    final Iterable<String> xpaths = SLASH_SPLITTER.split(relativeXPath.toString());

    // Find out how many "parent" components there are
    // FIXME: is .contains() the right check here?
    // FIXME: case ../../node1/node2/../node3/../node4
    int colCount = 0;
    for (final Iterator<String> it = xpaths.iterator(); it.hasNext() && it.next().contains("..");) {
        ++colCount;
    }

    final Iterable<QName> schemaNodePath = actualSchemaNode.getPath().getPathFromRoot();

    if (Iterables.size(schemaNodePath) - colCount >= 0) {
        return Iterables.concat(Iterables.limit(schemaNodePath, Iterables.size(schemaNodePath) - colCount),
                Iterables.transform(Iterables.skip(xpaths, colCount),
                        input -> stringPathPartToQName(context, module, input)));
    }
    return Iterables.concat(schemaNodePath, Iterables.transform(Iterables.skip(xpaths, colCount),
            input -> stringPathPartToQName(context, module, input)));
}

From source file:org.geoserver.catalog.impl.DefaultCatalogFacade.java

@Override
public <T extends CatalogInfo> CloseableIterator<T> list(final Class<T> of, final Filter filter,
        @Nullable Integer offset, @Nullable Integer count, @Nullable SortBy... sortOrder) {

    if (sortOrder != null) {
        for (SortBy so : sortOrder) {
            if (sortOrder != null && !canSort(of, so.getPropertyName().getPropertyName())) {
                throw new IllegalArgumentException(
                        "Can't sort objects of type " + of.getName() + " by " + so.getPropertyName());
            }/*from  w  w  w. ja va 2 s .  co  m*/
        }
    }

    Iterable<T> iterable = iterable(of, filter, sortOrder);

    if (offset != null && offset.intValue() > 0) {
        iterable = Iterables.skip(iterable, offset.intValue());
    }

    if (count != null && count.intValue() >= 0) {
        iterable = Iterables.limit(iterable, count.intValue());
    }

    Iterator<T> iterator = iterable.iterator();

    return new CloseableIteratorAdapter<T>(iterator);
}

From source file:dagger2.internal.codegen.BindingGraphValidator.java

@SuppressWarnings("resource") // Appendable is a StringBuilder.
private void reportDuplicateBindings(Deque<ResolvedRequest> path,
        ValidationReport.Builder<BindingGraph> reportBuilder) {
    ResolvedBindings resolvedBinding = path.peek().binding();
    StringBuilder builder = new StringBuilder();
    new Formatter(builder).format(ErrorMessages.DUPLICATE_BINDINGS_FOR_KEY_FORMAT,
            keyFormatter.format(path.peek().request().key()));
    for (Binding binding : Iterables.limit(resolvedBinding.bindings(), DUPLICATE_SIZE_LIMIT)) {
        builder.append('\n').append(INDENT);
        // TODO(user): Refactor the formatters so we don't need these instanceof checks.
        if (binding instanceof ProvisionBinding) {
            builder.append(provisionBindingFormatter.format((ProvisionBinding) binding));
        } else if (binding instanceof ProductionBinding) {
            builder.append(productionBindingFormatter.format((ProductionBinding) binding));
        }//www.ja  v a  2s.  c om
    }
    int numberOfOtherBindings = resolvedBinding.bindings().size() - DUPLICATE_SIZE_LIMIT;
    if (numberOfOtherBindings > 0) {
        builder.append('\n').append(INDENT).append("and ").append(numberOfOtherBindings).append(" other");
    }
    if (numberOfOtherBindings > 1) {
        builder.append('s');
    }
    reportBuilder.addItem(builder.toString(), path.getLast().request().requestElement());
}

From source file:com.eucalyptus.autoscaling.activities.ActivityManager.java

private ScalingProcessTask<?, ?> perhapsReplaceInstances(final AutoScalingGroupScalingView group) {
    final List<String> instancesToTerminate = Lists.newArrayList();
    boolean anyRegisteredInstances = false;
    if (scalingProcessEnabled(ScalingProcessType.ReplaceUnhealthy, group))
        try {/*from   w  w  w . ja  v a  2  s.  co  m*/
            final List<AutoScalingInstanceCoreView> currentInstances = autoScalingInstances
                    .listUnhealthyByGroup(group,
                            TypeMappers.lookup(AutoScalingInstance.class, AutoScalingInstanceCoreView.class));
            Iterables.addAll(instancesToTerminate, Iterables.limit(
                    Iterables.transform(currentInstances, RestrictedTypes.toDisplayName()),
                    Math.min(AutoScalingConfiguration.getMaxLaunchIncrement(), currentInstances.size())));
            anyRegisteredInstances = Iterables.any(currentInstances, ConfigurationState.Registered.forView());
            if (!instancesToTerminate.isEmpty()) {
                logger.info("Terminating unhealthy instances: " + instancesToTerminate);
            }
        } catch (final Exception e) {
            logger.error(e, e);
        }
    return removeFromLoadBalancerOrTerminate(group, group.getCapacity(), anyRegisteredInstances,
            instancesToTerminate,
            Collections.singletonList(
                    new ActivityCause("an instance was taken out of service in response to a health-check")),
            true);
}

From source file:com.palantir.atlasdb.transaction.impl.SnapshotTransaction.java

private <T> void getWithPostfiltering(String tableName, Map<Cell, Value> rawResults,
        @Output Map<Cell, T> results, Function<Value, T> transformer) {
    long bytes = 0;
    for (Map.Entry<Cell, Value> e : rawResults.entrySet()) {
        bytes += e.getValue().getContents().length + Cells.getApproxSizeOfCell(e.getKey());
    }//  w  w w.ja  va  2 s .  co m
    if (bytes > TransactionConstants.ERROR_LEVEL_FOR_QUEUED_BYTES
            && !AtlasDbConstants.TABLES_KNOWN_TO_BE_POORLY_DESIGNED.contains(tableName)) {
        log.error(
                "A single get had a lot of bytes: " + bytes + " for table " + tableName + ". "
                        + "The number of results was " + rawResults.size() + ". " + "The first 10 results were "
                        + Iterables.limit(rawResults.entrySet(), 10) + ". "
                        + "This can potentially cause out-of-memory errors.",
                new RuntimeException("This exception and stack trace are provided for debugging purposes."));
    } else if (bytes > TransactionConstants.WARN_LEVEL_FOR_QUEUED_BYTES && log.isWarnEnabled()) {
        log.warn(
                "A single get had quite a few bytes: " + bytes + " for table " + tableName + ". "
                        + "The number of results was " + rawResults.size() + ". " + "The first 10 results were "
                        + Iterables.limit(rawResults.entrySet(), 10) + ". ",
                new RuntimeException("This exception and stack trace are provided for debugging purposes."));
    }

    if (isTempTable(tableName)
            || (AtlasDbConstants.SKIP_POSTFILTER_TABLES.contains(tableName) && allowHiddenTableAccess)) {
        // If we are reading from a temp table, we can just bypass postfiltering
        // or skip postfiltering if reading the transaction or namespace table from atlasdb shell
        for (Map.Entry<Cell, Value> e : rawResults.entrySet()) {
            results.put(e.getKey(), transformer.apply(e.getValue()));
        }
        return;
    }
    while (!rawResults.isEmpty()) {
        rawResults = getWithPostfilteringInternal(tableName, rawResults, results, transformer);
    }
}

From source file:org.pantsbuild.tools.jar.JarBuilder.java

private Optional<ReadableEntry> processEntries(Predicate<CharSequence> skipPath,
        DuplicateHandler duplicateHandler, String jarPath, Collection<ReadableEntry> itemEntries) {

    if (skipPath.apply(jarPath)) {
        listener.onSkip(Optional.<Entry>absent(), itemEntries);
        return Optional.absent();
    }/*from   w ww  . j  a  v a  2 s .  c  o  m*/

    if (itemEntries.size() < 2) {
        ReadableEntry entry = Iterables.getOnlyElement(itemEntries);
        listener.onWrite(entry);
        return Optional.of(entry);
    }

    DuplicateAction action = duplicateHandler.actionFor(jarPath);
    switch (action) {
    case SKIP: {
        ReadableEntry original = Iterables.get(itemEntries, 0);
        listener.onSkip(Optional.of(original), Iterables.skip(itemEntries, 1));
        return Optional.of(original);
    }

    case REPLACE: {
        ReadableEntry replacement = Iterables.getLast(itemEntries);
        listener.onReplace(Iterables.limit(itemEntries, itemEntries.size() - 1), replacement);
        return Optional.of(replacement);
    }
    case CONCAT: {
        ByteSource concat = ByteSource.concat(Iterables.transform(itemEntries, ReadableEntry.GET_CONTENTS));

        ReadableEntry concatenatedEntry = new ReadableEntry(
                NamedByteSource.create(memorySource(), jarPath, concat), jarPath);

        listener.onConcat(jarPath, itemEntries);
        return Optional.of(concatenatedEntry);
    }

    case CONCAT_TEXT: {
        ByteSource concat_text = ByteSource
                .concat(Iterables.transform(itemEntries, ReadableTextEntry.GET_CONTENTS));

        ReadableEntry concatenatedTextEntry = new ReadableEntry(
                NamedByteSource.create(memorySource(), jarPath, concat_text), jarPath);

        listener.onConcat(jarPath, itemEntries);
        return Optional.of(concatenatedTextEntry);
    }

    case THROW:
        throw new DuplicateEntryException(Iterables.get(itemEntries, 1));

    default:
        throw new IllegalArgumentException("Unrecognized DuplicateAction " + action);
    }
}

From source file:org.apache.jackrabbit.oak.plugins.document.DocumentNodeStore.java

/**
 * Returns the child documents at the given {@code path} and returns up to
 * {@code limit} documents. The returned child documents are sorted in
 * ascending child node name order. If a {@code name} is passed, the first
 * child document returned is after the given name. That is, the name is the
 * lower exclusive bound.//from   w  w w.j  ava  2 s  .  c o m
 *
 * @param path the path of the parent document.
 * @param name the lower exclusive bound or {@code null}.
 * @param limit the maximum number of child documents to return.
 * @return the child documents.
 */
@Nonnull
private Iterable<NodeDocument> readChildDocs(@Nonnull final String path, @Nullable String name,
        final int limit) {
    final String to = Utils.getKeyUpperLimit(checkNotNull(path));
    final String from;
    if (name != null) {
        from = Utils.getIdFromPath(concat(path, name));
    } else {
        from = Utils.getKeyLowerLimit(path);
    }
    if (name != null || limit > NUM_CHILDREN_CACHE_LIMIT) {
        // do not use cache when there is a lower bound name
        // or more than 16k child docs are requested
        return store.query(Collection.NODES, from, to, limit);
    }
    final StringValue key = new StringValue(path);
    // check cache
    NodeDocument.Children c = docChildrenCache.getIfPresent(key);
    if (c == null) {
        c = new NodeDocument.Children();
        List<NodeDocument> docs = store.query(Collection.NODES, from, to, limit);
        for (NodeDocument doc : docs) {
            String p = doc.getPath();
            c.childNames.add(PathUtils.getName(p));
        }
        c.isComplete = docs.size() < limit;
        docChildrenCache.put(key, c);
        return docs;
    } else if (c.childNames.size() < limit && !c.isComplete) {
        // fetch more and update cache
        String lastName = c.childNames.get(c.childNames.size() - 1);
        String lastPath = concat(path, lastName);
        String low = Utils.getIdFromPath(lastPath);
        int remainingLimit = limit - c.childNames.size();
        List<NodeDocument> docs = store.query(Collection.NODES, low, to, remainingLimit);
        NodeDocument.Children clone = c.clone();
        for (NodeDocument doc : docs) {
            String p = doc.getPath();
            clone.childNames.add(PathUtils.getName(p));
        }
        clone.isComplete = docs.size() < remainingLimit;
        docChildrenCache.put(key, clone);
        c = clone;
    }
    Iterable<NodeDocument> head = filter(transform(c.childNames, new Function<String, NodeDocument>() {
        @Override
        public NodeDocument apply(String name) {
            String p = concat(path, name);
            NodeDocument doc = store.find(Collection.NODES, Utils.getIdFromPath(p));
            if (doc == null) {
                docChildrenCache.invalidate(key);
            }
            return doc;
        }
    }), Predicates.notNull());
    Iterable<NodeDocument> it;
    if (c.isComplete) {
        it = head;
    } else {
        // OAK-2420: 'head' may have null documents when documents are
        // concurrently removed from the store. concat 'tail' to fetch
        // more documents if necessary
        final String last = getIdFromPath(concat(path, c.childNames.get(c.childNames.size() - 1)));
        Iterable<NodeDocument> tail = new Iterable<NodeDocument>() {
            @Override
            public Iterator<NodeDocument> iterator() {
                return store.query(NODES, last, to, limit).iterator();
            }
        };
        it = Iterables.concat(head, tail);
    }
    return Iterables.limit(it, limit);
}

From source file:org.apache.aurora.scheduler.thrift.SchedulerThriftInterface.java

@Override
public Response pruneTasks(TaskQuery query) throws TException {
    if (query.isSetStatuses() && query.getStatuses().stream().anyMatch(ACTIVE_STATES::contains)) {
        return error("Tasks in non-terminal state cannot be pruned.");
    } else if (!query.isSetStatuses()) {
        query.setStatuses(TERMINAL_STATES);
    }/*www .  j  av  a 2  s .co m*/

    Iterable<IScheduledTask> tasks = storage
            .read(storeProvider -> storeProvider.getTaskStore().fetchTasks(Query.arbitrary(query)));
    // For some reason fetchTasks ignores the offset/limit options of a TaskQuery. So we have to
    // manually apply the limit here. To be fixed in AURORA-1892.
    if (query.isSetLimit()) {
        tasks = Iterables.limit(tasks, query.getLimit());
    }

    Iterable<String> taskIds = Iterables.transform(tasks, task -> task.getAssignedTask().getTaskId());

    return storage.write(storeProvider -> {
        stateManager.deleteTasks(storeProvider, Sets.newHashSet(taskIds));
        return ok();
    });
}