01: package uk.co.jezuk.mango.algorithms;
02:
03: import uk.co.jezuk.mango.iterators.SelectingIterator;
04: import java.util.Iterator;
05:
06: /**
07: * <code>CountIf</code> is similar to <code>Count</code>, but more general.
08: * It computes the number of elements in the sequence which satisfy some condition.
09: * The condition is a described in the user-supplied <code>test</code> object, and
10: * <code>CountIf</code> computes the number of objects such that <code>test.test(o)</code>
11: * is <code>true</code>.
12: * @see Count
13: * @see CountIfNot
14: * @version $Id: CountIf.java 114 2006-09-28 21:17:57Z jez $
15: */
16: public class CountIf {
17: static public int execute(java.util.Iterator iterator,
18: uk.co.jezuk.mango.Predicate test) {
19: if ((iterator == null) || (test == null))
20: return 0;
21:
22: int c = 0;
23: for (Iterator filter = new SelectingIterator(iterator, test); filter
24: .hasNext(); filter.next(), ++c)
25: ;
26:
27: return c;
28: } // execute
29:
30: /////////////////////////////////////////////////////////
31: private CountIf() {
32: }
33: } // CountIf
|