KickJava   Java API By Example, From Geeks To Geeks.

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


1 package uk.co.jezuk.mango.algorithms;
2
3 /**
4  * <code>Count</code> computes the number of elements in the sequence that
5  * are equal to <code>value</code>. <br>
6  * <code>value</code> may be <code>null</code>.<br>
7  * The objects in the sequence and <code>value</code> must be comparable using
8  * <code>Object.equals</code> (unless <code>value</code> is <code>null</code>).
9  * @see CountIf
10  * @version $Id: Count.java 50 2002-06-11 15:21:40Z jez $
11  */

12 public class Count
13 {
14   static public int execute(java.util.Iterator JavaDoc iterator, Object JavaDoc value)
15   {
16     if(iterator == null)
17       return 0;
18
19     if(value == null)
20       return execute_null(iterator);
21
22     int c = 0;
23     while(iterator.hasNext())
24       if(value.equals(iterator.next()))
25     ++c;
26
27     return c;
28   } // execute
29

30   static private int execute_null(java.util.Iterator JavaDoc iterator)
31   {
32     int c = 0;
33       
34     while(iterator.hasNext())
35       if(iterator.next() == null)
36     ++c;
37     return c;
38   } // executer_null
39

40   private Count() { }
41 } // Count
42
Popular Tags