本文整理汇总了Java中com.google.common.collect.Iterators.peekingIterator方法的典型用法代码示例。如果您正苦于以下问题:Java Iterators.peekingIterator方法的具体用法?Java Iterators.peekingIterator怎么用?Java Iterators.peekingIterator使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.google.common.collect.Iterators
的用法示例。
在下文中一共展示了Iterators.peekingIterator方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getNonOverlappingBounds
import com.google.common.collect.Iterators; //导入方法依赖的package包/类
/**
* Retrieves non-overlapping bounds for the list of input bounds
*
* Assume we have the following bounds
* (brackets representing left/right bound):
* [ ] [ ] [ ] [ ]
* [ ] [ ]
* This method will return the following bounds:
* [ ] [ ]
*
* @param bounds unsorted bounds to find overlaps
* @return the non-overlapping bounds
*/
public static <T extends RingPosition<T>> Set<Bounds<T>> getNonOverlappingBounds(Iterable<Bounds<T>> bounds)
{
ArrayList<Bounds<T>> sortedBounds = Lists.newArrayList(bounds);
Collections.sort(sortedBounds, new Comparator<Bounds<T>>()
{
public int compare(Bounds<T> o1, Bounds<T> o2)
{
return o1.left.compareTo(o2.left);
}
});
Set<Bounds<T>> nonOverlappingBounds = Sets.newHashSet();
PeekingIterator<Bounds<T>> it = Iterators.peekingIterator(sortedBounds.iterator());
while (it.hasNext())
{
Bounds<T> beginBound = it.next();
Bounds<T> endBound = beginBound;
while (it.hasNext() && endBound.right.compareTo(it.peek().left) >= 0)
endBound = it.next();
nonOverlappingBounds.add(new Bounds<>(beginBound.left, endBound.right));
}
return nonOverlappingBounds;
}
示例2: visitStatements
import com.google.common.collect.Iterators; //导入方法依赖的package包/类
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);
}
}
}
示例3: readAllForwards
import com.google.common.collect.Iterators; //导入方法依赖的package包/类
@Override
public Stream<ResolvedEvent> readAllForwards(Position positionExclusive) {
try(Stream<ResolvedEvent> allForwards = underlying.readAllForwards(positionExclusive)) {
PeekingIterator<ResolvedEvent> allForwardsIterator = Iterators.peekingIterator(allForwards.iterator());
if (allForwardsIterator.hasNext() && sortKeyExtractor.apply(allForwardsIterator.peek()).compareTo(cutoffSortKey) < 0) {
allForwards.close();
return bufferedAndSortedReadAllForwards(positionExclusive);
}
return stream(spliteratorUnknownSize(allForwardsIterator, ORDERED), false).onClose(allForwards::close);
}
}
示例4: bufferedAndSortedReadAllForwards
import com.google.common.collect.Iterators; //导入方法依赖的package包/类
private Stream<ResolvedEvent> bufferedAndSortedReadAllForwards(Position positionExclusive) {
try(Stream<ResolvedEvent> allForwards = underlying.readAllForwards()) {
Iterator<ResolvedEvent> remainder = allForwards.iterator();
PeekingIterator<EventWithSortKey<T>> sortCandidates = Iterators.peekingIterator(
Iterators.transform(remainder, re -> new EventWithSortKey<>(re, sortKeyExtractor.apply(re)))
);
final LinkedList<EventWithSortKey<T>> buffer = new LinkedList<>();
while (sortCandidates.hasNext() && sortCandidates.peek().sortKey.compareTo(cutoffSortKey) < 0) {
buffer.add(sortCandidates.next());
}
if (!sortCandidates.hasNext()) {
return Stream.empty();
}
buffer.sort(Comparator.naturalOrder());
if (!positionExclusive.equals(underlying.emptyStorePosition())) {
Iterator<EventWithSortKey<T>> bufferIterator = buffer.iterator();
while (!bufferIterator.next().event.position().equals(positionExclusive)) {
bufferIterator.remove();
}
bufferIterator.remove();
}
Stream<EventWithSortKey<T>> reorderedEvents = buffer.stream().onClose(buffer::clear);
Stream<EventWithSortKey<T>> eventInTheGap = Stream.of(sortCandidates.peek());
Stream<ResolvedEvent> remainingEvents = stream(spliteratorUnknownSize(remainder, ORDERED), false);
return concat(concat(reorderedEvents, eventInTheGap).map(EventWithSortKey::toResolvedEvent), remainingEvents).onClose(allForwards::close);
}
}
示例5: Structurizer
import com.google.common.collect.Iterators; //导入方法依赖的package包/类
Structurizer(Iterable<Term> terms) {
this.terms = Iterators.peekingIterator(Iterators.filter(terms.iterator(), whitespaces));
}
示例6: visitForLoop
import com.google.common.collect.Iterators; //导入方法依赖的package包/类
@Override
public Void visitForLoop(ForLoopTree node, Void unused) {
sync(node);
token("for");
builder.space();
token("(");
builder.open(plusFour);
builder.open(
node.getInitializer().size() > 1
&& node.getInitializer().get(0).getKind() == Tree.Kind.EXPRESSION_STATEMENT
? plusFour
: ZERO);
if (!node.getInitializer().isEmpty()) {
if (node.getInitializer().get(0).getKind() == VARIABLE) {
PeekingIterator<StatementTree> it =
Iterators.<StatementTree>peekingIterator(node.getInitializer().iterator());
visitVariables(
variableFragments(it, it.next()), DeclarationKind.NONE, Direction.HORIZONTAL);
} else {
boolean first = true;
builder.open(ZERO);
for (StatementTree t : node.getInitializer()) {
if (!first) {
token(",");
builder.breakOp(" ");
}
scan(((ExpressionStatementTree) t).getExpression(), null);
first = false;
}
token(";");
builder.close();
}
} else {
token(";");
}
builder.close();
builder.breakOp(" ");
if (node.getCondition() != null) {
scan(node.getCondition(), null);
}
token(";");
if (!node.getUpdate().isEmpty()) {
builder.breakOp(" ");
builder.open(node.getUpdate().size() <= 1 ? ZERO : plusFour);
boolean firstUpdater = true;
for (ExpressionStatementTree updater : node.getUpdate()) {
if (!firstUpdater) {
token(",");
builder.breakToFill(" ");
}
scan(updater.getExpression(), null);
firstUpdater = false;
}
builder.guessToken(";");
builder.close();
} else {
builder.space();
}
builder.close();
token(")");
visitStatement(
node.getStatement(),
CollapseEmptyOrNot.YES,
AllowLeadingBlankLine.YES,
AllowTrailingBlankLine.NO);
return null;
}
示例7: addBodyDeclarations
import com.google.common.collect.Iterators; //导入方法依赖的package包/类
/**
* 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();
}
}
}