Skip to content

Commit

Permalink
Implement select and reject with targets on primitive iterables.
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 Jan 24, 2017
1 parent 6c53174 commit 52e29e1
Show file tree
Hide file tree
Showing 7 changed files with 223 additions and 301 deletions.
Expand Up @@ -50,27 +50,88 @@ import org.eclipse.collections.api.set.primitive.Mutable<name>Set;
*/
public interface <name>Iterable extends PrimitiveIterable
{
/**
* Returns a primitive iterator that can be used to iterate over the <name>Iterable in an
* imperative style.
*/
<name>Iterator <type>Iterator();

/**
* Converts the <name>Iterable to a primitive <type> array.
*/
<type>[] toArray();

/**
* Returns true if the value is contained in the <name>Iterable, and false if it is not.
*/
boolean contains(<type> value);

/**
* Returns true if the all of the values specified in the source array are contained
* in the <name>Iterable, and false if they are not.
*/
boolean containsAll(<type>... source);

/**
* Returns true if the all of the values specified in the source <name>Iterable are contained
* in the <name>Iterable, and false if they are not.
*/
boolean containsAll(<name>Iterable source);

/**
* Applies the <name>Procedure to each element in the <name>Iterable.
*/
void forEach(<name>Procedure procedure);

/**
* A synonym for forEach.
*
* @since 7.0.
*/
void each(<name>Procedure procedure);

/**
* Returns a new <name>Iterable with all of the elements in the <name>Iterable that
* return true for the specified predicate.
*/
<name>Iterable select(<name>Predicate predicate);

/**
* Returns a new <name>Iterable with all of the elements in the <name>Iterable that
* return false for the specified predicate.
*/
<name>Iterable reject(<name>Predicate predicate);

/**
* @since 8.1.
*/
default \<R extends Mutable<name>Collection> R select(<name>Predicate predicate, R target)
{
this.each(each ->
{
if (predicate.accept(each))
{
target.add(each);
}
});
return target;
}

/**
* @since 8.1.
*/
default \<R extends Mutable<name>Collection> R reject(<name>Predicate predicate, R target)
{
this.each(each ->
{
if (!predicate.accept(each))
{
target.add(each);
}
});
return target;
}

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

/**
Expand Down Expand Up @@ -176,20 +237,48 @@ public interface <name>Iterable extends PrimitiveIterable

<type> detectIfNone(<name>Predicate predicate, <type> ifNone);

/**
* Returns a count of the number of elements in the <name>Iterable that return true for the
* specified predicate.
*/
int count(<name>Predicate predicate);

/**
* Returns true if any of the elements in the <name>Iterable return true for the
* specified predicate, otherwise returns false.
*/
boolean anySatisfy(<name>Predicate predicate);

/**
* Returns true if all of the elements in the <name>Iterable return true for the
* specified predicate, otherwise returns false.
*/
boolean allSatisfy(<name>Predicate predicate);

/**
* Returns true if none of the elements in the <name>Iterable return true for the
* specified predicate, otherwise returns false.
*/
boolean noneSatisfy(<name>Predicate predicate);

/**
* Converts the <name>Iterable to a new Mutable<name>List.
*/
Mutable<name>List toList();

/**
* Converts the <name>Iterable to a new Mutable<name>Set.
*/
Mutable<name>Set toSet();

/**
* Converts the <name>Iterable to a new Mutable<name>Bag.
*/
Mutable<name>Bag toBag();

/**
* Returns a Lazy<name>Iterable adapter wrapping the source <name>Iterable.
*/
Lazy<name>Iterable asLazy();

\<T> T injectInto(T injectedValue, Object<name>ToObjectFunction\<? super T, ? extends T> function);
Expand Down
Expand Up @@ -19,6 +19,7 @@ package org.eclipse.collections.impl.list.immutable.primitive;
import java.io.IOException;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Collection;
import java.util.NoSuchElementException;

import org.eclipse.collections.api.<name>Iterable;
Expand All @@ -30,6 +31,7 @@ import org.eclipse.collections.api.block.function.primitive.Object<name>IntToObj
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;
import org.eclipse.collections.api.collection.primitive.Mutable<name>Collection;
import org.eclipse.collections.api.iterator.<name>Iterator;
import org.eclipse.collections.api.list.ImmutableList;
import org.eclipse.collections.api.list.primitive.Immutable<name>List;
Expand Down Expand Up @@ -187,40 +189,47 @@ final class Immutable<name>ArrayList

public boolean noneSatisfy(<name>Predicate predicate)
{
for (<type> item : this.items)
{
if (predicate.accept(item))
{
return false;
}
}
return true;
return !this.anySatisfy(predicate);
}

public Immutable<name>List select(<name>Predicate predicate)
{
<name>ArrayList result = new <name>ArrayList();
return this.select(predicate, new <name>ArrayList()).toImmutable();
}

/**
* @since 8.1.
*/
public \<R extends Mutable<name>Collection> R select(<name>Predicate predicate, R target)
{
for (<type> item : this.items)
{
if (predicate.accept(item))
{
result.add(item);
target.add(item);
}
}
return result.toImmutable();
return target;
}

public Immutable<name>List reject(<name>Predicate predicate)
{
<name>ArrayList result = new <name>ArrayList();
return this.reject(predicate, new <name>ArrayList()).toImmutable();
}

/**
* @since 8.1.
*/
public \<R extends Mutable<name>Collection> R reject(<name>Predicate predicate, R target)
{
for (<type> item : this.items)
{
if (!predicate.accept(item))
{
result.add(item);
target.add(item);
}
}
return result.toImmutable();
return target;
}

public <type> detectIfNone(<name>Predicate predicate, <type> ifNone)
Expand All @@ -237,12 +246,19 @@ final class Immutable<name>ArrayList

public \<V> ImmutableList\<V> collect(<name>ToObjectFunction\<? extends V> function)
{
FastList\<V> target = FastList.newList(this.items.length);
return this.collect(function, FastList.newList(this.items.length)).toImmutable();
}

/**
* @since 8.1.
*/
public \<V, R extends Collection\<V>\> R collect(<name>ToObjectFunction\<? extends V> function, R target)
{
for (<type> item : this.items)
{
target.add(function.valueOf(item));
}
return target.toImmutable();
return target;
}

<if(primitive.floatingPoint)>public <wideType.(type)> sum()
Expand Down
Expand Up @@ -21,6 +21,7 @@ import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.Arrays;
import java.util.Collection;
import java.util.NoSuchElementException;

import org.eclipse.collections.api.<name>Iterable;
Expand All @@ -31,6 +32,7 @@ import org.eclipse.collections.api.block.function.primitive.<name>ToObjectFuncti
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;
import org.eclipse.collections.api.collection.primitive.Mutable<name>Collection;
import org.eclipse.collections.api.iterator.Mutable<name>Iterator;
import org.eclipse.collections.api.list.MutableList;
import org.eclipse.collections.api.list.primitive.<name>List;
Expand Down Expand Up @@ -573,42 +575,49 @@ public class <name>ArrayList extends Abstract<name>Iterable

public boolean noneSatisfy(<name>Predicate predicate)
{
for (int i = 0; i \< this.size; i++)
{
if (predicate.accept(this.items[i]))
{
return false;
}
}
return true;
return !this.anySatisfy(predicate);
}

public <name>ArrayList select(<name>Predicate predicate)
{
<name>ArrayList result = new <name>ArrayList();
return this.select(predicate, new <name>ArrayList());
}

/**
* @since 8.1.
*/
public \<R extends Mutable<name>Collection> R select(<name>Predicate predicate, R target)
{
for (int i = 0; i \< this.size; i++)
{
<type> item = this.items[i];
if (predicate.accept(item))
{
result.add(item);
target.add(item);
}
}
return result;
return target;
}

public <name>ArrayList reject(<name>Predicate predicate)
{
<name>ArrayList result = new <name>ArrayList();
return this.reject(predicate, new <name>ArrayList());
}

/**
* @since 8.1.
*/
public \<R extends Mutable<name>Collection> R reject(<name>Predicate predicate, R target)
{
for (int i = 0; i \< this.size; i++)
{
<type> item = this.items[i];
if (!predicate.accept(item))
{
result.add(item);
target.add(item);
}
}
return result;
return target;
}

public <type> detectIfNone(<name>Predicate predicate, <type> ifNone)
Expand All @@ -626,7 +635,14 @@ public class <name>ArrayList extends Abstract<name>Iterable

public \<V> MutableList\<V> collect(<name>ToObjectFunction\<? extends V> function)
{
FastList\<V> target = FastList.newList(this.size);
return this.collect(function, FastList.newList(this.size));
}

/**
* @since 8.1.
*/
public \<V, R extends Collection\<V>\> R collect(<name>ToObjectFunction\<? extends V> function, R target)
{
for (int i = 0; i \< this.size; i++)
{
target.add(function.valueOf(this.items[i]));
Expand Down

0 comments on commit 52e29e1

Please sign in to comment.