commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bugzi...@apache.org
Subject DO NOT REPLY [Bug 9422] New: - Extending CollectionsUtil with methods #exists and #forAll
Date Sun, 26 May 2002 08:55:41 GMT
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:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message