private void executeParallelIterate(int level, ExecutorService executorService) {
   MutableList<Integer> items = Lists.mutable.of();
   for (int i = 0; i < 20000; i++) {
     items.add(i % 1000 == 0 ? level : 0);
   }
   ParallelIterate.forEach(items, new RecursiveProcedure(), executorService);
 }
Ejemplo n.º 2
0
  @Test
  public void keySet_retainAll() {
    // a map with a null key
    MutableMap<Integer, Integer> map = this.newMapWithKeyValue(null, 0);

    MutableList<Object> retained = Lists.mutable.of();
    retained.add(null);
    Assert.assertFalse(map.keySet().retainAll(retained));
    Verify.assertContains(null, map.keySet());

    // a map with a chain containing empty slots
    MutableMap<Integer, Integer> map2 = this.mapWithCollisionsOfSize(5);
    Assert.assertFalse(map2.keySet().retainAll(FastList.<Integer>newListWith(0, 17, 34, 51, 68)));
    Verify.assertContainsAll(map2.keySet(), 0, 17, 34, 51, 68);

    // a map with no chaining, nothing retained
    MutableMap<Integer, String> map3 = this.newMapWithKeyValue(1, "One");
    Assert.assertTrue(map3.keySet().retainAll(FastList.<Integer>newListWith(9)));
    Verify.assertEmpty(map3);

    Set<Integer> keys =
        this.newMapWithKeysValues(1, "One", 2, "Two", 3, "Three", 4, "Four").keySet();
    Assert.assertTrue(keys.retainAll(FastList.<Integer>newListWith(1, 2, 3)));
    Verify.assertContainsAll(keys, 1, 2, 3);
  }
  private static void runUnifiedSetRetainAllFromList(int shift) {
    MultiReaderUnifiedSet<CollidingInt> set = MultiReaderUnifiedSet.newSet();

    MutableList<CollidingInt> toRetain = Lists.mutable.of();

    int size = 100000;
    for (int i = 0; i < size; i++) {
      set.add(new CollidingInt(i, shift));
      if (i % 2 == 0) {
        toRetain.add(new CollidingInt(i, shift));
      }
    }
    Verify.assertSize(size, set);
    Assert.assertTrue(set.containsAll(toRetain));

    Assert.assertTrue(set.retainAll(toRetain));
    Assert.assertTrue(set.containsAll(toRetain));

    Assert.assertFalse(set.retainAll(toRetain)); // a second call should not modify the set

    Verify.assertSize(size / 2, set);

    for (int i = 0; i < size; i += 2) {
      Verify.assertContains(new CollidingInt(i, shift), set);
    }
  }
  @Test
  @Override
  public void subList() {
    // Not serializable

    MutableList<String> list = this.newWith("A", "B", "C", "D");
    MutableList<String> sublist = list.subList(1, 3);
    Verify.assertSize(2, sublist);
    Verify.assertContainsAll(sublist, "B", "C");
    sublist.add("X");
    Verify.assertSize(3, sublist);
    Verify.assertContainsAll(sublist, "B", "C", "X");
    Verify.assertSize(5, list);
    Verify.assertContainsAll(list, "A", "B", "C", "X", "D");
    sublist.remove("X");
    Verify.assertContainsAll(sublist, "B", "C");
    Verify.assertContainsAll(list, "A", "B", "C", "D");
    Assert.assertEquals("C", sublist.set(1, "R"));
    Verify.assertContainsAll(sublist, "B", "R");
    Verify.assertContainsAll(list, "A", "B", "R", "D");
    sublist.addAll(Arrays.asList("W", "G"));
    Verify.assertContainsAll(sublist, "B", "R", "W", "G");
    Verify.assertContainsAll(list, "A", "B", "R", "W", "G", "D");
    sublist.clear();
    Verify.assertEmpty(sublist);
    Assert.assertFalse(sublist.remove("X"));
    Verify.assertEmpty(sublist);
    Verify.assertContainsAll(list, "A", "D");
  }
 @Test
 public void testAddWithIndex() {
   MutableList<String> list = new CompositeFastList<String>();
   list.addAll(FastList.newListWith("1", "2", "3", "4"));
   list.addAll(FastList.newListWith("A", "B", "C", "B"));
   list.add(3, "NEW");
   Verify.assertSize(9, list);
   Assert.assertEquals("NEW", list.get(3));
   Assert.assertEquals("4", list.get(4));
   list.add(0, "START");
   Verify.assertSize(10, list);
   Assert.assertEquals("START", list.getFirst());
   list.add(10, "END");
   Verify.assertSize(11, list);
   Assert.assertEquals("END", list.getLast());
 }
 @Test
 public void testDefaultConstructor() {
   MutableList<String> list = new CompositeFastList<String>();
   list.add("1");
   list.add("2");
   Verify.assertSize(2, list);
   Verify.assertContains("1", list);
 }
Ejemplo n.º 7
0
 public <V> MutableList<V> collect(BooleanToObjectFunction<? extends V> function) {
   MutableList<V> results = FastList.newList(BooleanArrayList.this.size);
   BooleanIterator iterator = this.booleanIterator();
   while (iterator.hasNext()) {
     results.add(function.valueOf(iterator.next()));
   }
   return results;
 }
 protected static MutableList<String> generateCollisions() {
   MutableList<String> collisions = FastList.newList();
   ObjectBooleanHashMap<String> hashMap = new ObjectBooleanHashMap<String>();
   for (int each = 3; collisions.size() <= 10; each++) {
     if (hashMap.index(String.valueOf(each)) == hashMap.index(String.valueOf(3))) {
       collisions.add(String.valueOf(each));
     }
   }
   return collisions;
 }
  @Test
  public void flatten_value() {
    MutableMap<String, String> map = this.newMapWithKeysValues("1", "One", "2", "Two");

    Function<String, Iterable<Character>> function =
        object -> {
          MutableList<Character> result = Lists.mutable.of();
          if (object != null) {
            char[] chars = object.toCharArray();
            for (char aChar : chars) {
              result.add(Character.valueOf(aChar));
            }
          }
          return result;
        };

    RichIterable<Character> blob = map.flatCollect(function);
    RichIterable<Character> blobFromTarget =
        map.flatCollect(function, FastList.<Character>newList());

    switch (map.size()) {
      case 1:
        Assert.assertTrue(
            blob.containsAllArguments(
                Character.valueOf('O'), Character.valueOf('n'), Character.valueOf('e')));
        Assert.assertTrue(
            blobFromTarget.containsAllArguments(
                Character.valueOf('O'), Character.valueOf('n'), Character.valueOf('e')));
        break;
      case 2:
      case 3:
        Assert.assertTrue(
            blob.containsAllArguments(
                Character.valueOf('O'),
                Character.valueOf('n'),
                Character.valueOf('e'),
                Character.valueOf('T'),
                Character.valueOf('w'),
                Character.valueOf('o')));
        Assert.assertTrue(
            blobFromTarget.containsAllArguments(
                Character.valueOf('O'),
                Character.valueOf('n'),
                Character.valueOf('e'),
                Character.valueOf('T'),
                Character.valueOf('w'),
                Character.valueOf('o')));
        break;
      default:
        Assert.assertEquals(0, blob.size());
        Assert.assertEquals(0, blobFromTarget.size());
        break;
    }
  }
 @Test
 public void forEachWithIndex() {
   int[] indexSum = new int[1];
   MutableList<String> result = Lists.mutable.of();
   MutableSet<String> source = Sets.fixedSize.of("1", "2", "3", "4");
   source.forEachWithIndex(
       (each, index) -> {
         result.add(each);
         indexSum[0] += index;
       });
   Assert.assertEquals(FastList.newListWith("1", "2", "3", "4"), result);
   Assert.assertEquals(6, indexSum[0]);
 }
 @Override
 @Test
 public void forEachWith() {
   super.forEachWith();
   MutableList<Integer> list = FastList.newList();
   CompositeFastList<Integer> iterables = new CompositeFastList<Integer>();
   iterables.addComposited(Interval.fromTo(6, 10).toList());
   iterables.addComposited(Interval.oneTo(5).toList());
   iterables.forEachWith((each, parameter) -> list.add(parameter.intValue(), each), 0);
   Verify.assertSize(10, list);
   Verify.assertAllSatisfy(list, Predicates.greaterThan(0).and(Predicates.lessThan(11)));
   Verify.assertStartsWith(list, 5, 4, 3, 2, 1, 10, 9, 8, 7, 6);
 }
 @Override
 @Test
 public void forEachWithIndex() {
   super.forEachWithIndex();
   MutableList<Integer> list = FastList.newList();
   CompositeFastList<Integer> iterables = new CompositeFastList<Integer>();
   iterables.addComposited(Interval.fromTo(6, 10).toList());
   iterables.addComposited(Interval.oneTo(5).toList());
   iterables.forEachWithIndex((each, index) -> list.add(index, each));
   Verify.assertSize(10, list);
   Verify.assertAllSatisfy(list, Predicates.greaterThan(0).and(Predicates.lessThan(11)));
   Verify.assertStartsWith(list, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5);
 }
  private static void assertUnifiedSetForEachWith(int shift) {
    MultiReaderUnifiedSet<CollidingInt> set = MultiReaderUnifiedSet.newSet();

    int size = 100000;
    for (int i = 0; i < size; i++) {
      Assert.assertTrue(set.add(new CollidingInt(i, shift)));
    }
    MutableList<CollidingInt> keys = FastList.newList(size);
    set.forEachWith(
        (key, s) -> {
          Assert.assertEquals("foo", s);
          keys.add(key);
        },
        "foo");
    Verify.assertSize(size, keys);
    Collections.sort(keys);

    for (int i = 0; i < size; i++) {
      Verify.assertItemAtIndex(new CollidingInt(i, shift), i, keys);
    }
  }
 @Override
 @Test
 public void subList() {
   MutableList<String> list = this.newWith("A", "B", "C", "D");
   MutableList<String> sublist = list.subList(1, 3);
   Verify.assertPostSerializedEqualsAndHashCode(sublist);
   Verify.assertSize(2, sublist);
   Verify.assertContainsAll(sublist, "B", "C");
   sublist.add("X");
   Verify.assertSize(3, sublist);
   Verify.assertContainsAll(sublist, "B", "C", "X");
   Verify.assertSize(5, list);
   Verify.assertContainsAll(list, "A", "B", "C", "X", "D");
   sublist.remove("X");
   Verify.assertContainsAll(sublist, "B", "C");
   Verify.assertContainsAll(list, "A", "B", "C", "D");
   Assert.assertEquals("C", sublist.set(1, "R"));
   Verify.assertContainsAll(sublist, "B", "R");
   Verify.assertContainsAll(list, "A", "B", "R", "D");
   sublist.clear();
   Verify.assertEmpty(sublist);
   Verify.assertContainsAll(list, "A", "D");
 }