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

The following examples show how to use com.google.common.collect.Iterators#addAll() . 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: TokenTree.java    From sasi with Apache License 2.0 6 votes vote down vote up
@Override
public void merge(CombinedValue<Long> other)
{
    if (!(other instanceof Token))
        return;

    Token o = (Token) other;
    if (token != o.token)
        throw new IllegalArgumentException(String.format("%s != %s", token, o.token));

    if (o instanceof OnDiskToken)
    {
        info.addAll(((OnDiskToken) other).info);
    }
    else
    {
        Iterators.addAll(loadedKeys, o.iterator());
    }
}
 
Example 2
Source File: Formatter.java    From java-n-IDE-for-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Converts zero-indexed, [closed, open) line ranges in the given source file to character ranges.
 */
public static RangeSet<Integer> lineRangesToCharRanges(
        String input, RangeSet<Integer> lineRanges) {
    List<Integer> lines = new ArrayList<>();
    Iterators.addAll(lines, Newlines.lineOffsetIterator(input));
    lines.add(input.length() + 1);

    final RangeSet<Integer> characterRanges = TreeRangeSet.create();
    for (Range<Integer> lineRange :
            lineRanges.subRangeSet(Range.closedOpen(0, lines.size() - 1)).asRanges()) {
        int lineStart = lines.get(lineRange.lowerEndpoint());
        // Exclude the trailing newline. This isn't strictly necessary, but handling blank lines
        // as empty ranges is convenient.
        int lineEnd = lines.get(lineRange.upperEndpoint()) - 1;
        Range<Integer> range = Range.closedOpen(lineStart, lineEnd);
        characterRanges.add(range);
    }
    return characterRanges;
}
 
Example 3
Source File: N4JSMemberRedefinitionValidator.java    From n4js with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Constraints 41 (Abstract Class)
 */
private boolean constraints_41_AbstractClass(TClassifier classifier, MemberCube memberCube) {
	List<TMember> abstractMembers = null;
	if (!classifier.isAbstract() && classifier instanceof TClass) {
		for (Entry<NameStaticPair, MemberMatrix> entry : memberCube.entrySet()) {
			MemberMatrix mm = entry.getValue();
			MemberList<TMember> l = new MemberList<>();
			Iterators.addAll(l, mm.actuallyInheritedAndMixedMembers());
			for (SourceAwareIterator iter = mm.actuallyInheritedAndMixedMembers(); iter.hasNext();) {
				TMember m = iter.next();
				if (m.isAbstract()) {
					if (abstractMembers == null) {
						abstractMembers = new ArrayList<>();
					}
					abstractMembers.add(m);
				}
			}
		}
	}
	if (abstractMembers != null) {
		messageMissingImplementations(abstractMembers);
		return false;
	}
	return true;
}
 
Example 4
Source File: BatchInserterTests.java    From azure-documentdb-java with MIT License 6 votes vote down vote up
@Test
public void callbackCount() {
    BulkImportStoredProcedureOptions options = null;
    String bulkImportSproc = null;
    DocumentClient client = Mockito.mock(DocumentClient.class);
    List<List<String>> batchesToInsert = new ArrayList<>();
    batchesToInsert.add(new ArrayList<>());
    batchesToInsert.add(new ArrayList<>());
    batchesToInsert.add(new ArrayList<>());

    String partitionIndex = "0";
    BatchInserter bi = new BatchInserter(partitionIndex, batchesToInsert, client, bulkImportSproc, options);

    Iterator<Callable<InsertMetrics>> callbackIterator = bi.miniBatchInsertExecutionCallableIterator();

    List<Callable<InsertMetrics>> list = new ArrayList<>();
    Iterators.addAll(list, callbackIterator);

    assertThat(list.size(), equalTo(3));
}
 
Example 5
Source File: DefaultDedupEventStore.java    From emodb with Apache License 2.0 6 votes vote down vote up
@Override
public Iterator<String> listChannels() {
    // The implementation of this is unfortunate in that, because the underlying methods return channel
    // and queue names in random order, the only way to dedup names between the 3 underlying data structures
    // is to read all the channels into memory.  Hopefully there aren't so many that this causes problems,
    // but this is why the list channels method shouldn't be exposed as a public supported API, just an
    // internal feature that can be re-implemented or reconceived if necessary.
    Set<String> queues = Sets.newHashSet();

    // Enumerate the persistent sorted queues
    Iterators.addAll(queues, _queueDAO.listQueues());

    // Enumerate the read & write channels.
    Iterator<String> channelIter = _delegate.listChannels();
    while (channelIter.hasNext()) {
        String channel = channelIter.next();
        String queue;
        if ((queue = _channels.queueFromReadChannel(channel)) != null) {
            queues.add(queue);
        } else if ((queue = _channels.queueFromWriteChannel(channel)) != null) {
            queues.add(queue);
        }
    }

    return queues.iterator();  // Unordered to be consistent with non-dedup'd event store
}
 
Example 6
Source File: Formatter.java    From google-java-format with Apache License 2.0 6 votes vote down vote up
/**
 * Converts zero-indexed, [closed, open) line ranges in the given source file to character ranges.
 */
public static RangeSet<Integer> lineRangesToCharRanges(
    String input, RangeSet<Integer> lineRanges) {
  List<Integer> lines = new ArrayList<>();
  Iterators.addAll(lines, Newlines.lineOffsetIterator(input));
  lines.add(input.length() + 1);

  final RangeSet<Integer> characterRanges = TreeRangeSet.create();
  for (Range<Integer> lineRange :
      lineRanges.subRangeSet(Range.closedOpen(0, lines.size() - 1)).asRanges()) {
    int lineStart = lines.get(lineRange.lowerEndpoint());
    // Exclude the trailing newline. This isn't strictly necessary, but handling blank lines
    // as empty ranges is convenient.
    int lineEnd = lines.get(lineRange.upperEndpoint()) - 1;
    Range<Integer> range = Range.closedOpen(lineStart, lineEnd);
    characterRanges.add(range);
  }
  return characterRanges;
}
 
Example 7
Source File: CleanupContextTest.java    From dcos-cassandra-service with Apache License 2.0 6 votes vote down vote up
@Test
public void testJSONSerializationWithSnakeCaseMembers() throws Exception {
    CleanupContext context = new CleanupContext(
            Arrays.asList("node1"), Arrays.asList("keyspace1"), Arrays.asList("column_family1"));
    ObjectMapper om = new ObjectMapper();

    JsonSerializer serializer = new JsonSerializer();
    String jsonContext = new String(serializer.serialize(context), StandardCharsets.UTF_8);

    JsonNode rehydratedContext = om.readTree(jsonContext);
    List<String> keys = new ArrayList<>();
    Iterators.addAll(keys, rehydratedContext.getFieldNames());
    keys.sort(String::compareTo);

    Assert.assertEquals(Arrays.asList("column_families", "key_spaces", "nodes"), keys);

    context = serializer.deserialize(jsonContext.getBytes(StandardCharsets.UTF_8), CleanupContext.class);
    Assert.assertEquals(Arrays.asList("column_family1"), context.getColumnFamilies());
    Assert.assertEquals(Arrays.asList("keyspace1"), context.getKeySpaces());
    Assert.assertEquals(Arrays.asList("node1"), context.getNodes());
}
 
Example 8
Source File: PackratParserGenUtil.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
private static List<String> getConflictingKeywordsImpl(final Grammar grammar, TerminalRule rule) {
	final Iterator<Keyword> conflictingKeywords = getConflictingKeywords(rule,
			Iterators.filter(EcoreUtil.getAllContents(grammar, true), Keyword.class));
	Set<String> res = Sets.newLinkedHashSet();
	Iterators.addAll(res, Iterators.transform(conflictingKeywords, new Function<Keyword, String>() {
		@Override
		public String apply(Keyword param) {
			return param.getValue();
		}
	}));
	return Lists.newArrayList(res);
}
 
Example 9
Source File: TokenMetadataTest.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
private void testRingIterator(ArrayList<Token> ring, String start, boolean includeMin, String... expected)
{
    ArrayList<Token> actual = new ArrayList<Token>();
    Iterators.addAll(actual, TokenMetadata.ringIterator(ring, token(start), includeMin));
    assertEquals(actual.toString(), expected.length, actual.size());
    for (int i = 0; i < expected.length; i++)
        assertEquals("Mismatch at index " + i + ": " + actual, token(expected[i]), actual.get(i));
}
 
Example 10
Source File: FlattenSamplerTest.java    From log-synth with Apache License 2.0 5 votes vote down vote up
@Test
public void testEmptyPrefix() throws IOException {
    SchemaSampler s = new SchemaSampler(Resources.asCharSource(Resources.getResource("schema023.json"), Charsets.UTF_8).read());
    JsonNode v = s.sample();
    Set<String> names = Sets.newTreeSet();
    Iterators.addAll(names, v.fieldNames());
    assertEquals("[city, decommisioned, estimatedPopulation, latitude, location, locationType, longitude, state, " +
            "taxReturnsFiled, totalWages, zip, zipType]", names.toString());
}
 
Example 11
Source File: WidthLimitingDecoratorTest.java    From picard with MIT License 5 votes vote down vote up
@Test
public void testForVcf() throws Exception {

    final Segment entireThing = new Segment(1, 9942);
    final ImmutableList<Segment> expectedSubThings = ImmutableList.of(
            new Segment(1, 1000),
            new Segment(1001, 2000),
            new Segment(2001, 3000),
            new Segment(3001, 4000),
            new Segment(4001, 5000),
            new Segment(5001, 6000),
            new Segment(6001, 7000),
            new Segment(7001, 8000),
            new Segment(8001, 9000),
            new Segment(9001, 9942)
    );

    final VcfFileSegmentGenerator.WidthLimitingDecorator strategy = VcfFileSegmentGenerator.WidthLimitingDecorator.wrapping(new VcfFileSegmentGenerator() {
        @Override
        public Iterable<VcfFileSegment> forVcf(final File vcf) {
            return Collections.singleton((VcfFileSegment) entireThing);
        }
    }, 1000);

    final List<VcfFileSegment> observed = new ArrayList<VcfFileSegment>();
    Iterators.addAll(observed, strategy.forVcf(new File("B")).iterator());
    final Iterator<VcfFileSegment> observedIterator = observed.iterator();
    for (final VcfFileSegment e : expectedSubThings) {
        Assert.assertTrue(observedIterator.hasNext());
        final VcfFileSegment o = observedIterator.next();
        Assert.assertEquals(ComparisonChain.start()
                .compare(o.contig(), e.contig())
                .compare(o.start(), e.start())
                .compare(o.stop(), e.stop())
                .compare(o.vcf(), e.vcf())
                .result(), 0, String.format(String.format("observed=%s@%s:%s-%s  expected=%s", o.vcf(), o.contig(), o.start(), 
                o.stop(), e.toString())));
    }
}
 
Example 12
Source File: EntryArrayList.java    From titan1withtp3.1 with Apache License 2.0 5 votes vote down vote up
public static EntryArrayList of(Iterable<? extends Entry> i) {
    // This is adapted from Guava's Lists.newArrayList implementation
    EntryArrayList result;
    if (i instanceof Collection) {
        // Let ArrayList's sizing logic work, if possible
        result = new EntryArrayList((Collection)i);
    } else {
        // Unknown size
        result = new EntryArrayList();
        Iterators.addAll(result, i.iterator());
    }
    return result;
}
 
Example 13
Source File: TestVCFRoundTrip.java    From Hadoop-BAM with MIT License 4 votes vote down vote up
@Test
public void testRoundTrip() throws Exception {
    Path vcfPath = new Path("file://" + testVCFFileName);

    // run a MR job to write out a VCF file
    Path outputPath = doMapReduce(vcfPath, true);

    // verify the output is the same as the input
    List<VariantContext> expectedVariants = new ArrayList<>();
    VCFFileReader vcfFileReader = parseVcf(new File(testVCFFileName));
    Iterators.addAll(expectedVariants, vcfFileReader.iterator());

    int splits = 0;
    List<VariantContext> actualVariants = new ArrayList<>();
    File[] vcfFiles = new File(outputPath.toUri()).listFiles(
        pathname -> (!pathname.getName().startsWith(".") &&
            !pathname.getName().startsWith("_")));
    Arrays.sort(vcfFiles); // ensure files are sorted by name
    for (File vcf : vcfFiles) {
        splits++;
        Iterators.addAll(actualVariants, parseVcf(vcf).iterator());
        if (BGZFCodec.class.equals(codecClass)) {
            assertTrue(BlockCompressedInputStream.isValidFile(
                new BufferedInputStream(new FileInputStream(vcf))));
        } else if (BGZFEnhancedGzipCodec.class.equals(codecClass)) {
            assertTrue(VCFFormat.isGzip(
                new BufferedInputStream(new FileInputStream(vcf))));
        }
    }

    switch (expectedSplits) {
        case EXACTLY_ONE:
            assertEquals("Should be exactly one split", 1, splits);
            break;
        case MORE_THAN_ONE:
            assertTrue("Should be more than one split", splits > 1);
            break;
        case ANY:
        default:
            break;
    }

    // use a VariantContextComparator to check variants are equal
    VCFHeader vcfHeader = VCFHeaderReader.readHeaderFrom(new SeekableFileStream(new
        File(testVCFFileName)));
    VariantContextComparator vcfRecordComparator = vcfHeader.getVCFRecordComparator();
    assertEquals(expectedVariants.size(), actualVariants.size());
    for (int i = 0; i < expectedVariants.size(); i++) {
        assertEquals(0, vcfRecordComparator.compare(expectedVariants.get(i),
            actualVariants.get(i)));
    }
}
 
Example 14
Source File: OptionList.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
public OptionList getNonSystemOptions() {
  final OptionList options = new OptionList();
  Iterators.addAll(options, Iterators.filter(iterator(), Predicates.not(IS_SYSTEM_OPTION)));
  return options;
}
 
Example 15
Source File: LocalCache.java    From codebuff with BSD 2-Clause "Simplified" License 4 votes vote down vote up
private static <E> ArrayList<E> toArrayList(Collection<E> c) {
  // Avoid calling ArrayList(Collection), which may call back into toArray.
  ArrayList<E> result = new ArrayList<E>(c.size());
  Iterators.addAll(result, c.iterator());
  return result;
}
 
Example 16
Source File: Selection.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
public static Selection wildcard(CFMetaData cfm)
{
    List<ColumnDefinition> all = new ArrayList<ColumnDefinition>(cfm.allColumns().size());
    Iterators.addAll(all, cfm.allColumnsInSelectOrder());
    return new SimpleSelection(all, true);
}
 
Example 17
Source File: OptionsParserImpl.java    From bazel with Apache License 2.0 4 votes vote down vote up
/**
 * Parses the args, and returns what it doesn't parse. May be called multiple times, and may be
 * called recursively. Calls may contain intersecting sets of options; in that case, the arg seen
 * last takes precedence.
 *
 * <p>The method treats options that have neither an implicitDependent nor an expandedFrom value
 * as explicitly set.
 */
private ResidueAndPriority parse(
    OptionPriority priority,
    Function<OptionDefinition, String> sourceFunction,
    ParsedOptionDescription implicitDependent,
    ParsedOptionDescription expandedFrom,
    List<String> args)
    throws OptionsParsingException {
  List<String> unparsedArgs = new ArrayList<>();
  List<String> unparsedPostDoubleDashArgs = new ArrayList<>();

  Iterator<String> argsIterator = argsPreProcessor.preProcess(args).iterator();
  while (argsIterator.hasNext()) {
    String arg = argsIterator.next();

    if (!arg.startsWith("-")) {
      unparsedArgs.add(arg);
      continue; // not an option arg
    }

    if (skippedPrefixes.stream().anyMatch(prefix -> arg.startsWith(prefix))) {
      unparsedArgs.add(arg);
      continue;
    }

    if (arg.equals("--")) { // "--" means all remaining args aren't options
      Iterators.addAll(unparsedPostDoubleDashArgs, argsIterator);
      break;
    }

    ParsedOptionDescription parsedOption =
        identifyOptionAndPossibleArgument(
            arg, argsIterator, priority, sourceFunction, implicitDependent, expandedFrom);
    handleNewParsedOption(parsedOption);
    priority = OptionPriority.nextOptionPriority(priority);
  }

  // Go through the final values and make sure they are valid values for their option. Unlike any
  // checks that happened above, this also checks that flags that were not set have a valid
  // default value. getValue() will throw if the value is invalid.
  for (OptionValueDescription valueDescription : asListOfEffectiveOptions()) {
    valueDescription.getValue();
  }

  return new ResidueAndPriority(unparsedArgs, unparsedPostDoubleDashArgs, priority);
}
 
Example 18
Source File: LocalCache.java    From codebuff with BSD 2-Clause "Simplified" License 4 votes vote down vote up
private static <E> ArrayList<E> toArrayList(Collection<E> c) {
  // Avoid calling ArrayList(Collection), which may call back into toArray.
  ArrayList<E> result = new ArrayList<E>(c.size());
  Iterators.addAll(result, c.iterator());
  return result;
}
 
Example 19
Source File: TestHashJoinOperator.java    From presto with Apache License 2.0 4 votes vote down vote up
@Override
public SingleStreamSpiller create(List<Type> types, SpillContext spillContext, LocalMemoryContext memoryContext)
{
    return new SingleStreamSpiller()
    {
        private boolean writing = true;
        private final List<Page> spills = new ArrayList<>();

        @Override
        public ListenableFuture<?> spill(Iterator<Page> pageIterator)
        {
            checkState(writing, "writing already finished");
            if (failSpill) {
                return immediateFailedFuture(new PrestoException(GENERIC_INTERNAL_ERROR, "Spill failed"));
            }
            Iterators.addAll(spills, pageIterator);
            return immediateFuture(null);
        }

        @Override
        public Iterator<Page> getSpilledPages()
        {
            if (failUnspill) {
                throw new PrestoException(GENERIC_INTERNAL_ERROR, "Unspill failed");
            }
            writing = false;
            return unmodifiableIterator(spills.iterator());
        }

        @Override
        public long getSpilledPagesInMemorySize()
        {
            return spills.stream()
                    .mapToLong(Page::getSizeInBytes)
                    .sum();
        }

        @Override
        public ListenableFuture<List<Page>> getAllSpilledPages()
        {
            if (failUnspill) {
                return immediateFailedFuture(new PrestoException(GENERIC_INTERNAL_ERROR, "Unspill failed"));
            }
            writing = false;
            return immediateFuture(ImmutableList.copyOf(spills));
        }

        @Override
        public void close()
        {
            writing = false;
        }
    };
}
 
Example 20
Source File: ImmutableNullableList.java    From Bats with Apache License 2.0 2 votes vote down vote up
/**
 * Adds each element of {@code elements} to the
 * {@code ImmutableNullableList}.
 *
 * @param elements the elements to add to the {@code ImmutableNullableList}
 * @return this {@code Builder} object
 * @throws NullPointerException if {@code elements} is null
 */
public Builder<E> addAll(Iterator<? extends E> elements) {
  Iterators.addAll(contents, elements);
  return this;
}