Java Collections.nCopies Examples

Java Collections.nCopies - 30 examples found. These are the top rated real world Java examples of java.util.Collections.nCopies extracted from open source projects. You can rate examples to help us improve the quality of examples.
  @Test
  public void zip() {
    List<Object> nulls = Collections.nCopies(this.lazyIterable.size(), null);
    List<Object> nullsPlusOne = Collections.nCopies(this.lazyIterable.size() + 1, null);
    List<Object> nullsMinusOne = Collections.nCopies(this.lazyIterable.size() - 1, null);

    LazyIterable<Pair<Integer, Object>> pairs = this.lazyIterable.zip(nulls);
    Assert.assertEquals(
        this.lazyIterable.toSet(), pairs.collect(Functions.<Integer>firstOfPair()).toSet());
    Assert.assertEquals(nulls, pairs.collect(Functions.<Object>secondOfPair(), Lists.mutable.of()));

    LazyIterable<Pair<Integer, Object>> pairsPlusOne = this.lazyIterable.zip(nullsPlusOne);
    Assert.assertEquals(
        this.lazyIterable.toSet(), pairsPlusOne.collect(Functions.<Integer>firstOfPair()).toSet());
    Assert.assertEquals(
        nulls, pairsPlusOne.collect(Functions.<Object>secondOfPair(), Lists.mutable.of()));

    LazyIterable<Pair<Integer, Object>> pairsMinusOne = this.lazyIterable.zip(nullsMinusOne);
    Assert.assertEquals(this.lazyIterable.size() - 1, pairsMinusOne.size());
    Assert.assertTrue(
        this.lazyIterable.containsAllIterable(
            pairsMinusOne.collect(Functions.<Integer>firstOfPair())));

    Assert.assertEquals(
        this.lazyIterable.zip(nulls).toSet(),
        this.lazyIterable.zip(nulls, UnifiedSet.<Pair<Integer, Object>>newSet()));
  }
  @Test
  public void zip() {
    ImmutableBag<String> immutableBag = this.newBag();
    List<Object> nulls = Collections.nCopies(immutableBag.size(), null);
    List<Object> nullsPlusOne = Collections.nCopies(immutableBag.size() + 1, null);
    List<Object> nullsMinusOne = Collections.nCopies(immutableBag.size() - 1, null);

    ImmutableBag<Pair<String, Object>> pairs = immutableBag.zip(nulls);
    Assert.assertEquals(
        immutableBag, pairs.collect((Function<Pair<String, ?>, String>) Pair::getOne));
    Assert.assertEquals(
        HashBag.newBag(nulls), pairs.collect((Function<Pair<?, Object>, Object>) Pair::getTwo));

    ImmutableBag<Pair<String, Object>> pairsPlusOne = immutableBag.zip(nullsPlusOne);
    Assert.assertEquals(
        immutableBag, pairsPlusOne.collect((Function<Pair<String, ?>, String>) Pair::getOne));
    Assert.assertEquals(
        HashBag.newBag(nulls),
        pairsPlusOne.collect((Function<Pair<?, Object>, Object>) Pair::getTwo));

    ImmutableBag<Pair<String, Object>> pairsMinusOne = immutableBag.zip(nullsMinusOne);
    Assert.assertEquals(immutableBag.size() - 1, pairsMinusOne.size());
    Assert.assertTrue(
        immutableBag.containsAllIterable(
            pairsMinusOne.collect((Function<Pair<String, ?>, String>) Pair::getOne)));

    Assert.assertEquals(
        immutableBag.zip(nulls), immutableBag.zip(nulls, HashBag.<Pair<String, Object>>newBag()));
  }
  @Test
  public void zip() {
    ImmutableMap<String, String> map =
        this.newMapWithKeysValues("1", "One", "2", "Two", "3", "Three", "4", "Four");

    List<Object> nulls = Collections.nCopies(map.size(), null);
    List<Object> nullsPlusOne = Collections.nCopies(map.size() + 1, null);

    RichIterable<Pair<String, Object>> pairs = map.zip(nulls);
    Assert.assertEquals(
        map.toSet(), pairs.collect((Function<Pair<String, ?>, String>) Pair::getOne).toSet());
    Assert.assertEquals(
        nulls, pairs.collect((Function<Pair<?, Object>, Object>) Pair::getTwo, Lists.mutable.of()));

    RichIterable<Pair<String, Object>> pairsPlusOne = map.zip(nullsPlusOne);
    Assert.assertEquals(
        map.toSet(),
        pairsPlusOne.collect((Function<Pair<String, ?>, String>) Pair::getOne).toSet());
    Assert.assertEquals(
        nulls,
        pairsPlusOne.collect((Function<Pair<?, Object>, Object>) Pair::getTwo, Lists.mutable.of()));

    if (map.notEmpty()) {
      List<Object> nullsMinusOne = Collections.nCopies(map.size() - 1, null);
      RichIterable<Pair<String, Object>> pairsMinusOne = map.zip(nullsMinusOne);
      Assert.assertEquals(map.size() - 1, pairsMinusOne.size());
    }

    Assert.assertEquals(
        map.zip(nulls).toSet(), map.zip(nulls, UnifiedSet.<Pair<String, Object>>newSet()));
  }
  @Test
  public void zip() {
    MutableMap<String, String> map = this.classUnderTest();

    List<Object> nulls = Collections.nCopies(map.size(), null);
    List<Object> nullsPlusOne = Collections.nCopies(map.size() + 1, null);

    RichIterable<Pair<String, Object>> pairs = map.zip(nulls);
    Assert.assertEquals(
        map.toSet(), pairs.collect((Function<Pair<String, ?>, String>) Pair::getOne).toSet());
    Assert.assertEquals(
        nulls, pairs.collect((Function<Pair<?, Object>, Object>) Pair::getTwo, Lists.mutable.of()));

    RichIterable<Pair<String, Object>> pairsPlusOne = map.zip(nullsPlusOne);
    Assert.assertEquals(
        map.toSet(),
        pairsPlusOne.collect((Function<Pair<String, ?>, String>) Pair::getOne).toSet());
    Assert.assertEquals(
        nulls,
        pairsPlusOne.collect((Function<Pair<?, Object>, Object>) Pair::getTwo, Lists.mutable.of()));

    if (map.notEmpty()) {
      List<Object> nullsMinusOne = Collections.nCopies(map.size() - 1, null);
      RichIterable<Pair<String, Object>> pairsMinusOne = map.zip(nullsMinusOne);
      Assert.assertEquals(map.size() - 1, pairsMinusOne.size());
      Assert.assertTrue(
          map.values()
              .containsAll(
                  pairsMinusOne.collect((Function<Pair<String, ?>, String>) Pair::getOne).toSet()));
    }

    Assert.assertEquals(
        map.zip(nulls).toSet(), map.zip(nulls, UnifiedSet.<Pair<String, Object>>newSet()));
  }
  @NotNull
  public static List<Color> generateLinearColorSequence(
      @NotNull List<Color> anchorColors, int count) {
    if (count == 0) return Collections.emptyList();
    if (anchorColors.isEmpty()) return Collections.nCopies(count, JBColor.GRAY);
    if (anchorColors.size() == 1) return Collections.nCopies(count, anchorColors.get(0));

    List<Color> result = new ArrayList<>();
    result.add(anchorColors.get(0));

    int segmentCount = anchorColors.size() - 1;
    int colorPerSegment = (count - 1) / segmentCount;
    if (count - 1 > colorPerSegment * segmentCount) colorPerSegment++; // divide rounding up

    for (int i = 0; i < segmentCount; i++) {
      Color color1 = anchorColors.get(i);
      Color color2 = anchorColors.get(i + 1);

      List<Color> linearColors = generateLinearColorSequence(color1, color2, colorPerSegment + 1);

      // skip first element from sequence to avoid duplication from connected segments
      result.addAll(linearColors.subList(1, linearColors.size()));
    }
    return result.subList(0, count);
  }
    @SuppressWarnings({"rawtypes", "unchecked"})
    public AnnotationDifferDocumentFeaturesImpl(int correct, int missing, int spurious) {
      super();

      this.spurious = spurious;
      this.missing = missing;
      this.correctMatches = correct;

      keyList = new ArrayList(Collections.nCopies(getKeysCount(), null));
      responseList = new ArrayList(Collections.nCopies(getResponsesCount(), null));
    }