Java Code Examples for com.google.common.collect.Iterables#limit()

The following examples show how to use com.google.common.collect.Iterables#limit() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SoyDirectives.java    From deploymentmanager-autogen with Apache License 2.0 6 votes vote down vote up
@Override
public SoyValue applyForJava(SoyValue value, List<SoyValue> args) {
  final String spacer = Strings.repeat(" ", args.get(0).integerValue());
  Iterable<String> pre;
  Iterable<String> toBeIndented;
  if (getIndentFirstArg(args)) {
    pre = ImmutableList.of();
    toBeIndented = SPLITTER.split(value.coerceToString());
  } else {
    List<String> lines = SPLITTER.splitToList(value.coerceToString());
    pre = Iterables.limit(lines, 1);
    toBeIndented = Iterables.skip(lines, 1);
  }
  Iterable<String> indented = Iterables.transform(toBeIndented, new Function<String, String>() {
    @Override
    public String apply(String s) {
      if (s.trim().isEmpty()) {
        // Don't indent lines with only spaces.
        return s;
      }
      return spacer + s;
    }
  });
  return StringData.forValue(JOINER.join(Iterables.concat(pre, indented)));
}
 
Example 2
Source File: SortableFacet.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/**
 * Apply the sorting options to the given facet results.
 *
 * @param facetResults to apply sorting options to
 * @return the sorted results
 */
@SuppressWarnings({"unchecked", "rawtypes"})
protected Iterable<FacetBucket> applyOptions(List<FacetBucket> facetResults) {
  // Sorting the buckets if a sort specification is provided
  if (sort == null || facetResults.isEmpty()) {
    return facetResults;
  }
  Comparator comp = sort.getComparator();
  Collections.sort(facetResults, comp);

  Iterable<FacetBucket> facetResultsIter = facetResults;
  // apply the limit
  if (sort.getLimit() > 0) {
    if (sort.getOffset() > 0) {
      facetResultsIter = Iterables.skip(facetResultsIter, sort.getOffset());
    }
    facetResultsIter = Iterables.limit(facetResultsIter, sort.getLimit());
  } else if (sort.getLimit() == 0) {
    return new LinkedList<FacetBucket>();
  }
  return facetResultsIter;
}
 
Example 3
Source File: RdapNameserverSearchAction.java    From nomulus with Apache License 2.0 6 votes vote down vote up
/** Output JSON for a list of hosts. */
private NameserverSearchResponse makeSearchResults(
    List<HostResource> hosts,
    IncompletenessWarningType incompletenessWarningType,
    int numHostsRetrieved,
    CursorType cursorType) {
  metricInformationBuilder.setNumHostsRetrieved(numHostsRetrieved);
  OutputDataType outputDataType =
      (hosts.size() > 1) ? OutputDataType.SUMMARY : OutputDataType.FULL;
  NameserverSearchResponse.Builder builder =
      NameserverSearchResponse.builder().setIncompletenessWarningType(incompletenessWarningType);
  Optional<String> newCursor = Optional.empty();
  for (HostResource host : Iterables.limit(hosts, rdapResultSetMaxSize)) {
    newCursor =
        Optional.of((cursorType == CursorType.NAME) ? host.getHostName() : host.getRepoId());
    builder
        .nameserverSearchResultsBuilder()
        .add(rdapJsonFormatter.createRdapNameserver(host, outputDataType));
  }
  if (rdapResultSetMaxSize < hosts.size()) {
    builder.setNextPageUri(createNavigationUri(newCursor.get()));
    builder.setIncompletenessWarningType(IncompletenessWarningType.TRUNCATED);
  }
  return builder.build();
}
 
Example 4
Source File: RdapDomainSearchAction.java    From nomulus with Apache License 2.0 6 votes vote down vote up
/**
 * Output JSON for a list of domains.
 *
 * <p>The incompletenessWarningType should be set to TRUNCATED if the search found more results
 * than are in the list, or MIGHT_BE_INCOMPLETE if a search for domains by nameserver returned the
 * maximum number of nameservers in the first stage query.
 */
private DomainSearchResponse makeSearchResults(
    List<DomainBase> domains,
    IncompletenessWarningType incompletenessWarningType,
    Optional<Long> numDomainsRetrieved) {
  numDomainsRetrieved.ifPresent(metricInformationBuilder::setNumDomainsRetrieved);
  OutputDataType outputDataType =
      (domains.size() > 1) ? OutputDataType.SUMMARY : OutputDataType.FULL;
  DomainSearchResponse.Builder builder =
      DomainSearchResponse.builder()
          .setIncompletenessWarningType(incompletenessWarningType);
  Optional<String> newCursor = Optional.empty();
  for (DomainBase domain : Iterables.limit(domains, rdapResultSetMaxSize)) {
    newCursor = Optional.of(domain.getDomainName());
    builder
        .domainSearchResultsBuilder()
        .add(rdapJsonFormatter.createRdapDomain(domain, outputDataType));
  }
  if (rdapResultSetMaxSize < domains.size()) {
    builder.setNextPageUri(createNavigationUri(newCursor.get()));
    builder.setIncompletenessWarningType(IncompletenessWarningType.TRUNCATED);
  }
  return builder.build();
}
 
Example 5
Source File: Dumpers.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
private static void deepDumpInternal(Object o, Predicate<Field> fieldPredicate, PrintStream out, int indentSize, String prefix, List<Object> visited) throws IllegalArgumentException, IllegalAccessException {
    String indent = com.google.common.base.Strings.repeat(" ", indentSize*2);
    Class<?> clazz = (o != null) ? o.getClass() : null;
    
    if (o == null) {
        out.println(indent+prefix+"null");
    } else if (isClassUntraversable(clazz)) {
        out.println(indent+prefix+"(untraversable) type="+clazz+"; val="+o.toString());
    } else if (containsSame(visited, o)) {
        out.println(indent+prefix+"duplicate (type="+clazz+"; val="+o.toString()+")");
    } else {
        visited.add(o);
        out.println(indent+prefix+"type="+clazz+"; val="+o.toString());
        Map<String, Object> members = findMembers(o, fieldPredicate);
        for (Map.Entry<String, Object> entry : Iterables.limit(members.entrySet(), MAX_MEMBERS)) {
            deepDumpInternal(entry.getValue(), fieldPredicate, out, indentSize+1, ""+entry.getKey()+": ", visited);
        }
        if (members.size() > MAX_MEMBERS) {
            out.println(indent+prefix+"TRUNCATED ("+members.size()+" members in total)");
        }
    }
}
 
Example 6
Source File: ProjectStateSyncTask.java    From intellij with Apache License 2.0 6 votes vote down vote up
private static void printWorkingSet(BlazeContext context, WorkingSet workingSet) {
  List<String> messages = Lists.newArrayList();
  messages.addAll(
      workingSet.addedFiles.stream()
          .map(file -> file.relativePath() + " (added)")
          .collect(Collectors.toList()));
  messages.addAll(
      workingSet.modifiedFiles.stream()
          .map(file -> file.relativePath() + " (modified)")
          .collect(Collectors.toList()));
  Collections.sort(messages);

  if (messages.isEmpty()) {
    context.output(PrintOutput.log("Your working set is empty"));
    return;
  }
  int maxFiles = 20;
  for (String message : Iterables.limit(messages, maxFiles)) {
    context.output(PrintOutput.log("  " + message));
  }
  if (messages.size() > maxFiles) {
    context.output(PrintOutput.log(String.format("  (and %d more)", messages.size() - maxFiles)));
  }
  context.output(PrintOutput.output(""));
}
 
Example 7
Source File: NotificationStore.java    From notification with Apache License 2.0 6 votes vote down vote up
/**
 * Returns an iterable that skips forward to a given notification ID then only returns count more
 * notifications. If the given notification ID is not found
 *
 * @param notifications Iterable of notifications
 * @param startId notification ID to start at
 * @param inclusive Whether to include the startId notification or not
 * @param limitSize Number of notifications to return
 * @return Iterable containing the subset of the original notifications
 */
public Iterable<Notification> skip(
    final Iterable<Notification> notifications,
    final String startId,
    final boolean inclusive,
    final int limitSize) {

  Objects.requireNonNull(notifications, "notifications == null");

  final int position = indexOf(notifications, startId);
  if (position == -1) {
    return Iterables.limit(notifications, limitSize);
  }
  if (inclusive) {
    return Iterables.limit(Iterables.skip(notifications, position), limitSize);
  }
  return Iterables.limit(Iterables.skip(notifications, position + 1), limitSize);
}
 
Example 8
Source File: RunCommand.java    From bazel with Apache License 2.0 5 votes vote down vote up
private String makeErrorMessageForNotHavingASingleTarget(
    String targetPatternString, Iterable<String> expandedTargetNames) {
  final int maxNumExpandedTargetsToIncludeInErrorMessage = 5;
  boolean truncateTargetNameList = Iterables.size(expandedTargetNames) > 5;
  Iterable<String> targetNamesToIncludeInErrorMessage =
      truncateTargetNameList
          ? Iterables.limit(expandedTargetNames, maxNumExpandedTargetsToIncludeInErrorMessage)
          : expandedTargetNames;
  return String.format(
      "Only a single target can be run. Your target pattern %s expanded to the targets %s%s",
      targetPatternString,
      Joiner.on(", ").join(ImmutableSortedSet.copyOf(targetNamesToIncludeInErrorMessage)),
      truncateTargetNameList ? "[TRUNCATED]" : "");
}
 
Example 9
Source File: ReadOnlySchedulerImpl.java    From attic-aurora with Apache License 2.0 5 votes vote down vote up
private List<ScheduledTask> getTasks(TaskQuery query) {
  requireNonNull(query);

  Iterable<IScheduledTask> tasks = Storage.Util.fetchTasks(storage, Query.arbitrary(query));
  if (query.getOffset() > 0) {
    tasks = Iterables.skip(tasks, query.getOffset());
  }
  if (query.getLimit() > 0) {
    tasks = Iterables.limit(tasks, query.getLimit());
  }

  return IScheduledTask.toBuildersList(tasks);
}
 
Example 10
Source File: SequencedSkyframeExecutor.java    From bazel with Apache License 2.0 5 votes vote down vote up
private static void logDiffInfo(
    Iterable<Root> pathEntries,
    Iterable<SkyKey> changedWithoutNewValue,
    int numWithoutNewValues,
    Map<SkyKey, ? extends SkyValue> changedWithNewValue) {
  int numModified = changedWithNewValue.size() + numWithoutNewValues;
  StringBuilder result = new StringBuilder("DiffAwareness found ")
      .append(numModified)
      .append(" modified source files and directory listings");
  if (!Iterables.isEmpty(pathEntries)) {
    result.append(" for ");
    result.append(Joiner.on(", ").join(pathEntries));
  }

  if (numModified > 0) {
    Iterable<SkyKey> allModifiedKeys = Iterables.concat(changedWithoutNewValue,
        changedWithNewValue.keySet());
    Iterable<SkyKey> trimmed =
        Iterables.limit(allModifiedKeys, MAX_NUMBER_OF_CHANGED_KEYS_TO_LOG);

    result.append(": ")
        .append(Joiner.on(", ").join(trimmed));

    if (numModified > MAX_NUMBER_OF_CHANGED_KEYS_TO_LOG) {
      result.append(", ...");
    }
  }

  logger.atInfo().log(result.toString());
}
 
Example 11
Source File: MemoryExperimentsStore.java    From alchemy with MIT License 5 votes vote down vote up
@Override
public Iterable<Experiment> find(Filter filter, Experiment.BuilderFactory factory) {
    final List<Experiment> result = Lists.newArrayList();

    for (final Experiment experiment : db.values()) {
        if (filterMatches(
            filter.getFilter(),
            experiment.getName(),
            experiment.getDescription())) {
            result.add(Experiment.copyOf(experiment));
        }
    }

    if (filter.getOrdering() != null && !filter.getOrdering().isEmpty()) {
        final Comparator<Experiment> comparator = new OrderingComparator(filter.getOrdering());
        Collections.sort(result, comparator);
    }

    Iterable<Experiment> filteredResult = result;

    if (filter.getOffset() != null) {
        filteredResult = Iterables.skip(filteredResult, filter.getOffset());
    }

    if (filter.getLimit() != null) {
        filteredResult = Iterables.limit(filteredResult, filter.getLimit());
    }

    return Lists.newArrayList(filteredResult);
}
 
Example 12
Source File: DynamicRegionsFabricTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
protected void runUsesInitialLocationsForExistingChildren(int numChildren) throws Exception {
    List<TestEntity> existingChildren = Lists.newArrayList();
    for (int i = 0; i < numChildren; i++) {
        existingChildren.add(fabric.addChild(EntitySpec.create(TestEntity.class)));
    }
    app.start(ImmutableList.of(loc1, loc2));

    int expectedNumChildren = Math.max(numChildren, 2);
    Iterable<Location> expectedChildLocations = Iterables.limit(Iterables.cycle(ImmutableList.of(loc1, loc2)), expectedNumChildren);
    
    assertFabricChildren(fabric, expectedNumChildren, ImmutableList.copyOf(expectedChildLocations));
    assertTrue(fabric.getChildren().containsAll(existingChildren));
}
 
Example 13
Source File: StoreMailboxMessageResultIteratorTest.java    From james-project with Apache License 2.0 5 votes vote down vote up
@Override
public Iterator<MailboxMessage> findInMailbox(Mailbox mailbox, MessageRange set,
                                                      org.apache.james.mailbox.store.mail.MessageMapper.FetchType type, int limit)
        throws MailboxException {
    
    List<MailboxMessage> messages = new ArrayList<>();
    for (MessageUid uid: Iterables.limit(set, limit)) {
        if (messageRange.includes(uid)) {
            messages.add(createMessage(uid));
        }    
    }
    return messages.iterator();
}
 
Example 14
Source File: PcapTopologyIntegrationTest.java    From metron with Apache License 2.0 5 votes vote down vote up
private static Iterable<Map.Entry<byte[], byte[]>> readPcaps(Path pcapFile, boolean withHeaders) throws IOException {
  SequenceFile.Reader reader = new SequenceFile.Reader(new Configuration(),
      SequenceFile.Reader.file(pcapFile)
  );
  List<Map.Entry<byte[], byte[]> > ret = new ArrayList<>();
  IntWritable key = new IntWritable();
  BytesWritable value = new BytesWritable();
  while (reader.next(key, value)) {
    byte[] pcapWithHeader = value.copyBytes();
    //if you are debugging and want the hex dump of the packets, uncomment the following:

    //for(byte b : pcapWithHeader) {
    //  System.out.print(String.format("%02x", b));
    //}
    //System.out.println("");

    long calculatedTs = PcapHelper.getTimestamp(pcapWithHeader);
    {
      List<PacketInfo> info = PcapHelper.toPacketInfo(pcapWithHeader);
      for (PacketInfo pi : info) {
        assertEquals(calculatedTs, pi.getPacketTimeInNanos());
        //IF you are debugging and want to see the packets, uncomment the following.
        //System.out.println( Long.toUnsignedString(calculatedTs) + " => " + pi.getJsonDoc());
      }
    }
    if (withHeaders) {
      ret.add(new AbstractMap.SimpleImmutableEntry<>(Bytes.toBytes(calculatedTs), pcapWithHeader));
    } else {
      byte[] pcapRaw = new byte[pcapWithHeader.length - PcapHelper.GLOBAL_HEADER_SIZE - PcapHelper.PACKET_HEADER_SIZE];
      System.arraycopy(pcapWithHeader, PcapHelper.GLOBAL_HEADER_SIZE + PcapHelper.PACKET_HEADER_SIZE, pcapRaw, 0, pcapRaw.length);
      ret.add(new AbstractMap.SimpleImmutableEntry<>(Bytes.toBytes(calculatedTs), pcapRaw));
    }
  }
  return Iterables.limit(ret, 2 * (ret.size() / 2));
}
 
Example 15
Source File: SchedulerThriftInterface.java    From attic-aurora with Apache License 2.0 5 votes vote down vote up
@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);
  }

  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 -> {
    Set<String> taskIdsSet = Sets.newHashSet(taskIds);
    stateManager.deleteTasks(storeProvider, taskIdsSet);
    pruneTasksCounter.addAndGet(taskIdsSet.size());
    return ok();
  });
}
 
Example 16
Source File: AutomaticContingenciesAndActionsDatabaseClient.java    From ipst with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public List<Contingency> getContingencies(Network network) {
    List<Contingency> contingencies = new ArrayList<>(lineContigencyCount);
    for (Line l : Iterables.limit(network.getLines(), lineContigencyCount)) {
        contingencies.add(new Contingency(l.getId(), Arrays.<ContingencyElement>asList(new BranchContingency(l.getId()))));
    }
    return contingencies;
}
 
Example 17
Source File: Validator.java    From presto with Apache License 2.0 5 votes vote down vote up
public String getResultsComparison(int precision)
{
    List<List<Object>> controlResults = controlResult.getResults();
    List<List<Object>> testResults = testResult.getResults();

    if (valid() || (controlResults == null) || (testResults == null)) {
        return "";
    }

    Multiset<List<Object>> control = ImmutableSortedMultiset.copyOf(rowComparator(precision), controlResults);
    Multiset<List<Object>> test = ImmutableSortedMultiset.copyOf(rowComparator(precision), testResults);

    try {
        Iterable<ChangedRow> diff = ImmutableSortedMultiset.<ChangedRow>naturalOrder()
                .addAll(Iterables.transform(Multisets.difference(control, test), row -> new ChangedRow(Changed.REMOVED, row, precision)))
                .addAll(Iterables.transform(Multisets.difference(test, control), row -> new ChangedRow(Changed.ADDED, row, precision)))
                .build();
        diff = Iterables.limit(diff, 100);

        StringBuilder sb = new StringBuilder();

        sb.append(format("Control %s rows, Test %s rows%n", control.size(), test.size()));
        if (verboseResultsComparison) {
            Joiner.on("\n").appendTo(sb, diff);
        }
        else {
            sb.append("RESULTS DO NOT MATCH\n");
        }

        return sb.toString();
    }
    catch (TypesDoNotMatchException e) {
        return e.getMessage();
    }
}
 
Example 18
Source File: QueryWithLimitWorker.java    From dynamodb-janusgraph-storage-backend with Apache License 2.0 4 votes vote down vote up
@Override
protected List<Map<String, AttributeValue>> getFinalItemList() {
    final Iterable<Map<String, AttributeValue>> limitedIter = Iterables.limit(super.getFinalItemList(), limit);
    return Lists.newArrayList(limitedIter);
}
 
Example 19
Source File: MutableActionGraph.java    From bazel with Apache License 2.0 4 votes vote down vote up
/** Pretty print action diffs (at most {@code MAX_DIFF_ARTIFACTS_TO_REPORT} lines). */
private static void prettyPrintArtifactDiffs(StringBuilder sb, Set<Artifact> diff) {
  for (Artifact artifact : Iterables.limit(diff, MAX_DIFF_ARTIFACTS_TO_REPORT)) {
    sb.append("\t" + artifact.prettyPrint() + "\n");
  }
}
 
Example 20
Source File: GraphOutputFormatter.java    From bazel with Apache License 2.0 4 votes vote down vote up
private String getConditionsGraphLabel(
    Iterable<Node<Target>> lhs, Iterable<Node<Target>> rhs, ConditionalEdges conditionalEdges) {
  StringBuilder buf = new StringBuilder();

  if (this.graphConditionalEdgesLimit == 0) {
    return buf.toString();
  }

  Set<Label> annotatedLabels = new HashSet<>();
  for (Node<Target> src : lhs) {
    Label srcLabel = src.getLabel().getLabel();
    for (Node<Target> dest : rhs) {
      Label destLabel = dest.getLabel().getLabel();
      Optional<Set<Label>> conditions = conditionalEdges.get(srcLabel, destLabel);
      if (conditions.isPresent()) {
        boolean firstItem = true;

        int limit =
            (this.graphConditionalEdgesLimit == -1)
                ? conditions.get().size()
                : (this.graphConditionalEdgesLimit - 1);

        for (Label conditionLabel : Iterables.limit(conditions.get(), limit)) {
          if (!annotatedLabels.add(conditionLabel)) {
            // duplicate label; skip.
            continue;
          }

          if (!firstItem) {
            buf.append("\\n");
          }

          buf.append(conditionLabel.getCanonicalForm());
          firstItem = false;
        }
        if (conditions.get().size() > limit) {
          buf.append("...");
        }
      }
    }
  }
  return buf.toString();
}