Skip to content

Commit

Permalink
Change MutableBagIterable.addOccurrences(T item, int occurrences) to …
Browse files Browse the repository at this point in the history
…return the updated number of occurrences instead of void. Fixes #27.

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

* Changed MutableBagIterable.addOccurrences(T item, int occurrences) to return the updated number of occurrences instead of void.
* Implemented Multimap.keySet() to return an unmodifiable SetIterable of keys.

Optimizations
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,20 @@

public interface MutableBagIterable<T> extends Bag<T>, MutableCollection<T>
{
void addOccurrences(T item, int occurrences);
/**
* Add number of {@code occurrences} for an {@code item}. If the {@code item} does not exist, then the {@code item} is added to the bag.
*
* <p>
* For Example:
* <pre>
* MutableBagIterable&lt;String&gt; names = Bags.mutable.of("A", "B", "B");
* Assert.assertEquals(4, names.<b>addOccurrences</b>("A", 3));
* </pre>
*
* @return updated number of occurrences.
* @throws IllegalArgumentException if {@code occurrences} are less than 0.
*/
int addOccurrences(T item, int occurrences);

boolean removeOccurrences(Object item, int occurrences);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,19 @@ public void value(T each, int occurrences)
return source.notEmpty();
}

public void addOccurrences(T item, int occurrences)
public int addOccurrences(T item, int occurrences)
{
if (occurrences < 0)
{
throw new IllegalArgumentException("Cannot add a negative number of occurrences");
}
if (occurrences > 0)
{
this.items.updateValue(item, 0, IntToIntFunctions.add(occurrences));
int updatedOccurrences = this.items.updateValue(item, 0, IntToIntFunctions.add(occurrences));
this.size += occurrences;
return updatedOccurrences;
}
return this.occurrencesOf(item);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,12 +217,12 @@ public ImmutableBag<T> toImmutable()
}
}

public void addOccurrences(T item, int occurrences)
public int addOccurrences(T item, int occurrences)
{
this.acquireWriteLock();
try
{
this.delegate.addOccurrences(item, occurrences);
return this.delegate.addOccurrences(item, occurrences);
}
finally
{
Expand Down Expand Up @@ -860,9 +860,9 @@ public Iterator<T> iterator()
return iterator;
}

public void addOccurrences(T item, int occurrences)
public int addOccurrences(T item, int occurrences)
{
this.getDelegate().addOccurrences(item, occurrences);
return this.getDelegate().addOccurrences(item, occurrences);
}

public boolean removeOccurrences(Object item, int occurrences)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,11 @@ protected Object writeReplace()
return new SynchronizedCollectionSerializationProxy<T>(this.getDelegate());
}

public void addOccurrences(T item, int occurrences)
public int addOccurrences(T item, int occurrences)
{
synchronized (this.getLock())
{
this.getDelegate().addOccurrences(item, occurrences);
return this.getDelegate().addOccurrences(item, occurrences);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ public <V> MutableBagMultimap<V, T> groupByEach(Function<? super T, ? extends It
return this.getMutableBag().groupByEach(function);
}

public void addOccurrences(T item, int occurrences)
public int addOccurrences(T item, int occurrences)
{
throw new UnsupportedOperationException("Cannot call addOccurrences() on " + this.getClass().getSimpleName());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,11 @@ protected Object writeReplace()
return new SynchronizedCollectionSerializationProxy<T>(this.getDelegate());
}

public void addOccurrences(T item, int occurrences)
public int addOccurrences(T item, int occurrences)
{
synchronized (this.getLock())
{
this.getDelegate().addOccurrences(item, occurrences);
return this.getDelegate().addOccurrences(item, occurrences);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -444,17 +444,20 @@ public Iterator<T> iterator()
return new InternalIterator();
}

public void addOccurrences(T item, int occurrences)
public int addOccurrences(T item, int occurrences)
{
if (occurrences < 0)
{
throw new IllegalArgumentException("Cannot add a negative number of occurrences");
}
if (occurrences > 0)
{
this.items.getIfAbsentPut(item, NEW_COUNTER_BLOCK).add(occurrences);
Counter counter = this.items.getIfAbsentPut(item, NEW_COUNTER_BLOCK);
counter.add(occurrences);
this.size += occurrences;
return counter.getCount();
}
return this.occurrencesOf(item);
}

public boolean removeOccurrences(Object item, int occurrences)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public MutableSortedBag<T> newEmpty()
return this.getSortedBag().newEmpty();
}

public void addOccurrences(T item, int occurrences)
public int addOccurrences(T item, int occurrences)
{
throw new UnsupportedOperationException("Cannot call addOccurrences() on " + this.getClass().getSimpleName());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ default void MutableBagIterable_removeOccurrences_throws()
default void MutableBagIterable_addOccurrences()
{
MutableBagIterable<Integer> mutableBag = this.newWith(1, 2, 2, 3, 3, 3);
mutableBag.addOccurrences(4, 4);
assertEquals(4, mutableBag.addOccurrences(4, 4));
assertEquals(Bags.immutable.with(1, 2, 2, 3, 3, 3, 4, 4, 4, 4), mutableBag);
mutableBag.addOccurrences(1, 2);
assertEquals(3, mutableBag.addOccurrences(1, 2));
assertEquals(Bags.immutable.with(1, 1, 1, 2, 2, 3, 3, 3, 4, 4, 4, 4), mutableBag);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ default void Bag_toStringOfItemToCount()
default void MutableBagIterable_addOccurrences()
{
MutableSortedBag<Integer> mutableSortedBag = this.newWith(1, 2, 2, 3, 3, 3);
mutableSortedBag.addOccurrences(4, 4);
assertEquals(4, mutableSortedBag.addOccurrences(4, 4));
assertEquals(TreeBag.newBagWith(1, 2, 2, 3, 3, 3, 4, 4, 4, 4), mutableSortedBag);
mutableSortedBag.addOccurrences(1, 2);
assertEquals(3, mutableSortedBag.addOccurrences(1, 2));
assertEquals(TreeBag.newBagWith(1, 1, 1, 2, 2, 3, 3, 3, 4, 4, 4, 4), mutableSortedBag);
mutableSortedBag.addOccurrences(1, 0);
assertEquals(3, mutableSortedBag.addOccurrences(1, 0));
assertEquals(TreeBag.newBagWith(1, 1, 1, 2, 2, 3, 3, 3, 4, 4, 4, 4), mutableSortedBag);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ public interface MutableSortedBagTestCase extends SortedBagTestCase, MutableOrde
default void MutableBagIterable_addOccurrences()
{
MutableSortedBag<Integer> mutableSortedBag = this.newWith(3, 3, 3, 2, 2, 1);
mutableSortedBag.addOccurrences(4, 4);
assertEquals(4, mutableSortedBag.addOccurrences(4, 4));
assertEquals(TreeBag.newBagWith(Comparators.reverseNaturalOrder(), 4, 4, 4, 4, 3, 3, 3, 2, 2, 1), mutableSortedBag);
mutableSortedBag.addOccurrences(1, 2);
assertEquals(3, mutableSortedBag.addOccurrences(1, 2));
assertEquals(TreeBag.newBagWith(Comparators.reverseNaturalOrder(), 4, 4, 4, 4, 3, 3, 3, 2, 2, 1, 1, 1), mutableSortedBag);
mutableSortedBag.addOccurrences(1, 0);
assertEquals(3, mutableSortedBag.addOccurrences(1, 0));
assertEquals(TreeBag.newBagWith(Comparators.reverseNaturalOrder(), 4, 4, 4, 4, 3, 3, 3, 2, 2, 1, 1, 1), mutableSortedBag);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,12 @@ public void asUnmodifiable()
@Test
public void addOccurrences()
{
MutableBag<Integer> bag = MultiReaderHashBag.newBagWith(1, 1);
bag.addOccurrences(1, 2);
MutableBag<Integer> bag = this.newWith(1, 1);
Assert.assertEquals(4, bag.addOccurrences(1, 2));
MutableBagTestCase.assertBagsEqual(HashBag.newBagWith(1, 1, 1, 1), bag);
Assert.assertEquals(0, bag.addOccurrences(2, 0));
Assert.assertEquals(2, bag.addOccurrences(2, 2));
MutableBagTestCase.assertBagsEqual(HashBag.newBagWith(1, 1, 1, 1, 2, 2), bag);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,14 @@ public void toImmutable()
public void addOccurrences()
{
MultiReaderHashBag<Integer> bag = MultiReaderHashBag.newBagWith(1, 1, 2, 3);
bag.addOccurrences(1, 2);
bag.addOccurrences(4, 2);
bag.addOccurrences(2, 1);
Assert.assertEquals(2, bag.addOccurrences(1, 0));
Assert.assertEquals(4, bag.addOccurrences(1, 2));
Assert.assertEquals(0, bag.addOccurrences(4, 0));
Assert.assertEquals(2, bag.addOccurrences(4, 2));
Assert.assertEquals(2, bag.addOccurrences(2, 1));
MutableBagTestCase.assertBagsEqual(HashBag.newBagWith(1, 1, 1, 1, 2, 2, 3, 4, 4), bag);
Assert.assertEquals(3, bag.addOccurrences(2, 1));
MutableBagTestCase.assertBagsEqual(HashBag.newBagWith(1, 1, 1, 1, 2, 2, 2, 3, 4, 4), bag);
}

@Test(expected = IllegalArgumentException.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,16 @@ public void occurrencesOf()
@Test
public void addOccurrences()
{
MutableBagIterable<Object> bag = this.newWith();
bag.addOccurrences(new Object(), 0);
MutableBagTestCase.assertBagsEqual(HashBag.newBag(), bag);
MutableBagIterable<String> bag = this.newWith();
Assert.assertEquals(0, bag.addOccurrences("0", 0));
Assert.assertEquals(1, bag.addOccurrences("1", 1));
Assert.assertEquals(1, bag.addOccurrences("1", 0));
Assert.assertEquals(2, bag.addOccurrences("2", 2));
MutableBagTestCase.assertBagsEqual(HashBag.newBagWith("1", "2", "2"), bag);
Assert.assertEquals(1, bag.addOccurrences("1", 0));
Assert.assertEquals(6, bag.addOccurrences("2", 4));
Assert.assertEquals(1, bag.addOccurrences("3", 1));
MutableBagTestCase.assertBagsEqual(HashBag.newBagWith("1", "2", "2", "2", "2", "2", "2", "3"), bag);
}

@Test(expected = IllegalArgumentException.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,12 @@ public void selectByOccurrences()
public void addOccurrences()
{
MutableBag<Integer> integers = this.newWith(1, 1, 1, 1, 2, 2, 2, 3, 3, 4);
Assert.assertEquals(0, integers.occurrencesOf(5));
integers.addOccurrences(5, 5);
Assert.assertEquals(5, integers.occurrencesOf(5));
Assert.assertEquals(6, integers.addOccurrences(1, 2));
Verify.assertBagsEqual(this.newWith(1, 1, 1, 1, 1, 1, 2, 2, 2, 3, 3, 4), integers);
Assert.assertEquals(0, integers.addOccurrences(5, 0));
Assert.assertEquals(2, integers.addOccurrences(5, 2));
Assert.assertEquals(3, integers.addOccurrences(3, 1));
Verify.assertBagsEqual(this.newWith(1, 1, 1, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 5, 5), integers);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -888,20 +888,20 @@ public void addOccurrences()
super.addOccurrences();

MutableSortedBag<Integer> bag = this.newWith();
bag.addOccurrences(0, 3);
bag.addOccurrences(2, 0);
bag.addOccurrences(1, 2);
Assert.assertEquals(3, bag.addOccurrences(0, 3));
Assert.assertEquals(0, bag.addOccurrences(2, 0));
Assert.assertEquals(2, bag.addOccurrences(1, 2));
Verify.assertSortedBagsEqual(TreeBag.newBagWith(0, 0, 0, 1, 1), bag);
bag.addOccurrences(0, 3);
bag.addOccurrences(1, 2);
Assert.assertEquals(6, bag.addOccurrences(0, 3));
Assert.assertEquals(4, bag.addOccurrences(1, 2));
Verify.assertSortedBagsEqual(TreeBag.newBagWith(0, 0, 0, 0, 0, 0, 1, 1, 1, 1), bag);

MutableSortedBag<Integer> revBag = this.newWith(Collections.<Integer>reverseOrder());
revBag.addOccurrences(2, 3);
revBag.addOccurrences(3, 2);
Assert.assertEquals(3, revBag.addOccurrences(2, 3));
Assert.assertEquals(2, revBag.addOccurrences(3, 2));
Verify.assertSortedBagsEqual(TreeBag.newBagWith(Collections.reverseOrder(), 3, 3, 2, 2, 2), revBag);
revBag.addOccurrences(2, 3);
revBag.addOccurrences(3, 2);
Assert.assertEquals(6, revBag.addOccurrences(2, 3));
Assert.assertEquals(4, revBag.addOccurrences(3, 2));
Verify.assertSortedBagsEqual(TreeBag.newBagWith(Collections.reverseOrder(), 3, 3, 3, 3, 2, 2, 2, 2, 2, 2), revBag);
}

Expand Down

0 comments on commit f4fda8f

Please sign in to comment.