You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by bu...@apache.org on 2002/05/26 10:55:41 UTC
DO NOT REPLY [Bug 9422] New: -
Extending CollectionsUtil with methods #exists and #forAll
DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://nagoya.apache.org/bugzilla/show_bug.cgi?id=9422>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND
INSERTED IN THE BUG DATABASE.
http://nagoya.apache.org/bugzilla/show_bug.cgi?id=9422
Extending CollectionsUtil with methods #exists and #forAll
Summary: Extending CollectionsUtil with methods #exists and
#forAll
Product: Commons
Version: unspecified
Platform: Other
OS/Version: Other
Status: NEW
Severity: Enhancement
Priority: Other
Component: Collections
AssignedTo: commons-dev@jakarta.apache.org
ReportedBy: BluePhelix@web.de
I suggest two new methods to be integrated into CollectionsUtil. They tell us
whether one or all objects of a collection fulfill a given predicate.
/** Answers true if a predicate is true for all elements of a collection
*
* @return whether each element of the collection matches the predicate or
false if one element doesn't match
*/
public static boolean forAll( Collection inputCollection, Predicate
predicate );
/** Answers true if a predicate is true for at least one predicate of a
collection
*
* @return whether at least one element of the collection matches the
predicate or false if none could be found
*/
public static boolean exists( Collection inputCollection, Predicate
predicate );
-----------------------------------------------
A possible implementation could look like this:
/** Selects all elements from inputCollection which don't match the given
predicate
* into an output collection.
*/
public static Collection reject( Collection inputCollection, Predicate
predicate ) {
ArrayList answer = new ArrayList( inputCollection.size() );
reject( inputCollection, predicate, answer );
return answer;
}
/** Selects all elements from inputCollection which don't match the given
predicate
* and adds them to outputCollection.
*/
public static void reject( Collection inputCollection, Predicate predicate,
Collection outputCollection ) {
if ( inputCollection != null && predicate != null ) {
for ( Iterator iter = inputCollection.iterator(); iter.hasNext(); )
{
Object item = iter.next();
if ( !predicate.evaluate( item ) ) {
outputCollection.add( item );
}
}
}
}
/** Answers true if a predicate is true for at least one predicate of a
collection
*
* @return whether at least one element of the collection matches the
predicate or false if none could be found
*/
public static boolean exists( Collection inputCollection, Predicate
predicate ) {
return find ( inputCollection, predicate ) != null;
}
/** Answers true if a predicate is true for all elements of a collection
*
* @return whether each element of the collection matches the predicate or
false if one element doesn't match
*/
public static boolean forAll( Collection inputCollection, Predicate
predicate ) {
return reject ( inputCollection, predicate ).isEmpty();
}
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>