/* * Copyright 2014 Goldman Sachs. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.gs.collections.impl.collection.mutable; import com.gs.collections.api.RichIterable; import com.gs.collections.api.collection.MutableCollection; import com.gs.collections.api.multimap.Multimap; import com.gs.collections.api.multimap.MutableMultimap; import com.gs.collections.api.partition.PartitionMutableCollection; import com.gs.collections.impl.block.factory.IntegerPredicates; import com.gs.collections.impl.block.factory.Predicates2; import com.gs.collections.impl.block.function.NegativeIntervalFunction; import com.gs.collections.impl.list.Interval; import com.gs.collections.impl.list.mutable.FastList; import com.gs.collections.impl.list.mutable.SynchronizedMutableList; import com.gs.collections.impl.multimap.list.FastListMultimap; import com.gs.collections.impl.test.Verify; import org.junit.Assert; import org.junit.Test; import static com.gs.collections.impl.factory.Iterables.*; /** * JUnit test for {@link SynchronizedMutableCollection}. */ public class SynchronizedMutableCollectionTest extends AbstractSynchronizedCollectionTestCase { @Override protected <T> MutableCollection<T> newWith(T... littleElements) { return new SynchronizedMutableCollection<>(FastList.newListWith(littleElements)); } @Override @Test public void newEmpty() { super.newEmpty(); Verify.assertInstanceOf(SynchronizedMutableList.class, this.newWith().newEmpty()); } @Override public void equalsAndHashCode() { Assert.assertNotEquals(this.newWith(), this.newWith()); } @Override @Test public void groupBy() { RichIterable<Integer> list = this.newWith(1, 2, 3, 4, 5, 6, 7); Multimap<Boolean, Integer> multimap = list.groupBy(object -> IntegerPredicates.isOdd().accept(object)); Assert.assertEquals(FastList.newListWith(1, 3, 5, 7), multimap.get(Boolean.TRUE)); Assert.assertEquals(FastList.newListWith(2, 4, 6), multimap.get(Boolean.FALSE)); } @Override @Test public void groupByEach() { RichIterable<Integer> underTest = this.newWith(1, 2, 3, 4, 5, 6, 7); MutableMultimap<Integer, Integer> expected = FastListMultimap.newMultimap(); for (int i = 1; i < 8; i++) { expected.putAll(-i, Interval.fromTo(i, 7)); } Multimap<Integer, Integer> actual = underTest.groupByEach(new NegativeIntervalFunction()); Assert.assertEquals(expected, actual); Multimap<Integer, Integer> actualWithTarget = underTest.groupByEach(new NegativeIntervalFunction(), FastListMultimap.<Integer, Integer>newMultimap()); Assert.assertEquals(expected, actualWithTarget); } @Override @Test public void asSynchronized() { Verify.assertInstanceOf(SynchronizedMutableCollection.class, this.newWith().asSynchronized()); } @Override @Test public void asUnmodifiable() { Verify.assertInstanceOf(UnmodifiableMutableCollection.class, this.newWith().asUnmodifiable()); } @Override @Test public void partition() { MutableCollection<Integer> integers = this.newWith(-3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9); PartitionMutableCollection<Integer> result = integers.partition(IntegerPredicates.isEven()); Assert.assertEquals(iList(-2, 0, 2, 4, 6, 8), result.getSelected()); Assert.assertEquals(iList(-3, -1, 1, 3, 5, 7, 9), result.getRejected()); } @Override @Test public void partitionWith() { MutableCollection<Integer> integers = this.newWith(-3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9); PartitionMutableCollection<Integer> result = integers.partitionWith(Predicates2.in(), integers.select(IntegerPredicates.isEven())); Assert.assertEquals(iList(-2, 0, 2, 4, 6, 8), result.getSelected()); Assert.assertEquals(iList(-3, -1, 1, 3, 5, 7, 9), result.getRejected()); } @Override @Test public void with() { MutableCollection<Integer> coll = this.newWith(1, 2, 3); MutableCollection<Integer> collWith = coll.with(4); Assert.assertSame(coll, collWith); Assert.assertEquals(this.newWith(1, 2, 3, 4).toList(), collWith.toList()); } @Override @Test public void withAll() { MutableCollection<Integer> coll = this.newWith(1, 2, 3); MutableCollection<Integer> collWith = coll.withAll(FastList.newListWith(4, 5)); Assert.assertSame(coll, collWith); Assert.assertEquals(this.newWith(1, 2, 3, 4, 5).toList(), collWith.toList()); } @Override @Test public void without() { MutableCollection<Integer> coll = this.newWith(1, 2, 3); MutableCollection<Integer> collWithout = coll.without(2); Assert.assertSame(coll, collWithout); MutableCollection<Integer> expectedSet = this.newWith(1, 3); Assert.assertEquals(expectedSet.toList(), collWithout.toList()); Assert.assertEquals(expectedSet.toList(), collWithout.without(4).toList()); } @Override @Test public void withoutAll() { MutableCollection<Integer> coll = this.newWith(1, 2, 3, 4, 5); MutableCollection<Integer> collWithout = coll.withoutAll(FastList.newListWith(2, 4)); Assert.assertSame(coll, collWithout); MutableCollection<Integer> expectedSet = this.newWith(1, 3, 5); Assert.assertEquals(expectedSet.toList(), collWithout.toList()); Assert.assertEquals(expectedSet.toList(), collWithout.withoutAll(FastList.newListWith(2, 4)).toList()); } }