Java Code Examples for com.google.common.collect.Iterators#peekingIterator()

The following examples show how to use com.google.common.collect.Iterators#peekingIterator() . 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: TraceCommonService.java    From glowroot with Apache License 2.0 6 votes vote down vote up
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 2
Source File: JavaInputAstVisitor.java    From google-java-format with Apache License 2.0 6 votes vote down vote up
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);
    }
  }
}
 
Example 3
Source File: JavaInputAstVisitor.java    From java-n-IDE-for-Android with Apache License 2.0 6 votes vote down vote up
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 4
Source File: PreloadSomeSuperTypesCache.java    From glowroot with Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example 5
Source File: WebWhoisToken.java    From nomulus with Apache License 2.0 5 votes vote down vote up
@Inject
public WebWhoisToken(@WebWhoisProtocol ImmutableList<String> topLevelDomainsList) {
  checkArgument(!topLevelDomainsList.isEmpty(), "topLevelDomainsList must not be empty.");

  this.tldCycleIterator =
      Iterators.peekingIterator(Iterables.cycle(topLevelDomainsList).iterator());
}
 
Example 6
Source File: ItUtil.java    From lsmtree with Apache License 2.0 5 votes vote down vote up
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;
        }
    };
}
 
Example 7
Source File: FileDataQuery.java    From accumulo-examples with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 8
Source File: ChunkInputStreamTest.java    From accumulo-examples with Apache License 2.0 5 votes vote down vote up
@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());
}
 
Example 9
Source File: MutableProfile.java    From glowroot with Apache License 2.0 4 votes vote down vote up
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;
    }
}
 
Example 10
Source File: MizoRegionFamilyCellsIterator.java    From mizo with Apache License 2.0 4 votes vote down vote up
/**
 * 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)
    );
}
 
Example 11
Source File: JavaInputAstVisitor.java    From java-n-IDE-for-Android with Apache License 2.0 4 votes vote down vote up
/**
 * 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 12
Source File: MizoVerticesIterator.java    From mizo with Apache License 2.0 4 votes vote down vote up
public MizoVerticesIterator(Iterator<IMizoRelationParser> relationsIterator, IMizoRDDConfig config) {
    super(config);

    this.relationsIterator = Iterators.peekingIterator(relationsIterator);
}
 
Example 13
Source File: ColumnScannerImpl.java    From fluo with Apache License 2.0 4 votes vote down vote up
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);
}
 
Example 14
Source File: ChunkInputStreamTest.java    From accumulo-examples with Apache License 2.0 4 votes vote down vote up
@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);
}
 
Example 15
Source File: OnDiskIndex.java    From sasi with Apache License 2.0 4 votes vote down vote up
@Override
protected void performSkipTo(Long nextToken)
{
    currentIterator = Iterators.peekingIterator(tokens.tailMap(nextToken, true).values().iterator());
}
 
Example 16
Source File: ScannerStream.java    From datawave with Apache License 2.0 4 votes vote down vote up
private ScannerStream(Iterator<Tuple2<String,IndexInfo>> itr, StreamContext ctx, JexlNode currNode, IndexStream debugDelegate) {
    this(Iterators.peekingIterator(itr), ctx, currNode, debugDelegate);
}
 
Example 17
Source File: GTAggregateTransformScanner.java    From kylin with Apache License 2.0 4 votes vote down vote up
PrefixFragmentIterator(Iterator<GTRecord> input, ImmutableBitSet prefixMask) {
    this.input = Iterators.peekingIterator(input);
    this.prefixComparator = GTRecord.getComparator(prefixMask);
}
 
Example 18
Source File: ChunkInputStreamIT.java    From accumulo-examples with Apache License 2.0 4 votes vote down vote up
@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());
}
 
Example 19
Source File: AMQPConnection_1_0Impl.java    From qpid-broker-j with Apache License 2.0 4 votes vote down vote up
@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();
                    }
                }
            }
        }
    }
}
 
Example 20
Source File: ItUtil.java    From lsmtree with Apache License 2.0 4 votes vote down vote up
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;
        }
    };
}