Skip to content

Commit

Permalink
Implement selectByOccurrences on primitive Bags.
Browse files Browse the repository at this point in the history
Signed-off-by: Donald Raab <Donald.Raab@gs.com>
  • Loading branch information
Donald Raab authored and Donald Raab committed Aug 20, 2016
1 parent 2dc7b14 commit 78bd9e4
Show file tree
Hide file tree
Showing 14 changed files with 133 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package org.eclipse.collections.api.bag.primitive;
import org.eclipse.collections.api.<name>Iterable;
import org.eclipse.collections.api.bag.ImmutableBag;
import org.eclipse.collections.api.block.function.primitive.<name>ToObjectFunction;
import org.eclipse.collections.api.block.predicate.primitive.IntPredicate;
import org.eclipse.collections.api.block.predicate.primitive.<name>Predicate;
import org.eclipse.collections.api.collection.primitive.Immutable<name>Collection;

Expand All @@ -29,6 +30,13 @@ public interface Immutable<name>Bag extends Immutable<name>Collection, <name>Bag
{
<sharedAPI(fileName(primitive), name)>

/**
* Returns all elements of the bag that have a number of occurrences that satisfy the predicate.
*
* @since 8.0
*/
Immutable<name>Bag selectByOccurrences(IntPredicate predicate);

\<V> ImmutableBag\<V> collect(<name>ToObjectFunction\<? extends V> function);

<immutableAPI(fileName(primitive), type, name)>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package org.eclipse.collections.api.bag.primitive;
import org.eclipse.collections.api.<name>Iterable;
import org.eclipse.collections.api.bag.MutableBag;
import org.eclipse.collections.api.block.function.primitive.<name>ToObjectFunction;
import org.eclipse.collections.api.block.predicate.primitive.IntPredicate;
import org.eclipse.collections.api.block.predicate.primitive.<name>Predicate;
import org.eclipse.collections.api.collection.primitive.Mutable<name>Collection;

Expand All @@ -31,6 +32,13 @@ public interface Mutable<name>Bag extends Mutable<name>Collection, <name>Bag

boolean removeOccurrences(<type> item, int occurrences);

/**
* Returns all elements of the bag that have a number of occurrences that satisfy the predicate.
*
* @since 8.0
*/
Mutable<name>Bag selectByOccurrences(IntPredicate predicate);

<sharedAPI(fileName(primitive), name)>

\<V> MutableBag\<V> collect(<name>ToObjectFunction\<? extends V> function);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package org.eclipse.collections.api.bag.primitive;
import org.eclipse.collections.api.<name>Iterable;
import org.eclipse.collections.api.bag.Bag;
import org.eclipse.collections.api.block.function.primitive.<name>ToObjectFunction;
import org.eclipse.collections.api.block.predicate.primitive.IntPredicate;
import org.eclipse.collections.api.block.predicate.primitive.<name>Predicate;
import org.eclipse.collections.api.block.procedure.primitive.<name>IntProcedure;

Expand All @@ -32,6 +33,13 @@ public interface <name>Bag extends <name>Iterable
*/
int sizeDistinct();

/**
* Returns all elements of the bag that have a number of occurrences that satisfy the predicate.
*
* @since 8.0
*/
<name>Bag selectByOccurrences(IntPredicate predicate);

/**
* The occurrences of a distinct item in the bag.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import org.eclipse.collections.api.bag.primitive.<name>Bag;
import org.eclipse.collections.api.bag.primitive.Mutable<name>Bag;
import org.eclipse.collections.api.block.function.primitive.<name>ToObjectFunction;
import org.eclipse.collections.api.block.function.primitive.Object<name>ToObjectFunction;
import org.eclipse.collections.api.block.predicate.primitive.IntPredicate;
import org.eclipse.collections.api.block.predicate.primitive.<name>Predicate;
import org.eclipse.collections.api.block.procedure.primitive.<name>IntProcedure;
import org.eclipse.collections.api.block.procedure.primitive.<name>Procedure;
Expand Down Expand Up @@ -154,6 +155,11 @@ final class Immutable<name>EmptyBag implements Immutable<name>Bag, Serializable
{
}

public Immutable<name>Bag selectByOccurrences(IntPredicate predicate)
{
return this;
}

public <type> detectIfNone(<name>Predicate predicate, <type> ifNone)
{
return ifNone;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import org.eclipse.collections.api.bag.primitive.Immutable<name>Bag;
import org.eclipse.collections.api.bag.primitive.Mutable<name>Bag;
import org.eclipse.collections.api.block.function.primitive.<name>ToObjectFunction;
import org.eclipse.collections.api.block.function.primitive.Object<name>ToObjectFunction;
import org.eclipse.collections.api.block.predicate.primitive.IntPredicate;
import org.eclipse.collections.api.block.predicate.primitive.<name>Predicate;
import org.eclipse.collections.api.block.procedure.primitive.<name>IntProcedure;
import org.eclipse.collections.api.block.procedure.primitive.<name>Procedure;
Expand Down Expand Up @@ -143,6 +144,11 @@ final class Immutable<name>HashBag implements Immutable<name>Bag, Serializable
return this.delegate.select(predicate).toImmutable();
}

public Immutable<name>Bag selectByOccurrences(IntPredicate predicate)
{
return this.delegate.selectByOccurrences(predicate).toImmutable();
}

public Immutable<name>Bag reject(<name>Predicate predicate)
{
return this.delegate.reject(predicate).toImmutable();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import org.eclipse.collections.api.bag.primitive.<name>Bag;
import org.eclipse.collections.api.bag.primitive.Mutable<name>Bag;
import org.eclipse.collections.api.block.function.primitive.<name>ToObjectFunction;
import org.eclipse.collections.api.block.function.primitive.Object<name>ToObjectFunction;
import org.eclipse.collections.api.block.predicate.primitive.IntPredicate;
import org.eclipse.collections.api.block.predicate.primitive.<name>Predicate;
import org.eclipse.collections.api.block.procedure.primitive.<name>IntProcedure;
import org.eclipse.collections.api.block.procedure.primitive.<name>Procedure;
Expand Down Expand Up @@ -139,14 +140,23 @@ final class Immutable<name>SingletonBag implements Immutable<name>Bag, Serializa

public Immutable<name>Bag select(<name>Predicate predicate)
{
return predicate.accept(this.element1) ? <name>HashBag.newBagWith(this.element1).toImmutable()
: new <name>HashBag().toImmutable();
return predicate.accept(this.element1) ?
<name>Bags.immutable.with(this.element1) :
<name>Bags.immutable.empty();
}

public Immutable<name>Bag selectByOccurrences(IntPredicate predicate)
{
return predicate.accept(1) ?
<name>Bags.immutable.with(this.element1) :
<name>Bags.immutable.empty();
}

public Immutable<name>Bag reject(<name>Predicate predicate)
{
return predicate.accept(this.element1) ? new <name>HashBag().toImmutable()
: <name>HashBag.newBagWith(this.element1).toImmutable();
return predicate.accept(this.element1) ?
<name>Bags.immutable.empty() :
<name>Bags.immutable.with(this.element1);
}

public \<V> ImmutableBag\<V> collect(<name>ToObjectFunction\<? extends V> function)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import org.eclipse.collections.api.bag.primitive.Mutable<name>Bag;
import org.eclipse.collections.api.block.function.primitive.<name>ToObjectFunction;
import org.eclipse.collections.api.block.function.primitive.Object<name>ToObjectFunction;
import org.eclipse.collections.api.block.function.primitive.IntToIntFunction;
import org.eclipse.collections.api.block.predicate.primitive.IntPredicate;
import org.eclipse.collections.api.block.predicate.primitive.<name>Predicate;
import org.eclipse.collections.api.block.procedure.primitive.<name>Procedure;
import org.eclipse.collections.api.block.procedure.primitive.<name>IntProcedure;
Expand Down Expand Up @@ -194,6 +195,19 @@ public final class <name>HashBag extends Abstract<name>Iterable implements Mutab
this.items.forEachKeyValue(procedure);
}

public <name>HashBag selectByOccurrences(IntPredicate predicate)
{
final <name>HashBag result = new <name>HashBag();
this.forEachWithOccurrences((each, occurrences) ->
{
if (predicate.accept(occurrences))
{
result.addOccurrences(each, occurrences);
}
});
return result;
}

public boolean add(<type> item)
{
this.items.updateValue(item, 0, IntToIntFunctions.increment());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import org.eclipse.collections.api.bag.MutableBag;
import org.eclipse.collections.api.bag.primitive.Immutable<name>Bag;
import org.eclipse.collections.api.bag.primitive.Mutable<name>Bag;
import org.eclipse.collections.api.block.function.primitive.<name>ToObjectFunction;
import org.eclipse.collections.api.block.predicate.primitive.IntPredicate;
import org.eclipse.collections.api.block.predicate.primitive.<name>Predicate;
import org.eclipse.collections.api.block.procedure.primitive.<name>IntProcedure;
import org.eclipse.collections.api.iterator.<name>Iterator;
Expand Down Expand Up @@ -156,6 +157,14 @@ public final class Synchronized<name>Bag
}
}

public Mutable<name>Bag selectByOccurrences(IntPredicate predicate)
{
synchronized (this.getLock())
{
return this.getMutable<name>Bag().selectByOccurrences(predicate);
}
}

@Override
public Mutable<name>Bag reject(<name>Predicate predicate)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import org.eclipse.collections.api.bag.MutableBag;
import org.eclipse.collections.api.bag.primitive.Immutable<name>Bag;
import org.eclipse.collections.api.bag.primitive.Mutable<name>Bag;
import org.eclipse.collections.api.block.function.primitive.<name>ToObjectFunction;
import org.eclipse.collections.api.block.predicate.primitive.IntPredicate;
import org.eclipse.collections.api.block.predicate.primitive.<name>Predicate;
import org.eclipse.collections.api.block.procedure.primitive.<name>IntProcedure;
import org.eclipse.collections.impl.collection.mutable.primitive.AbstractUnmodifiable<name>Collection;
Expand Down Expand Up @@ -100,6 +101,11 @@ public final class Unmodifiable<name>Bag
this.getMutable<name>Bag().forEachWithOccurrences(procedure);
}

public Mutable<name>Bag selectByOccurrences(IntPredicate predicate)
{
return this.getMutable<name>Bag().selectByOccurrences(predicate);
}

@Override
public Mutable<name>Bag select(<name>Predicate predicate)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ public abstract class AbstractImmutable<name>BagTestCase extends AbstractImmutab
Assert.assertEquals(3L, this.newWith(<["0", "1", "1", "2", "2", "2"]:(literal.(type))(); separator=", ">).sizeDistinct());
}

@Test
public void selectByOccurrences()
{
Immutable<name>Bag bag = this.newWith(<["1", "2", "2", "3", "3", "3"]:(literal.(type))(); separator=", ">);
Immutable<name>Bag filtered = bag.selectByOccurrences(i -> i > 2);
Assert.assertEquals(<name>HashBag.newBagWith(<["3", "3", "3"]:(literal.(type))(); separator=", ">), filtered);
}

@Test
public void forEachWithOccurrences()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ body(type, name, wrapperName) ::= <<
package org.eclipse.collections.impl.bag.immutable.primitive;

import org.eclipse.collections.api.bag.primitive.Immutable<name>Bag;
import org.eclipse.collections.impl.bag.mutable.primitive.<name>HashBag;
import org.eclipse.collections.impl.factory.primitive.<name>Bags;
import org.eclipse.collections.impl.math.Mutable<wrapperName>;
import org.junit.Assert;
Expand Down Expand Up @@ -45,6 +46,17 @@ public class Immutable<name>SingletonBagTest extends AbstractImmutable<name>BagT
Assert.assertEquals("<(toStringLiteral.(type))("1")>1", string);
}

@Override
@Test
public void selectByOccurrences()
{
Immutable<name>SingletonBag bag = new Immutable<name>SingletonBag(<(literal.(type))("1")>);
Immutable<name>Bag filtered1 = bag.selectByOccurrences(i -> i > 0);
Assert.assertEquals(<name>HashBag.newBagWith(<["1"]:(literal.(type))(); separator=", ">), filtered1);
Immutable<name>Bag filtered2 = bag.selectByOccurrences(i -> i > 1);
Assert.assertEquals(<name>Bags.immutable.empty(), filtered2);
}

@Override
@Test
public void injectInto()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,16 @@ public abstract class AbstractMutable<name>BagTestCase extends AbstractMutable<n
Assert.assertEquals(3L, this.newWith(<["0", "1", "1", "2", "2", "2"]:(literal.(type))(); separator=", ">).sizeDistinct());
}

@Test
public void selectByOccurrences()
{
Mutable<name>Bag bag = this.newWith();
bag.addOccurrences(<(literal.(type))("100")>, 3);
bag.addOccurrences(<(literal.(type))("50")>, 2);
Mutable<name>Bag filtered = bag.selectByOccurrences(i -> i > 2);
Assert.assertEquals(<name>HashBag.newBagWith(<["100", "100", "100"]:(literal.(type))(); separator=", ">), filtered);
}

@Test
public void addOccurrences()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import java.util.NoSuchElementException;

import org.eclipse.collections.api.iterator.<name>Iterator;
import org.eclipse.collections.api.iterator.Mutable<name>Iterator;
import org.eclipse.collections.api.bag.primitive.Mutable<name>Bag;
import org.eclipse.collections.impl.test.Verify;
import org.junit.Assert;
import org.junit.Test;
Expand All @@ -45,6 +46,15 @@ public class Unmodifiable<name>BagTest extends AbstractMutable<name>BagTestCase
return new Unmodifiable<name>Bag(<name>HashBag.newBagWith(elements));
}

@Override
@Test
public void selectByOccurrences()
{
Mutable<name>Bag bag = this.newWith(<["100", "100", "100", "50", "50"]:(literal.(type))(); separator=", ">);
Mutable<name>Bag filtered = bag.selectByOccurrences(i -> i > 2);
Assert.assertEquals(<name>HashBag.newBagWith(<["100", "100", "100"]:(literal.(type))(); separator=", ">), filtered);
}

@Override
@Test(expected = UnsupportedOperationException.class)
public void addOccurrences()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.eclipse.collections.api.block.function.primitive.BooleanToObjectFunction;
import org.eclipse.collections.api.block.function.primitive.ObjectBooleanToObjectFunction;
import org.eclipse.collections.api.block.predicate.primitive.BooleanPredicate;
import org.eclipse.collections.api.block.predicate.primitive.IntPredicate;
import org.eclipse.collections.api.block.procedure.primitive.BooleanIntProcedure;
import org.eclipse.collections.api.block.procedure.primitive.BooleanProcedure;
import org.eclipse.collections.api.iterator.BooleanIterator;
Expand Down Expand Up @@ -492,6 +493,19 @@ public MutableBooleanBag select(BooleanPredicate predicate)
return result;
}

@Override
public MutableBooleanBag selectByOccurrences(IntPredicate predicate)
{
MutableBooleanBag result = new BooleanHashBag();
this.forEachWithOccurrences((each, occurrences) -> {
if (predicate.accept(occurrences))
{
result.addOccurrences(each, occurrences);
}
});
return result;
}

@Override
public MutableBooleanBag reject(BooleanPredicate predicate)
{
Expand Down

0 comments on commit 78bd9e4

Please sign in to comment.