org.jutil.java.collections
Class BooleanAccumulator

java.lang.Object
  |
  +--org.jutil.java.collections.BooleanAccumulator
All Implemented Interfaces:
CollectionOperator
Direct Known Subclasses:
Exists, ForAll

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

A boolean accumulator for collections.

BooleanAccumulators process each element of a collection, and add the result of the process to the accumulator. The original collection remains unchanged.

Instead of an accumulate(Collection> method, it has an in(Collection) method. That name just seems more convenient.

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

Field Summary
static java.lang.String CVS_REVISION
           
 
Constructor Summary
BooleanAccumulator()
           
 
Method Summary
abstract  boolean accumulate(java.lang.Object element, boolean acc)
          public behavior

pre isValidElement(element);
 boolean 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 accumulating *);
abstract  boolean 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

BooleanAccumulator

public BooleanAccumulator()
Method Detail

initialAccumulator

public abstract boolean 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 boolean accumulate(Object element, boolean acc) for the first element.


accumulate

public abstract boolean accumulate(java.lang.Object element,
                                   boolean acc)
public behavior

pre isValidElement(element);

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 boolean in(java.util.Collection collection)
           throws java.util.ConcurrentModificationException
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 accumulating *);

Perform the accumulation defined in public boolean accumulate(Object element, boolean acc) for each element of . For the first element, the object returned by public boolean initialAccumulator() is used as accumulator. For the other elements, the result of the application of public boolean accumulate(Object element, boolean 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 boolean accumulate(Object element, boolean acc) on the last element of the collection to be processed.

The precondition is variable. Subclasses could strengthen the postcondition of isValidElement. To the user, this means that this method cannot be called safely in a polymorphic context, since she has no idea what the actual precondition will become in subclasses. This can be resolved by making the model method isValidElement final in the polymorph supertype she needs to use.

Parameters:
collection - The collection for which the ForAll operator has to be computed.