Skip to content

Commit

Permalink
Add tests for stream on mutable collections and throw on spliterator,…
Browse files Browse the repository at this point in the history
… stream and parallelStream for multi reader collections.

Signed-off-by: Donald Raab <Donald.Raab@gs.com>
  • Loading branch information
Donald Raab authored and Donald Raab committed Feb 20, 2017
1 parent 8e9bbb9 commit dc387b3
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
import java.util.Comparator;
import java.util.Iterator;
import java.util.Optional;
import java.util.Spliterator;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.stream.Stream;

import org.eclipse.collections.api.LazyIterable;
import org.eclipse.collections.api.RichIterable;
Expand Down Expand Up @@ -1302,27 +1304,21 @@ public boolean isEmpty()
* to use an iterator with a MultiReader collection, then you must do the following:
* <p>
* <pre>
* multiReaderList.withReadLockAndDelegate(new Procedure<MutableList<Person>>()
* {
* public void value(MutableList<Person> people)
* multiReaderList.withReadLockAndDelegate(MutableList<Person> ->
* {
* Iterator it = people.iterator();
* ....
* }
* });
* });
* </pre>
* <p>
* <pre>
* final Collection jdkSet = new HashSet();
* final boolean containsAll = new boolean[1];
* multiReaderList.withReadLockAndDelegate(new Procedure<MutableList<Person>>()
* {
* public void value(MutableList<Person> people)
* multiReaderList.withReadLockAndDelegate(MutableList<Person> people ->
* {
* set.addAll(people); // addAll uses iterator() in AbstractCollection
* containsAll[0] = set.containsAll(people); // containsAll uses iterator() in AbstractCollection
* }
* });
* });
* </pre>
*/
@Override
Expand All @@ -1333,6 +1329,45 @@ public Iterator<T> iterator()
+ "If you would like to use an iterator, you must either use withReadLockAndDelegate() or withWriteLockAndDelegate().");
}

/**
* This method is not supported directly on MultiReader collections because it is not thread-safe. If you would like
* to use a spliterator with a MultiReader collection, then you must protect the calls by calling either
* withReadLockAndDelegate or withWriteLockAndDelegate.
*/
@Override
public Spliterator<T> spliterator()
{
throw new UnsupportedOperationException(
"Spliterator is not supported directly on MultiReader collections. "
+ "If you would like to use an spliterator, you must either use withReadLockAndDelegate() or withWriteLockAndDelegate().");
}

/**
* This method is not supported directly on MultiReader collections because it is not thread-safe. If you would like
* to use stream with a MultiReader collection, then you must protect the calls by calling either
* withReadLockAndDelegate or withWriteLockAndDelegate.
*/
@Override
public Stream<T> stream()
{
throw new UnsupportedOperationException(
"Stream is not supported directly on MultiReader collections. "
+ "If you would like to use stream, you must either use withReadLockAndDelegate() or withWriteLockAndDelegate().");
}

/**
* This method is not supported directly on MultiReader collections because it is not thread-safe. If you would like
* to use parallelStream with a MultiReader collection, then you must protect the calls by calling either
* withReadLockAndDelegate or withWriteLockAndDelegate.
*/
@Override
public Stream<T> parallelStream()
{
throw new UnsupportedOperationException(
"parallelStream is not supported directly on MultiReader collections. "
+ "If you would like to use parallelStream, you must either use withReadLockAndDelegate() or withWriteLockAndDelegate().");
}

@Override
public boolean remove(Object item)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ protected <T> MultiReaderHashBag<T> newWith(T... littleElements)
return MultiReaderHashBag.newBagWith(littleElements);
}

@Override
@Test (expected = UnsupportedOperationException.class)
public void largeCollectionStreamToBagMultimap()
{
super.largeCollectionStreamToBagMultimap();
}

@Override
@Test
public void newEmpty()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,16 @@
import org.eclipse.collections.api.collection.ImmutableCollection;
import org.eclipse.collections.api.collection.MutableCollection;
import org.eclipse.collections.api.list.MutableList;
import org.eclipse.collections.api.multimap.bag.MutableBagMultimap;
import org.eclipse.collections.api.tuple.Twin;
import org.eclipse.collections.impl.AbstractRichIterableTestCase;
import org.eclipse.collections.impl.block.factory.Predicates;
import org.eclipse.collections.impl.block.factory.Predicates2;
import org.eclipse.collections.impl.collector.Collectors2;
import org.eclipse.collections.impl.factory.Bags;
import org.eclipse.collections.impl.factory.Multimaps;
import org.eclipse.collections.impl.lazy.LazyIterableAdapter;
import org.eclipse.collections.impl.list.Interval;
import org.eclipse.collections.impl.list.mutable.FastList;
import org.eclipse.collections.impl.set.mutable.UnifiedSet;
import org.eclipse.collections.impl.test.Verify;
Expand Down Expand Up @@ -304,6 +308,15 @@ public void withoutAll()
Assert.assertEquals(this.newWith(1, 3, 5), collWithout);
}

@Test
public void largeCollectionStreamToBagMultimap()
{
MutableCollection<Integer> collection = this.newWith(Interval.oneTo(100000).toArray());
MutableBagMultimap<Integer, Integer> expected = collection.groupBy(each -> each % 100, Multimaps.mutable.bag.empty());
Assert.assertEquals(expected, collection.stream().collect(Collectors2.toBagMultimap(each -> each % 100)));
Assert.assertEquals(expected, collection.parallelStream().collect(Collectors2.toBagMultimap(each -> each % 100)));
}

@Test
public void asLazy()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ protected <T> MultiReaderFastList<T> newWith(T... littleElements)
return MultiReaderFastList.newListWith(littleElements);
}

@Override
@Test (expected = UnsupportedOperationException.class)
public void largeCollectionStreamToBagMultimap()
{
super.largeCollectionStreamToBagMultimap();
}

@Override
@Test
public void newEmpty()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,22 @@ public void iterator_throws()
{
this.newWith(1, 2, 3).iterator();
}

@Test(expected = UnsupportedOperationException.class)
public void spliterator_throws()
{
this.newWith(1, 2, 3).spliterator();
}

@Test(expected = UnsupportedOperationException.class)
public void stream_throws()
{
this.newWith(1, 2, 3).stream();
}

@Test(expected = UnsupportedOperationException.class)
public void parallelStream_throws()
{
this.newWith(1, 2, 3).parallelStream();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ protected <T> MutableSet<T> newWith(T... littleElements)
return MultiReaderUnifiedSet.newSetWith(littleElements);
}

@Override
@Test (expected = UnsupportedOperationException.class)
public void largeCollectionStreamToBagMultimap()
{
super.largeCollectionStreamToBagMultimap();
}

@Override
@Test
public void asSynchronized()
Expand Down

0 comments on commit dc387b3

Please sign in to comment.