Search in sources :

Example 6 with MutableSet

use of org.eclipse.collections.api.set.MutableSet in project eclipse-collections by eclipse.

the class AbstractMutableSetTestCase method removeIf.

@Override
@Test
public void removeIf() {
    super.removeIf();
    MutableSet<IntegerWithCast> set = this.newWith();
    MutableList<IntegerWithCast> collisions = COLLISIONS.collect(IntegerWithCast::new);
    set.addAll(collisions);
    collisions.reverseForEach(each -> {
        Assert.assertFalse(set.remove(null));
        Assert.assertTrue(set.remove(each));
        Assert.assertFalse(set.remove(each));
        Assert.assertFalse(set.remove(null));
        Assert.assertFalse(set.remove(new IntegerWithCast(COLLISION_10)));
    });
    Assert.assertEquals(UnifiedSet.<IntegerWithCast>newSet(), set);
    collisions.forEach(Procedures.cast(each -> {
        MutableSet<IntegerWithCast> set2 = this.newWith();
        set2.addAll(collisions);
        Assert.assertFalse(set2.remove(null));
        Assert.assertTrue(set2.remove(each));
        Assert.assertFalse(set2.remove(each));
        Assert.assertFalse(set2.remove(null));
        Assert.assertFalse(set2.remove(new IntegerWithCast(COLLISION_10)));
    }));
    // remove the second-to-last item in a fully populated single chain to cause the last item to move
    MutableSet<Integer> set3 = this.newWith(COLLISION_1, COLLISION_2, COLLISION_3, COLLISION_4);
    Assert.assertTrue(set3.remove(COLLISION_3));
    Assert.assertEquals(UnifiedSet.newSetWith(COLLISION_1, COLLISION_2, COLLISION_4), set3);
    Assert.assertTrue(set3.remove(COLLISION_2));
    Assert.assertEquals(UnifiedSet.newSetWith(COLLISION_1, COLLISION_4), set3);
    // search a chain for a non-existent element
    MutableSet<Integer> chain = this.newWith(COLLISION_1, COLLISION_2, COLLISION_3, COLLISION_4);
    Assert.assertFalse(chain.remove(COLLISION_5));
    // search a deep chain for a non-existent element
    MutableSet<Integer> deepChain = this.newWith(COLLISION_1, COLLISION_2, COLLISION_3, COLLISION_4, COLLISION_5, COLLISION_6, COLLISION_7);
    Assert.assertFalse(deepChain.remove(COLLISION_8));
    // search for a non-existent element
    MutableSet<Integer> empty = this.newWith();
    Assert.assertFalse(empty.remove(COLLISION_1));
}
Also used : Arrays(java.util.Arrays) ArrayAdapter(org.eclipse.collections.impl.list.fixed.ArrayAdapter) Function(org.eclipse.collections.api.block.function.Function) UnsortedSetIterable(org.eclipse.collections.api.set.UnsortedSetIterable) Iterables.mList(org.eclipse.collections.impl.factory.Iterables.mList) Verify(org.eclipse.collections.impl.test.Verify) MutableList(org.eclipse.collections.api.list.MutableList) FastList(org.eclipse.collections.impl.list.mutable.FastList) AbstractCollectionTestCase(org.eclipse.collections.impl.collection.mutable.AbstractCollectionTestCase) LazyIterable(org.eclipse.collections.api.LazyIterable) MutableSet(org.eclipse.collections.api.set.MutableSet) RichIterable(org.eclipse.collections.api.RichIterable) IntegerWithCast(org.eclipse.collections.impl.IntegerWithCast) TreeBag(org.eclipse.collections.impl.bag.sorted.mutable.TreeBag) Interval(org.eclipse.collections.impl.list.Interval) NoSuchElementException(java.util.NoSuchElementException) Pair(org.eclipse.collections.api.tuple.Pair) Predicates(org.eclipse.collections.impl.block.factory.Predicates) Counter(org.eclipse.collections.impl.Counter) Predicates2(org.eclipse.collections.impl.block.factory.Predicates2) Iterator(java.util.Iterator) CollectionAddProcedure(org.eclipse.collections.impl.block.procedure.CollectionAddProcedure) MutableSortedBag(org.eclipse.collections.api.bag.sorted.MutableSortedBag) Procedures(org.eclipse.collections.impl.block.factory.Procedures) Test(org.junit.Test) Iterate(org.eclipse.collections.impl.utility.Iterate) Iterables.iSet(org.eclipse.collections.impl.factory.Iterables.iSet) Lists(org.eclipse.collections.impl.factory.Lists) Assert(org.junit.Assert) Collections(java.util.Collections) MutableSet(org.eclipse.collections.api.set.MutableSet) IntegerWithCast(org.eclipse.collections.impl.IntegerWithCast) Test(org.junit.Test)

Example 7 with MutableSet

use of org.eclipse.collections.api.set.MutableSet in project eclipse-collections by eclipse.

the class AbstractMutableSetTestCase method tap.

@Override
@Test
public void tap() {
    super.tap();
    int size = MORE_COLLISIONS.size();
    for (int i = 1; i < size; i++) {
        MutableList<Integer> tapResult = Lists.mutable.of();
        MutableSet<Integer> set = this.newWith();
        set.addAll(MORE_COLLISIONS.subList(0, i));
        Assert.assertSame(set, set.tap(tapResult::add));
        Assert.assertEquals(set.toList(), tapResult);
    }
    // test iterating on a bucket with only one element
    MutableSet<Integer> set = this.newWith(COLLISION_1, COLLISION_2);
    set.remove(COLLISION_2);
    Counter counter = new Counter();
    Assert.assertSame(set, set.tap(x -> counter.increment()));
    Assert.assertEquals(1, counter.getCount());
}
Also used : Arrays(java.util.Arrays) ArrayAdapter(org.eclipse.collections.impl.list.fixed.ArrayAdapter) Function(org.eclipse.collections.api.block.function.Function) UnsortedSetIterable(org.eclipse.collections.api.set.UnsortedSetIterable) Iterables.mList(org.eclipse.collections.impl.factory.Iterables.mList) Verify(org.eclipse.collections.impl.test.Verify) MutableList(org.eclipse.collections.api.list.MutableList) FastList(org.eclipse.collections.impl.list.mutable.FastList) AbstractCollectionTestCase(org.eclipse.collections.impl.collection.mutable.AbstractCollectionTestCase) LazyIterable(org.eclipse.collections.api.LazyIterable) MutableSet(org.eclipse.collections.api.set.MutableSet) RichIterable(org.eclipse.collections.api.RichIterable) IntegerWithCast(org.eclipse.collections.impl.IntegerWithCast) TreeBag(org.eclipse.collections.impl.bag.sorted.mutable.TreeBag) Interval(org.eclipse.collections.impl.list.Interval) NoSuchElementException(java.util.NoSuchElementException) Pair(org.eclipse.collections.api.tuple.Pair) Predicates(org.eclipse.collections.impl.block.factory.Predicates) Counter(org.eclipse.collections.impl.Counter) Predicates2(org.eclipse.collections.impl.block.factory.Predicates2) Iterator(java.util.Iterator) CollectionAddProcedure(org.eclipse.collections.impl.block.procedure.CollectionAddProcedure) MutableSortedBag(org.eclipse.collections.api.bag.sorted.MutableSortedBag) Procedures(org.eclipse.collections.impl.block.factory.Procedures) Test(org.junit.Test) Iterate(org.eclipse.collections.impl.utility.Iterate) Iterables.iSet(org.eclipse.collections.impl.factory.Iterables.iSet) Lists(org.eclipse.collections.impl.factory.Lists) Assert(org.junit.Assert) Collections(java.util.Collections) Counter(org.eclipse.collections.impl.Counter) Test(org.junit.Test)

Aggregations

MutableSet (org.eclipse.collections.api.set.MutableSet)7 MutableList (org.eclipse.collections.api.list.MutableList)5 Procedures (org.eclipse.collections.impl.block.factory.Procedures)5 Lists (org.eclipse.collections.impl.factory.Lists)5 Interval (org.eclipse.collections.impl.list.Interval)5 FastList (org.eclipse.collections.impl.list.mutable.FastList)5 Verify (org.eclipse.collections.impl.test.Verify)5 Assert (org.junit.Assert)5 Test (org.junit.Test)5 Collections (java.util.Collections)4 Iterator (java.util.Iterator)4 NoSuchElementException (java.util.NoSuchElementException)4 LazyIterable (org.eclipse.collections.api.LazyIterable)4 RichIterable (org.eclipse.collections.api.RichIterable)4 Function (org.eclipse.collections.api.block.function.Function)4 UnsortedSetIterable (org.eclipse.collections.api.set.UnsortedSetIterable)4 Pair (org.eclipse.collections.api.tuple.Pair)4 Predicates (org.eclipse.collections.impl.block.factory.Predicates)4 Iterables.mList (org.eclipse.collections.impl.factory.Iterables.mList)4 Iterate (org.eclipse.collections.impl.utility.Iterate)4