org.jutil.java.collections
Class Counter

java.lang.Object
  |
  +--org.jutil.java.collections.IntegerAccumulator
        |
        +--org.jutil.java.collections.Counter
All Implemented Interfaces:
CollectionOperator

public abstract class Counter
extends IntegerAccumulator

An IntegerAccumulator that counts the number of items in a collection that satisfies some criterion.

A convenience accumulator of collections that checks elements of a collection satisfy the criterion defined in public boolean criterion(Object element).

As with the accumulator, this class can best be used as an anonymous inner class, as shown below.


 int number = new Counter() {
                public boolean criterion(Object element) {
                  // criterion code
                }
              }.in(collection);
 

Version:
$Revision: 1.4 $
Author:
Marko van Dooren

Field Summary
static java.lang.String CVS_REVISION
           
 
Constructor Summary
Counter()
           
 
Method Summary
 int accumulate(java.lang.Object element, int acc)
          also public behavior

// If the element satisfies the criterion, acc + 1 is returned to
// indicate 1 more element satisfies the criterion.
// otherwise acc is returned.
post criterion(element) == false ==> \result == acc;
post criterion(element) == true ==> \result == acc + 1;
abstract  boolean criterion(java.lang.Object element)
          //As usual, criterion must be consistent with the semantics of equals
post (\forall Object o1;;
(\forall Object o2; o1.equals(o2);
criterion(o1) == criterion(o2)));
The criterion used to determine whether or not an element has to be counted.
 int initialAccumulator()
          also public behavior

post \result == 0;
 
Methods inherited from class org.jutil.java.collections.IntegerAccumulator
in
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CVS_REVISION

public static final java.lang.String CVS_REVISION
Constructor Detail

Counter

public Counter()
Method Detail

initialAccumulator

public int initialAccumulator()
also public behavior

post \result == 0;
Overrides:
initialAccumulator in class IntegerAccumulator

accumulate

public int accumulate(java.lang.Object element,
                      int acc)
also public behavior

// If the element satisfies the criterion, acc + 1 is returned to
// indicate 1 more element satisfies the criterion.
// otherwise acc is returned.
post criterion(element) == false ==> \result == acc;
post criterion(element) == true ==> \result == acc + 1;
Overrides:
accumulate in class IntegerAccumulator
Following copied from class: org.jutil.java.collections.IntegerAccumulator
Parameters:
element - The object the method will process and change the accumulator with.
acc - The accumulator for the accumulation. For the first element to be processed, the result of initialAccumulator is used. For the other elements, the result of this method applied on the previous element is used.

criterion

public abstract boolean criterion(java.lang.Object element)
//As usual, criterion must be consistent with the semantics of equals
post (\forall Object o1;;
(\forall Object o2; o1.equals(o2);
criterion(o1) == criterion(o2)));
The criterion used to determine whether or not an element has to be counted.