Skip to content

Commit

Permalink
Implement Bags.mutable.ofAll(Iterable<? extends T> items) and Bags.mu…
Browse files Browse the repository at this point in the history
…table.withAll(Iterable<? extends T> items).

Signed-off-by: Nikhil Nanivadekar <nikhil.nanivadekar@gs.com>
  • Loading branch information
nikhilnanivadekar committed Apr 15, 2016
1 parent 82f48f9 commit 8b97ee5
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 0 deletions.
1 change: 1 addition & 0 deletions RELEASE_NOTE_DRAFT.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ New Functionality
-----------------

* Changed MutableBagIterable.addOccurrences(T item, int occurrences) to return the updated number of occurrences instead of void.
* Implemented Bags.mutable.ofAll(Iterable<? extends T> items) and Bags.mutable.withAll(Iterable<? extends T> items).
* Implemented Multimap.keySet() to return an unmodifiable SetIterable of keys.
* Implemented MutableMultimap.putAllPairs(Iterable<Pair<K, V>> keyValuePairs).
* Pull up into() from LazyIterable to RichIterable.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,11 @@ public interface MutableBagFactory
<T> MutableBag<T> of(T... elements);

<T> MutableBag<T> with(T... elements);

/**
* Same as {@link #withAll(Iterable)}.
*/
<T> MutableBag<T> ofAll(Iterable<? extends T> items);

<T> MutableBag<T> withAll(Iterable<? extends T> items);
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,14 @@ public <T> MutableBag<T> with(T... elements)
{
return HashBag.newBagWith(elements);
}

public <T> MutableBag<T> ofAll(Iterable<? extends T> items)
{
return this.withAll(items);
}

public <T> MutableBag<T> withAll(Iterable<? extends T> items)
{
return HashBag.newBag(items);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,47 @@

package org.eclipse.collections.impl.factory;

import org.eclipse.collections.api.bag.Bag;
import org.eclipse.collections.api.bag.ImmutableBag;
import org.eclipse.collections.api.bag.MutableBag;
import org.eclipse.collections.api.factory.bag.ImmutableBagFactory;
import org.eclipse.collections.api.factory.bag.MutableBagFactory;
import org.eclipse.collections.impl.bag.mutable.HashBag;
import org.eclipse.collections.impl.list.mutable.FastList;
import org.eclipse.collections.impl.set.mutable.UnifiedSet;
import org.eclipse.collections.impl.test.Verify;
import org.junit.Assert;
import org.junit.Test;

public class BagsTest
{
@Test
public void mutables()
{
MutableBagFactory bagFactory = Bags.mutable;
Verify.assertBagsEqual(HashBag.newBag(), bagFactory.of());
Verify.assertInstanceOf(MutableBag.class, bagFactory.of());
Verify.assertBagsEqual(HashBag.newBagWith(1), bagFactory.of(1));
Verify.assertInstanceOf(MutableBag.class, bagFactory.of(1));
Verify.assertBagsEqual(HashBag.newBagWith(1, 2), bagFactory.of(1, 2));
Verify.assertInstanceOf(MutableBag.class, bagFactory.of(1, 2));
Verify.assertBagsEqual(HashBag.newBagWith(1, 2, 3), bagFactory.of(1, 2, 3));
Verify.assertInstanceOf(MutableBag.class, bagFactory.of(1, 2, 3));
Verify.assertBagsEqual(HashBag.newBagWith(1, 2, 3, 4), bagFactory.of(1, 2, 3, 4));
Verify.assertInstanceOf(MutableBag.class, bagFactory.of(1, 2, 3, 4));
Verify.assertBagsEqual(HashBag.newBagWith(1, 2, 3, 4, 5), bagFactory.of(1, 2, 3, 4, 5));
Verify.assertInstanceOf(MutableBag.class, bagFactory.of(1, 2, 3, 4, 5));

Bag<Integer> bag = HashBag.newBagWith(1, 2, 2, 3, 3, 3);
Verify.assertBagsEqual(HashBag.newBagWith(1, 2, 2, 3, 3, 3), bagFactory.ofAll(bag));
Verify.assertInstanceOf(MutableBag.class, bagFactory.ofAll(bag));
Assert.assertNotSame(bagFactory.ofAll(bag), bag);
Verify.assertBagsEqual(HashBag.newBagWith(1, 2, 2, 3, 3, 3), bagFactory.ofAll(FastList.newListWith(1, 2, 2, 3, 3, 3)));
Verify.assertInstanceOf(MutableBag.class, bagFactory.ofAll(FastList.newListWith(1, 2, 2, 3, 3, 3)));
Verify.assertBagsEqual(HashBag.newBagWith(1, 2, 3, 4, 5), bagFactory.ofAll(UnifiedSet.newSetWith(1, 2, 3, 4, 5)));
Verify.assertInstanceOf(MutableBag.class, bagFactory.ofAll(UnifiedSet.newSetWith(1, 2, 3, 4, 5)));
}

@Test
public void immutables()
{
Expand Down

0 comments on commit 8b97ee5

Please sign in to comment.