Java Code Examples for com.google.common.collect.PeekingIterator#hasNext()
The following examples show how to use
com.google.common.collect.PeekingIterator#hasNext() .
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: JavaInputAstVisitor.java From java-n-IDE-for-Android with Apache License 2.0 | 6 votes |
private void visitStatements(List<? extends StatementTree> statements) { boolean first = true; PeekingIterator<StatementTree> it = Iterators.<StatementTree>peekingIterator(statements.iterator()); dropEmptyDeclarations(); while (it.hasNext()) { StatementTree tree = it.next(); builder.forcedBreak(); if (!first) { builder.blankLineWanted(BlankLineWanted.PRESERVE); } markForPartialFormat(); first = false; List<VariableTree> fragments = variableFragments(it, tree); if (!fragments.isEmpty()) { visitVariables( fragments, DeclarationKind.NONE, canLocalHaveHorizontalAnnotations(fragments.get(0).getModifiers())); } else { scan(tree, null); } } }
Example 2
Source File: DefaultSlabAllocator.java From emodb with Apache License 2.0 | 6 votes |
/** Compute how many slots in a slab will be used to do a default (new slab) allocation, and how many bytes it will consume * * @param slabSlotsUsed - number of available slots in a slab that have been used prior to this allocation * @param slabBytesUsed - number of bytes in a slab that have been used prior to this allocation * @param eventSizes - list of the size in bytes of all events that we want to insert into the slab * * @return a pair of integers, the left value is the number of slots that will be used by this allocation and the * right value is the numb er of bytes that will be used by this allocation */ static Pair<Integer, Integer> defaultAllocationCount(int slabSlotsUsed, int slabBytesUsed, PeekingIterator<Integer> eventSizes) { int slabTotalSlotCount = slabSlotsUsed; int allocationSlotCount = 0; int slabTotalBytesUsed = slabBytesUsed; int allocationBytes = 0; while (eventSizes.hasNext()) { checkArgument(eventSizes.peek() <= Constants.MAX_EVENT_SIZE_IN_BYTES, "Event size (" + eventSizes.peek() + ") is greater than the maximum allowed (" + Constants.MAX_EVENT_SIZE_IN_BYTES + ") event size"); if (slabTotalSlotCount + 1 <= Constants.MAX_SLAB_SIZE && slabTotalBytesUsed + eventSizes.peek() <= Constants.MAX_SLAB_SIZE_IN_BYTES) { slabTotalSlotCount++; allocationSlotCount++; int eventSize = eventSizes.next(); slabTotalBytesUsed += eventSize; allocationBytes += eventSize; } else { break; } } return new ImmutablePair<>(allocationSlotCount, allocationBytes); }
Example 3
Source File: Numbers.java From attic-aurora with Apache License 2.0 | 6 votes |
/** * Converts a set of integers into a set of contiguous closed ranges that equally represent the * input integers. * <p> * The resulting ranges will be in ascending order. * <p> * TODO(wfarner): Change this to return a canonicalized RangeSet. * * @param values Values to transform to ranges. * @return Closed ranges with identical members to the input set. */ public static Set<Range<Integer>> toRanges(Iterable<Integer> values) { ImmutableSet.Builder<Range<Integer>> builder = ImmutableSet.builder(); PeekingIterator<Integer> iterator = Iterators.peekingIterator(Sets.newTreeSet(values).iterator()); // Build ranges until there are no numbers left. while (iterator.hasNext()) { // Start a new range. int start = iterator.next(); int end = start; // Increment the end until the range is non-contiguous. while (iterator.hasNext() && iterator.peek() == end + 1) { end++; iterator.next(); } builder.add(Range.closed(start, end)); } return builder.build(); }
Example 4
Source File: MergingIterator.java From lsmtree with Apache License 2.0 | 6 votes |
@Override protected Generation.Entry<K, V> computeNext() { if (heap.isEmpty()) { return endOfData(); } PeekingIterator<EntryAndGenerationId<K,V>> first = heap.poll(); EntryAndGenerationId<K,V> ret = first.next(); if (first.hasNext()) { temp.add(first); } while (!heap.isEmpty() && keyComparator.compare(ret.entry.getKey(), heap.peek().peek().entry.getKey()) == 0) { PeekingIterator<EntryAndGenerationId<K, V>> iter = heap.poll(); iter.next(); if (iter.hasNext()) { temp.add(iter); } } heap.addAll(temp); temp.clear(); return ret.entry; }
Example 5
Source File: TraceCommonService.java From glowroot with Apache License 2.0 | 6 votes |
private static void writeEntries(JsonGenerator jg, List<Trace.Entry> entries) throws IOException { jg.writeStartArray(); PeekingIterator<Trace.Entry> i = Iterators.peekingIterator(entries.iterator()); while (i.hasNext()) { Trace.Entry entry = i.next(); int depth = entry.getDepth(); jg.writeStartObject(); writeJson(entry, jg); int nextDepth = i.hasNext() ? i.peek().getDepth() : 0; if (nextDepth > depth) { jg.writeArrayFieldStart("childEntries"); } else if (nextDepth < depth) { jg.writeEndObject(); for (int j = depth; j > nextDepth; j--) { jg.writeEndArray(); jg.writeEndObject(); } } else { jg.writeEndObject(); } } jg.writeEndArray(); }
Example 6
Source File: AstyanaxEventReaderDAO.java From emodb with Apache License 2.0 | 6 votes |
void readAll(String channel, SlabFilter filter, EventSink sink, boolean weak) { // PeekingIterator is needed so that we can look ahead and see the next slab Id PeekingIterator<Column<ByteBuffer>> manifestColumns = Iterators.peekingIterator(readManifestForChannel(channel, weak)); while (manifestColumns.hasNext()) { Column<ByteBuffer> manifestColumn = manifestColumns.next(); ByteBuffer slabId = manifestColumn.getName(); ByteBuffer nextSlabId = manifestColumns.hasNext() ? manifestColumns.peek().getName() : null; boolean open = manifestColumn.getBooleanValue(); if (filter != null && !filter.accept(slabId, open, nextSlabId)) { continue; } if (!readSlab(channel, slabId, new SlabCursor(), open, sink)) { break; } } }
Example 7
Source File: LocalProperties.java From presto with Apache License 2.0 | 6 votes |
/** * Attempt to match the desired properties to a sequence of known properties. * <p> * Returns a list of the same length as the original. Entries are: * - Optional.empty(): the property was satisfied completely * - non-empty: the (simplified) property that was not satisfied */ public static <T> List<Optional<LocalProperty<T>>> match(List<LocalProperty<T>> actuals, List<LocalProperty<T>> desired) { // After normalizing actuals, each symbol should only appear once PeekingIterator<LocalProperty<T>> actualIterator = peekingIterator(normalizeAndPrune(actuals).iterator()); Set<T> constants = new HashSet<>(); boolean consumeMoreActuals = true; List<Optional<LocalProperty<T>>> result = new ArrayList<>(desired.size()); for (LocalProperty<T> desiredProperty : desired) { while (consumeMoreActuals && actualIterator.hasNext() && desiredProperty.isSimplifiedBy(actualIterator.peek())) { constants.addAll(actualIterator.next().getColumns()); } Optional<LocalProperty<T>> simplifiedDesired = desiredProperty.withConstants(constants); consumeMoreActuals &= simplifiedDesired.isEmpty(); // Only continue processing actuals if all previous desired properties were fully satisfied result.add(simplifiedDesired); } return result; }
Example 8
Source File: LoopingDatasetFinderSource.java From incubator-gobblin with Apache License 2.0 | 5 votes |
/** * Advance an iterator until the next value is larger than the reference. * @return the last value polled if it is equal to reference, or null otherwise. */ @Nullable private <T extends URNIdentified> T advanceUntilLargerThan(PeekingIterator<T> it, String reference) { if (reference == null) { return null; } int comparisonResult = -1; while (it.hasNext() && (comparisonResult = lexicographicalComparator.compare(it.peek(), reference)) < 0) { it.next(); } return comparisonResult == 0 ? it.next() : null; }
Example 9
Source File: Subscription.java From ua-server-sdk with GNU Affero General Public License v3.0 | 5 votes |
/** * Gather {@link MonitoredItemNotification}s and send them using {@code service}, if present. * * @param iterator a {@link PeekingIterator} over the current {@link BaseMonitoredItem}s. * @param service a {@link ServiceRequest}, if available. */ private void gatherAndSend(PeekingIterator<BaseMonitoredItem<?>> iterator, Optional<ServiceRequest<PublishRequest, PublishResponse>> service) { if (service.isPresent()) { List<UaStructure> notifications = Lists.newArrayList(); while (notifications.size() < maxNotificationsPerPublish && iterator.hasNext()) { BaseMonitoredItem<?> item = iterator.peek(); boolean gatheredAllForItem = gather(item, notifications, maxNotificationsPerPublish); if (gatheredAllForItem && iterator.hasNext()) { iterator.next(); } } moreNotifications = iterator.hasNext(); sendNotifications(service.get(), notifications); if (moreNotifications) { gatherAndSend(iterator, Optional.ofNullable(publishQueue().poll())); } } else { if (moreNotifications) { publishQueue().addSubscription(this); } } }
Example 10
Source File: JavaInputAstVisitor.java From java-n-IDE-for-Android with Apache License 2.0 | 5 votes |
/** * The parser expands multi-variable declarations into separate single-variable declarations. All * of the fragments in the original declaration have the same start position, so we use that as a * signal to collect them and preserve the multi-variable declaration in the output. * <p> * <p>e.g. {@code int x, y;} is parsed as {@code int x; int y;}. */ private List<VariableTree> variableFragments(PeekingIterator<? extends Tree> it, Tree first) { List<VariableTree> fragments = new ArrayList<>(); if (first.getKind() == VARIABLE) { int start = getStartPosition(first); fragments.add((VariableTree) first); while (it.hasNext() && it.peek().getKind() == VARIABLE && getStartPosition(it.peek()) == start) { fragments.add((VariableTree) it.next()); } } return fragments; }
Example 11
Source File: DatabusResource1.java From emodb with Apache License 2.0 | 5 votes |
private static <T> Iterator<T> streamingIterator(Iterator<T> iterator) { // Force the calculation of at least the first item in the iterator so that, if an exception occurs, we find // out before writing the HTTP response code & headers. Otherwise we will at best report a 500 error instead // of applying Jersey exception mappings and maybe returning a 400 error etc. PeekingIterator<T> peekingIterator = Iterators.peekingIterator(iterator); if (peekingIterator.hasNext()) { peekingIterator.peek(); } return new LoggingIterator<>(peekingIterator, _log); }
Example 12
Source File: FullyQualifiedName.java From bazel with Apache License 2.0 | 5 votes |
private static Qualifiers getQualifiers(Iterable<String> dirNameAndQualifiers) { PeekingIterator<String> rawQualifiers = Iterators.peekingIterator(dirNameAndQualifiers.iterator()); // Remove directory name final ResourceFolderType folderType = ResourceFolderType.getTypeByName(rawQualifiers.next()); // If there is no folder type, there are no qualifiers to parse. if (folderType == null) { return EMPTY_QUALIFIERS; } List<String> handledQualifiers = new ArrayList<>(); // Do some substitution of language/region qualifiers. while (rawQualifiers.hasNext()) { handledQualifiers.add(rawQualifiers.next()); } // Create a configuration FolderConfiguration config = FolderConfiguration.getConfigFromQualifiers(handledQualifiers); // FolderConfiguration returns an unhelpful null when it considers the qualifiers to be // invalid. if (config == null) { throw new IllegalArgumentException( String.format(INVALID_QUALIFIERS, DASH_JOINER.join(dirNameAndQualifiers))); } config.normalize(); ImmutableList.Builder<String> builder = ImmutableList.<String>builder(); // index 3 is past the country code, network code, and locale indices. for (int i = 0; i < FolderConfiguration.getQualifierCount(); ++i) { addIfNotNull(config.getQualifier(i), builder); } return new Qualifiers(folderType, builder.build(), config.getLocaleQualifier() == null); }
Example 13
Source File: EntityProcessorImpl.java From SciGraph with Apache License 2.0 | 5 votes |
/*** * Convert a list of annotations into annotation groups * * @param annotationList * Annotations * @param longestOnly * If shorter entities from annotation groups should be removed * @return annotation groups */ static List<EntityAnnotationGroup> getAnnotationGroups(List<EntityAnnotation> annotationList, boolean longestOnly) { List<EntityAnnotationGroup> groups = new ArrayList<>(); Collections.sort(annotationList, Collections.reverseOrder()); PeekingIterator<EntityAnnotation> iter = Iterators.peekingIterator(annotationList.iterator()); while (iter.hasNext()) { EntityAnnotationGroup group = new EntityAnnotationGroup(); group.add(iter.next()); Set<Entity> entitiesInGroup = new HashSet<>(); while (iter.hasNext() && group.intersects(iter.peek())) { if (!entitiesInGroup.contains(iter.peek().getToken())) { entitiesInGroup.add(iter.peek().getToken()); group.add(iter.next()); } else { iter.next(); } } if (longestOnly) { // Remove any entries that aren't as long as the first one Iterator<EntityAnnotation> groupIter = group.iterator(); int longest = group.peek().length(); while (groupIter.hasNext()) { EntityAnnotation annot = groupIter.next(); if (annot.length() < longest) { groupIter.remove(); } } } groups.add(group); } return groups; }
Example 14
Source File: LocalProperties.java From presto with Apache License 2.0 | 5 votes |
public static <T> Set<T> extractLeadingConstants(List<? extends LocalProperty<T>> properties) { ImmutableSet.Builder<T> builder = ImmutableSet.builder(); PeekingIterator<? extends LocalProperty<T>> iterator = peekingIterator(properties.iterator()); while (iterator.hasNext() && iterator.peek() instanceof ConstantProperty) { builder.add(((ConstantProperty<T>) iterator.next()).getColumn()); } return builder.build(); }
Example 15
Source File: JavaInputAstVisitor.java From google-java-format with Apache License 2.0 | 4 votes |
/** Add a list of declarations. */ protected void addBodyDeclarations( List<? extends Tree> bodyDeclarations, BracesOrNot braces, FirstDeclarationsOrNot first0) { if (bodyDeclarations.isEmpty()) { if (braces.isYes()) { builder.space(); tokenBreakTrailingComment("{", plusTwo); builder.blankLineWanted(BlankLineWanted.NO); builder.open(ZERO); token("}", plusTwo); builder.close(); } } else { if (braces.isYes()) { builder.space(); tokenBreakTrailingComment("{", plusTwo); builder.open(ZERO); } builder.open(plusTwo); boolean first = first0.isYes(); boolean lastOneGotBlankLineBefore = false; PeekingIterator<Tree> it = Iterators.peekingIterator(bodyDeclarations.iterator()); while (it.hasNext()) { Tree bodyDeclaration = it.next(); dropEmptyDeclarations(); builder.forcedBreak(); boolean thisOneGetsBlankLineBefore = bodyDeclaration.getKind() != VARIABLE || hasJavaDoc(bodyDeclaration); if (first) { builder.blankLineWanted(PRESERVE); } else if (!first && (thisOneGetsBlankLineBefore || lastOneGotBlankLineBefore)) { builder.blankLineWanted(YES); } markForPartialFormat(); if (bodyDeclaration.getKind() == VARIABLE) { visitVariables( variableFragments(it, bodyDeclaration), DeclarationKind.FIELD, fieldAnnotationDirection(((VariableTree) bodyDeclaration).getModifiers())); } else { scan(bodyDeclaration, null); } first = false; lastOneGotBlankLineBefore = thisOneGetsBlankLineBefore; } dropEmptyDeclarations(); builder.forcedBreak(); builder.close(); builder.forcedBreak(); markForPartialFormat(); if (braces.isYes()) { builder.blankLineWanted(BlankLineWanted.NO); token("}", plusTwo); builder.close(); } } }
Example 16
Source File: TaggingTokenClusteror.java From science-result-extractor with Apache License 2.0 | 4 votes |
public List<TaggingTokenCluster> cluster() { List<TaggingTokenCluster> result = new ArrayList<>(); PeekingIterator<LabeledTokensContainer> it = Iterators.peekingIterator(taggingTokenSynchronizer); if (!it.hasNext() || (it.peek() == null)) { return Collections.emptyList(); } // a boolean is introduced to indicate the start of the sequence in the case the label // has no beginning indicator (e.g. I-) boolean begin = true; TaggingTokenCluster curCluster = new TaggingTokenCluster(it.peek().getTaggingLabel()); BoundingBox curBox=null; while (it.hasNext()) { LabeledTokensContainer cont = it.next(); BoundingBox b = BoundingBox.fromLayoutToken(cont.getLayoutTokens().get(0)); if(!curCluster.concatTokens().isEmpty()){ curBox = BoundingBox.fromLayoutToken(curCluster.concatTokens().get(0)); if(b.distanceTo(curBox)>600){ curCluster = new TaggingTokenCluster(cont.getTaggingLabel()); result.add(curCluster); } } if (begin || cont.isBeginning() || cont.getTaggingLabel() != curCluster.getTaggingLabel()) { curCluster = new TaggingTokenCluster(cont.getTaggingLabel()); result.add(curCluster); } //for table, seperate caption and content if(curCluster!=null){ String tableStr = LayoutTokensUtil.normalizeText(curCluster.concatTokens()); if(tableStr.matches(".*?(Table|TABLE) \\d+(:|\\.| [A-Z]).*?")){ // if(tableStr.matches(".*?(Table|TABLE|Figure|FIGURE) \\d+(:|\\.).*?")){ if(toText(curCluster.getLastContainer().getLayoutTokens()).equalsIgnoreCase(". \n\n")){ curCluster = new TaggingTokenCluster(cont.getTaggingLabel()); result.add(curCluster); } } } curCluster.addLabeledTokensContainer(cont); if (begin) begin = false; } return result; }
Example 17
Source File: RestoreCommitLogsAction.java From nomulus with Apache License 2.0 | 4 votes |
@Override public void run() { checkArgument( RegistryEnvironment.get() == RegistryEnvironment.ALPHA || RegistryEnvironment.get() == RegistryEnvironment.CRASH || RegistryEnvironment.get() == RegistryEnvironment.UNITTEST, "DO NOT RUN ANYWHERE ELSE EXCEPT ALPHA, CRASH OR TESTS."); if (dryRun) { logger.atInfo().log("Running in dryRun mode"); } List<GcsFileMetadata> diffFiles = diffLister.listDiffFiles(fromTime, toTime); if (diffFiles.isEmpty()) { logger.atInfo().log("Nothing to restore"); return; } Map<Integer, DateTime> bucketTimestamps = new HashMap<>(); CommitLogCheckpoint lastCheckpoint = null; for (GcsFileMetadata metadata : diffFiles) { logger.atInfo().log("Restoring: %s", metadata.getFilename().getObjectName()); try (InputStream input = Channels.newInputStream( gcsService.openPrefetchingReadChannel(metadata.getFilename(), 0, BLOCK_SIZE))) { PeekingIterator<ImmutableObject> commitLogs = peekingIterator(createDeserializingIterator(input)); lastCheckpoint = (CommitLogCheckpoint) commitLogs.next(); saveOfy(ImmutableList.of(lastCheckpoint)); // Save the checkpoint itself. while (commitLogs.hasNext()) { CommitLogManifest manifest = restoreOneTransaction(commitLogs); bucketTimestamps.put(manifest.getBucketId(), manifest.getCommitTime()); } } catch (IOException e) { throw new RuntimeException(e); } } // Restore the CommitLogCheckpointRoot and CommitLogBuckets. saveOfy( Streams.concat( bucketTimestamps .entrySet() .stream() .map( entry -> new CommitLogBucket.Builder() .setBucketNum(entry.getKey()) .setLastWrittenTime(entry.getValue()) .build()), Stream.of(CommitLogCheckpointRoot.create(lastCheckpoint.getCheckpointTime()))) .collect(toImmutableList())); logger.atInfo().log("Restore complete"); }
Example 18
Source File: JavaInputAstVisitor.java From javaide with GNU General Public License v3.0 | 4 votes |
/** * Add a list of declarations. */ void addBodyDeclarations( List<? extends Tree> bodyDeclarations, BracesOrNot braces, FirstDeclarationsOrNot first0) { if (bodyDeclarations.isEmpty()) { if (braces.isYes()) { builder.space(); tokenBreakTrailingComment("{", plusTwo); builder.blankLineWanted(BlankLineWanted.NO); builder.open(ZERO); token("}", plusTwo); builder.close(); } } else { if (braces.isYes()) { builder.space(); tokenBreakTrailingComment("{", plusTwo); builder.open(ZERO); } builder.open(plusTwo); boolean first = first0.isYes(); boolean lastOneGotBlankLineBefore = false; PeekingIterator<Tree> it = Iterators.<Tree>peekingIterator(bodyDeclarations.iterator()); while (it.hasNext()) { Tree bodyDeclaration = it.next(); dropEmptyDeclarations(); builder.forcedBreak(); boolean thisOneGetsBlankLineBefore = bodyDeclaration.getKind() != VARIABLE || hasJavaDoc(bodyDeclaration); if (first) { builder.blankLineWanted(PRESERVE); } else if (!first && (thisOneGetsBlankLineBefore || lastOneGotBlankLineBefore)) { builder.blankLineWanted(YES); } markForPartialFormat(); if (bodyDeclaration.getKind() == VARIABLE) { visitVariables( variableFragments(it, bodyDeclaration), DeclarationKind.FIELD, fieldAnnotationDirection(((VariableTree) bodyDeclaration).getModifiers())); } else { scan(bodyDeclaration, null); } first = false; lastOneGotBlankLineBefore = thisOneGetsBlankLineBefore; } dropEmptyDeclarations(); builder.forcedBreak(); builder.close(); builder.forcedBreak(); markForPartialFormat(); if (braces.isYes()) { builder.blankLineWanted(BlankLineWanted.NO); token("}", plusTwo); builder.close(); } } }
Example 19
Source File: IndentationAwareCompletionPrefixProvider.java From xtext-core with Eclipse Public License 2.0 | 4 votes |
protected INode findBestEndToken(INode root, INode candidate, int completionColumn, boolean candidateIsEndToken) { LinkedList<ILeafNode> sameGrammarElement = Lists.newLinkedList(); PeekingIterator<ILeafNode> iterator = createReversedLeafIterator(root, candidate, sameGrammarElement); if (!iterator.hasNext()) { return candidate; } // collect all candidates that belong to the same offset LinkedList<ILeafNode> sameOffset = candidateIsEndToken ? collectLeafsWithSameOffset((ILeafNode)candidate, iterator) : Lists.newLinkedList(); // continue until we find a paired leaf with length 0 that is at the correct offset EObject grammarElement = tryGetGrammarElementAsRule(candidateIsEndToken || sameGrammarElement.isEmpty() ? candidate : sameGrammarElement.getLast()); ILeafNode result = candidateIsEndToken ? null : (ILeafNode) candidate; int sameOffsetSize = sameOffset.size(); while(iterator.hasNext()) { ILeafNode next = iterator.next(); if (result == null || result.isHidden()) { result = next; } if (next.getTotalLength() == 0) { // potential indentation token EObject rule = tryGetGrammarElementAsRule(next); if (rule != grammarElement) { LineAndColumn lineAndColumn = NodeModelUtils.getLineAndColumn(root, next.getTotalOffset()); if (lineAndColumn.getColumn() <= completionColumn) { return result; } else { if (sameOffset.isEmpty()) { if (sameGrammarElement.isEmpty()) { result = null; } else { result = sameGrammarElement.removeLast(); } } else { if (sameOffsetSize >= sameOffset.size()) { result = sameOffset.removeLast(); } else { sameOffset.removeLast(); } } } } else { sameOffset.add(next); } } } return candidate; }
Example 20
Source File: Subscription.java From ua-server-sdk with GNU Affero General Public License v3.0 | 3 votes |
private void returnNotifications(ServiceRequest<PublishRequest, PublishResponse> service) { LinkedHashSet<BaseMonitoredItem<?>> items = new LinkedHashSet<>(); lastIterator.forEachRemaining(items::add); itemsById.values().stream() .filter(item -> item.hasNotifications() || item.isTriggered()) .forEach(items::add); PeekingIterator<BaseMonitoredItem<?>> iterator = Iterators.peekingIterator(items.iterator()); gatherAndSend(iterator, Optional.of(service)); lastIterator = iterator.hasNext() ? iterator : Collections.emptyIterator(); }