com.google.common.collect.Iterators#peekingIterator ( )源码实例Demo

下面列出了com.google.common.collect.Iterators#peekingIterator ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: glowroot   文件: PreloadSomeSuperTypesCache.java
@Override
public boolean processLine(String line) {
    PeekingIterator<String> i = Iterators.peekingIterator(splitter.split(line).iterator());
    try {
        linesInFile++;
        if (hasAccessTimes == null) {
            char c = i.peek().charAt(0);
            hasAccessTimes = c >= '0' && c <= '9';
        }
        long accessTime = hasAccessTimes ? Long.parseLong(i.next()) : 0;
        String typeName = i.next();
        CacheValue cacheValue = new CacheValue(accessTime, ImmutableSet.copyOf(i));
        cache.put(typeName, cacheValue);
    } catch (Exception e) {
        // e.g. NoSuchElementException or NumberFormatException
        if (!errorLogged) {
            logger.error("error parsing {}: {}", file.getAbsolutePath(), e.getMessage(), e);
        }
        errorLogged = true;
    }
    return true;
}
 
源代码2 项目: google-java-format   文件: JavaInputAstVisitor.java
protected void visitStatements(List<? extends StatementTree> statements) {
  boolean first = true;
  PeekingIterator<StatementTree> it = Iterators.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);
    }
  }
}
 
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);
        }
    }
}
 
源代码4 项目: glowroot   文件: TraceCommonService.java
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();
}
 
源代码5 项目: accumulo-examples   文件: FileDataQuery.java
public ChunkInputStream getData(String hash) throws IOException {
  scanner.setRange(new Range(hash));
  scanner.setBatchSize(1);
  lastRefs.clear();
  PeekingIterator<Entry<Key,Value>> pi = Iterators.peekingIterator(scanner.iterator());
  if (pi.hasNext()) {
    while (!pi.peek().getKey().getColumnFamily().equals(FileDataIngest.CHUNK_CF)) {
      lastRefs.add(pi.peek());
      pi.next();
    }
  }
  cis.clear();
  cis.setSource(pi);
  return cis;
}
 
源代码6 项目: accumulo-examples   文件: ChunkInputStreamTest.java
@Test
public void testReadIntoBufferLargerThanChunks() throws IOException {
  ChunkInputStream cis = new ChunkInputStream();
  byte[] b = new byte[20];
  int read;
  PeekingIterator<Entry<Key,Value>> pi = Iterators.peekingIterator(data.iterator());

  cis.setSource(pi);
  assertEquals(read = cis.read(b), 8);
  assertEquals(new String(b, 0, read), "asdfjkl;");
  assertEquals(read = cis.read(b), -1);

  cis.setSource(pi);
  assertEquals(read = cis.read(b), 10);
  assertEquals(new String(b, 0, read), "qwertyuiop");
  assertEquals(read = cis.read(b), -1);
  assertEquals(cis.getVisibilities().toString(), "[A&B, B&C, D]");
  cis.close();

  cis.setSource(pi);
  assertEquals(read = cis.read(b), 16);
  assertEquals(new String(b, 0, read), "asdfjkl;asdfjkl;");
  assertEquals(read = cis.read(b), -1);
  assertEquals(cis.getVisibilities().toString(), "[A&B]");
  cis.close();

  cis.setSource(pi);
  assertEquals(read = cis.read(b), -1);
  cis.close();

  cis.setSource(pi);
  assertEquals(read = cis.read(b), 8);
  assertEquals(new String(b, 0, read), "asdfjkl;");
  assertEquals(read = cis.read(b), -1);
  cis.close();

  assertFalse(pi.hasNext());
}
 
源代码7 项目: lsmtree   文件: ItUtil.java
public static <A> Iterator<Iterator<A>> groupBy(final F2<A,A,Boolean> f, final Iterator<A> iterator) {
    return new AbstractIterator<Iterator<A>>() {
        PeekingIterator<A> it = Iterators.peekingIterator(iterator);
        InvalidatableIterator<A> prev = null;
        @Override
        protected Iterator<A> computeNext() {
            if (it.hasNext()) {
                if (prev != null) {
                    while (prev.hasNext()) prev.next();
                    prev.invalidate();
                }
                prev = new InvalidatableIterator<A>() {
                    A a;
                    boolean initialized = false;
                    @Override
                    protected A computeNext1() {
                        if (!initialized) {
                            a = it.next();
                            initialized = true;
                            return a;
                        }
                        if (it.hasNext()) {
                            if (f.f(a, it.peek())) {
                                a = it.next();
                                return a;
                            }
                        }
                        endOfData();
                        return null;
                    }
                };
                return prev;
            }
            endOfData();
            return null;
        }
    };
}
 
源代码8 项目: nomulus   文件: WebWhoisToken.java
@Inject
public WebWhoisToken(@WebWhoisProtocol ImmutableList<String> topLevelDomainsList) {
  checkArgument(!topLevelDomainsList.isEmpty(), "topLevelDomainsList must not be empty.");

  this.tldCycleIterator =
      Iterators.peekingIterator(Iterables.cycle(topLevelDomainsList).iterator());
}
 
源代码9 项目: lsmtree   文件: ItUtil.java
public static <A> P2<Iterator<A>, Iterator<A>> span(final F<A,Boolean> f, final Iterator<A> it) {
    return new P2<Iterator<A>, Iterator<A>>() {
        PeekingIterator<A> peekingIterator = Iterators.peekingIterator(it);
        boolean firstDone = false;
        Iterator<A> first = new AbstractIterator<A>() {
            @Override
            protected A computeNext() {
                if (firstDone) throw new IllegalStateException("cannot access first iterator after second has been accessed");
                if (peekingIterator.hasNext()) {
                    if (f.f(peekingIterator.peek())) {
                        return peekingIterator.next();
                    }
                }
                endOfData();
                firstDone = true;
                return null;
            }
        };
        Iterator<A> second = new AbstractIterator<A>() {
            @Override
            protected A computeNext() {
                if (!firstDone) {
                    while (first.hasNext()) first.next();
                    firstDone = true;
                }
                if (peekingIterator.hasNext()) return peekingIterator.next();
                endOfData();
                return null;
            }
        };
        @Override
        public Iterator<A> _1() {
            return first;
        }

        @Override
        public Iterator<A> _2() {
            return second;
        }
    };
}
 
源代码10 项目: qpid-broker-j   文件: AMQPConnection_1_0Impl.java
@Override
public void receive(final List<ChannelFrameBody> channelFrameBodies)
{
    if (!channelFrameBodies.isEmpty())
    {
        PeekingIterator<ChannelFrameBody> itr = Iterators.peekingIterator(channelFrameBodies.iterator());
        boolean cleanExit = false;
        try
        {
            while (itr.hasNext())
            {
                final ChannelFrameBody channelFrameBody = itr.next();
                final int frameChannel = channelFrameBody.getChannel();

                Session_1_0 session = _receivingSessions == null || frameChannel >= _receivingSessions.length
                        ? null
                        : _receivingSessions[frameChannel];
                if (session != null)
                {
                    final AccessControlContext context = session.getAccessControllerContext();
                    AccessController.doPrivileged((PrivilegedAction<Void>) () ->
                    {
                        ChannelFrameBody channelFrame = channelFrameBody;
                        boolean nextIsSameChannel;
                        do
                        {
                            received(frameChannel, channelFrame.getFrameBody());
                            nextIsSameChannel = itr.hasNext() && frameChannel == itr.peek().getChannel();
                            if (nextIsSameChannel)
                            {
                                channelFrame = itr.next();
                            }
                        }
                        while (nextIsSameChannel);
                        return null;
                    }, context);
                }
                else
                {
                    received(frameChannel, channelFrameBody.getFrameBody());
                }
            }
            cleanExit = true;
        }
        finally
        {
            if (!cleanExit)
            {
                while (itr.hasNext())
                {
                    final Object frameBody = itr.next().getFrameBody();
                    if (frameBody instanceof Transfer)
                    {
                        ((Transfer) frameBody).dispose();
                    }
                }
            }
        }
    }
}
 
源代码11 项目: accumulo-examples   文件: ChunkInputStreamIT.java
@Test
public void testWithAccumulo() throws AccumuloException, AccumuloSecurityException,
    TableExistsException, TableNotFoundException, IOException {
  client.tableOperations().create(tableName);
  BatchWriter bw = client.createBatchWriter(tableName, new BatchWriterConfig());

  for (Entry<Key,Value> e : data) {
    Key k = e.getKey();
    Mutation m = new Mutation(k.getRow());
    m.put(k.getColumnFamily(), k.getColumnQualifier(),
        new ColumnVisibility(k.getColumnVisibility()), e.getValue());
    bw.addMutation(m);
  }
  bw.close();

  Scanner scan = client.createScanner(tableName, AUTHS);

  ChunkInputStream cis = new ChunkInputStream();
  byte[] b = new byte[20];
  int read;
  PeekingIterator<Entry<Key,Value>> pi = Iterators.peekingIterator(scan.iterator());

  cis.setSource(pi);
  assertEquals(read = cis.read(b), 8);
  assertEquals(new String(b, 0, read), "asdfjkl;");
  assertEquals(read = cis.read(b), -1);

  cis.setSource(pi);
  assertEquals(read = cis.read(b), 10);
  assertEquals(new String(b, 0, read), "qwertyuiop");
  assertEquals(read = cis.read(b), -1);
  assertEquals(cis.getVisibilities().toString(), "[A&B, B&C, D]");
  cis.close();

  cis.setSource(pi);
  assertEquals(read = cis.read(b), 16);
  assertEquals(new String(b, 0, read), "asdfjkl;asdfjkl;");
  assertEquals(read = cis.read(b), -1);
  assertEquals(cis.getVisibilities().toString(), "[A&B]");
  cis.close();

  cis.setSource(pi);
  assertEquals(read = cis.read(b), -1);
  cis.close();

  cis.setSource(pi);
  assertEquals(read = cis.read(b), 8);
  assertEquals(new String(b, 0, read), "asdfjkl;");
  assertEquals(read = cis.read(b), -1);
  cis.close();

  assertFalse(pi.hasNext());
}
 
源代码12 项目: kylin   文件: GTAggregateTransformScanner.java
PrefixFragmentIterator(Iterator<GTRecord> input, ImmutableBitSet prefixMask) {
    this.input = Iterators.peekingIterator(input);
    this.prefixComparator = GTRecord.getComparator(prefixMask);
}
 
源代码13 项目: datawave   文件: ScannerStream.java
private ScannerStream(Iterator<Tuple2<String,IndexInfo>> itr, StreamContext ctx, JexlNode currNode, IndexStream debugDelegate) {
    this(Iterators.peekingIterator(itr), ctx, currNode, debugDelegate);
}
 
源代码14 项目: sasi   文件: OnDiskIndex.java
@Override
protected void performSkipTo(Long nextToken)
{
    currentIterator = Iterators.peekingIterator(tokens.tailMap(nextToken, true).values().iterator());
}
 
源代码15 项目: accumulo-examples   文件: ChunkInputStreamTest.java
@Test
public void testBadData() throws IOException {
  ChunkInputStream cis = new ChunkInputStream();
  byte[] b = new byte[20];
  int read;
  PeekingIterator<Entry<Key,Value>> pi = Iterators.peekingIterator(baddata.iterator());

  cis.setSource(pi);
  assumeExceptionOnRead(cis, b);
  assumeExceptionOnClose(cis);
  // can still get visibilities after exception -- bad?
  assertEquals(cis.getVisibilities().toString(), "[A]");

  cis.setSource(pi);
  assumeExceptionOnRead(cis, b);
  assumeExceptionOnClose(cis);
  assertEquals(cis.getVisibilities().toString(), "[B, C]");

  cis.setSource(pi);
  assumeExceptionOnRead(cis, b);
  assumeExceptionOnClose(cis);
  assertEquals(cis.getVisibilities().toString(), "[D, E]");

  cis.setSource(pi);
  assertEquals(read = cis.read(b), 8);
  assertEquals(new String(b, 0, read), "asdfjkl;");
  assertEquals(read = cis.read(b), -1);
  assertEquals(cis.getVisibilities().toString(), "[F, G]");
  cis.close();

  cis.setSource(pi);
  assumeExceptionOnRead(cis, b);
  cis.close();
  assertEquals(cis.getVisibilities().toString(), "[I, J]");

  try {
    cis.setSource(pi);
    fail();
  } catch (IOException e) {
    // expected, ignore
  }
  assumeExceptionOnClose(cis);
  assertEquals(cis.getVisibilities().toString(), "[K]");

  cis.setSource(pi);
  assertEquals(read = cis.read(b), -1);
  assertEquals(cis.getVisibilities().toString(), "[L]");
  cis.close();

  assertFalse(pi.hasNext());

  pi = Iterators.peekingIterator(baddata.iterator());
  cis.setSource(pi);
  assumeExceptionOnClose(cis);
}
 
源代码16 项目: glowroot   文件: MutableProfile.java
public void merge(List<StackTraceElement> stackTraceElements, Thread.State threadState) {
    PeekingIterator<StackTraceElement> i =
            Iterators.peekingIterator(Lists.reverse(stackTraceElements).iterator());
    ProfileNode lastMatchedNode = null;
    List<ProfileNode> mergeIntoNodes = rootNodes;

    boolean lookingForMatch = true;
    while (i.hasNext()) {
        StackTraceElement stackTraceElement = i.next();
        String fullClassName = stackTraceElement.getClassName();
        int index = fullClassName.lastIndexOf('.');
        String packageName;
        String className;
        if (index == -1) {
            packageName = "";
            className = fullClassName;
        } else {
            packageName = fullClassName.substring(0, index);
            className = fullClassName.substring(index + 1);
        }
        int packageNameIndex = getNameIndex(packageName, packageNameIndexes, packageNames);
        int classNameIndex = getNameIndex(className, classNameIndexes, classNames);
        int methodNameIndex =
                getNameIndex(MoreObjects.firstNonNull(stackTraceElement.getMethodName(),
                        "<null method name>"), methodNameIndexes, methodNames);
        int fileNameIndex = getNameIndex(Strings.nullToEmpty(stackTraceElement.getFileName()),
                fileNameIndexes, fileNames);
        int lineNumber = stackTraceElement.getLineNumber();
        Profile.LeafThreadState leafThreadState =
                i.hasNext() ? Profile.LeafThreadState.NONE : getThreadState(threadState);

        ProfileNode node = null;
        if (lookingForMatch) {
            for (ProfileNode childNode : mergeIntoNodes) {
                if (isMatch(childNode, packageNameIndex, classNameIndex, methodNameIndex,
                        fileNameIndex, lineNumber, leafThreadState)) {
                    node = childNode;
                    break;
                }
            }
        }
        if (node == null) {
            lookingForMatch = false;
            node = new ProfileNode(packageNameIndex, classNameIndex, methodNameIndex,
                    fileNameIndex, lineNumber, leafThreadState);
            mergeIntoNodes.add(node);
        }
        node.sampleCount++;
        lastMatchedNode = node;
        mergeIntoNodes = lastMatchedNode.childNodes;
    }
}
 
源代码17 项目: fluo   文件: ColumnScannerImpl.java
ColumnScannerImpl(Iterator<Entry<Key, Value>> e, Function<Key, Column> columnConverter) {
  peekingIter = Iterators.peekingIterator(e);
  this.columnConverter = columnConverter;
  row = ByteUtil.toBytes(peekingIter.peek().getKey().getRowData());
  iter = Iterators.transform(peekingIter, this::entry2cv);
}
 
源代码18 项目: mizo   文件: MizoVerticesIterator.java
public MizoVerticesIterator(Iterator<IMizoRelationParser> relationsIterator, IMizoRDDConfig config) {
    super(config);

    this.relationsIterator = Iterators.peekingIterator(relationsIterator);
}
 
/**
 * 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();
        }
    }
}
 
源代码20 项目: mizo   文件: MizoRegionFamilyCellsIterator.java
/**
 * Creates an ascending-sorted cells iterator, wrapped by a peeking iterator
 * @param regionEdgesFamilyPath Path of the HBase directory that contains Titan's Edges column-family
 * @return Ascending-sorted cells iterator, wrapped by a peeking iterator
 */
protected PeekingIterator<Cell> createSortedHFilesIterator(String regionEdgesFamilyPath) throws IOException {
    return Iterators.peekingIterator(
            Iterators.mergeSorted(createHFilesIterators(regionEdgesFamilyPath), ASC_CELL_COMPARATOR)
    );
}