org.jutil.java.collections
Class IntegerAccumulator

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

public abstract class IntegerAccumulator
extends java.lang.Object
implements CollectionOperator

An integer accumulator for collections.

IntegerAccumulators process each element of a collection, and update an accumulator while doing so. The original collection remains unchanged.

Version:
$Revision: 1.8 $
Author:
Jan Dockx, Marko van Dooren

Field Summary
static java.lang.String CVS_REVISION
           
 
Constructor Summary
IntegerAccumulator()
           
 
Method Summary
abstract  int accumulate(java.lang.Object element, int acc)
          // The given element may not be null
pre element != null;
 int in(java.util.Collection collection)
          public behavior

pre (\forall Object o, collection.contains(o); isValidElement(o));

post (* the result of the accumulation is returned *);
post collection == null => \result == initialAccumulator();

signals (ConcurrentModificationException) (* The collection was modified while filtering *);
abstract  int initialAccumulator()
          Subclasses should implement this method to return the initialized accumulator.
 
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

IntegerAccumulator

public IntegerAccumulator()
Method Detail

initialAccumulator

public abstract int initialAccumulator()

Subclasses should implement this method to return the initialized accumulator. This method is called at the start of the accumulation. The result will be used in the application of public int accumulate(Object element, int acc) for the first element.


accumulate

public abstract int accumulate(java.lang.Object element,
                               int acc)
// The given element may not be null
pre element != null;

This method is called for each element in the collection we are accumulating. Subclasses should implement this method to process and accumulate the result in .

The result is the accumulator that will be used for the next element of the collection to process.

.
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.

in

public int in(java.util.Collection collection)
public behavior

pre (\forall Object o, collection.contains(o); isValidElement(o));

post (* the result of the accumulation is returned *);
post collection == null => \result == initialAccumulator();

signals (ConcurrentModificationException) (* The collection was modified while filtering *);

Perform the accumulation defined in public int accumulate(Object element, int acc) for each element of . For the first element, the object returned by public int initialAccumulator() is used as accumulator. For the other elements, the result of the application of public int accumulate(Object element, int acc) on the previous element is used as accumulator.

The contents of is not changed.

The result of this method is the object returned by the application of public int accumulate(Object element, int acc) on the last element of the collection to be processed.

Parameters:
collection - The collection to perform this accumulation on. It will not be changed. This can be null, in which the initial accumulator is returned.