/* * 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; import com.gs.collections.api.RichIterable; import com.gs.collections.api.multimap.Multimap; import com.gs.collections.api.multimap.MutableMultimap; import com.gs.collections.api.partition.PartitionIterable; import com.gs.collections.impl.block.factory.IntegerPredicates; import com.gs.collections.impl.block.factory.Predicates2; import com.gs.collections.impl.factory.Lists; import com.gs.collections.impl.lazy.LazyIterableAdapter; import com.gs.collections.impl.list.mutable.FastList; 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.*; public class SynchronizedRichIterableTest extends AbstractRichIterableTestCase { @Override protected <T> RichIterable<T> newWith(T... littleElements) { return SynchronizedRichIterable.of(Lists.mutable.of(littleElements)); } @Override @Test public void partition() { RichIterable<Integer> integers = this.newWith(-3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9); PartitionIterable<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() { RichIterable<Integer> integers = this.newWith(-3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9); PartitionIterable<Integer> result = integers.partitionWith(Predicates2.in(), FastList.newListWith(-2, 0, 2, 4, 6, 8)); Assert.assertEquals(iList(-2, 0, 2, 4, 6, 8), result.getSelected()); Assert.assertEquals(iList(-3, -1, 1, 3, 5, 7, 9), result.getRejected()); } @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)); } @Test public void groupByWithTarget() { RichIterable<Integer> list = this.newWith(1, 2, 3, 4, 5, 6, 7); MutableMultimap<Boolean, Integer> multimap = new FastListMultimap<>(); list.groupBy(object -> IntegerPredicates.isOdd().accept(object), multimap); Assert.assertEquals(FastList.newListWith(1, 3, 5, 7), multimap.get(Boolean.TRUE)); Assert.assertEquals(FastList.newListWith(2, 4, 6), multimap.get(Boolean.FALSE)); } @Test public void asLazy() { RichIterable<Integer> integers = this.newWith(-3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9).asLazy(); Verify.assertInstanceOf(LazyIterableAdapter.class, integers); PartitionIterable<Integer> result = integers.partitionWith(Predicates2.in(), FastList.newListWith(-2, 0, 2, 4, 6, 8)); Assert.assertEquals(iList(-2, 0, 2, 4, 6, 8), result.getSelected()); Assert.assertEquals(iList(-3, -1, 1, 3, 5, 7, 9), result.getRejected()); } @Test(expected = IllegalArgumentException.class) public void nullCheck() { SynchronizedRichIterable.of(null, null); } }