KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > uk > co > jezuk > mango > algorithms > CountIf


1 package uk.co.jezuk.mango.algorithms;
2
3 import uk.co.jezuk.mango.iterators.SelectingIterator;
4 import java.util.Iterator JavaDoc;
5 /**
6  * <code>CountIf</code> is similar to <code>Count</code>, but more general.
7  * It computes the number of elements in the sequence which satisfy some condition.
8  * The condition is a described in the user-supplied <code>test</code> object, and
9  * <code>CountIf</code> computes the number of objects such that <code>test.test(o)</code>
10  * is <code>true</code>.
11  * @see Count
12  * @see CountIfNot
13  * @version $Id: CountIf.java 114 2006-09-28 21:17:57Z jez $
14  */

15 public class CountIf
16 {
17   static public int execute(java.util.Iterator JavaDoc iterator, uk.co.jezuk.mango.Predicate test)
18   {
19     if((iterator == null) || (test == null))
20       return 0;
21
22     int c = 0;
23     for(Iterator filter = new SelectingIterator(iterator, test);
24                 filter.hasNext();
25                 filter.next(), ++c);
26
27     return c;
28   } // execute
29

30   /////////////////////////////////////////////////////////
31
private CountIf() { }
32 } // CountIf
33
Popular Tags