org.limitless.util
Class GeneralEventSource

java.lang.Object
  extended by org.limitless.util.GeneralEventSource

public class GeneralEventSource
extends java.lang.Object

This class provides a simple way to ensure that an event source can handle all the queue management. Specifically, this handles the Observer's Subscriber aspect for Java's event model. It lets us add and remove listeners. It also allows us to discard all the listeners. To avoid ConcurrentModification exceptions, any changes to the list of listeners is queued during iterations in order and will be applied at the completion of iteration using the Command pattern.


Nested Class Summary
static interface GeneralEventSource.Invoke
          A listener must implement this if it wants to be invoked by this source and requires no exceptions be thrown.
static interface GeneralEventSource.InvokeWithException
          A listener must implement this if it wants to be invoked by this source and requires exceptions be thrown.
private  class GeneralEventSource.QueuedEvent
           
 
Field Summary
private  boolean isIterating
          This is set true when iterating, and will cause requests to add, remove, or clear to be "queued up" and carried out after the iteration completes.
private  java.util.Collection listeners
          The master listener collection.
private  java.util.LinkedList queuedEvents
          This holds QueuedEvent objects so we can "replay" at the end of an iteration what was requested.
 
Constructor Summary
GeneralEventSource()
          Create the basic object.
 
Method Summary
 void add(java.util.EventListener newListener)
          Call this to add a listener for invocations.
 void clear()
          Discard all listeners.
private  void processQueuedEvents()
           
 void remove(java.util.EventListener listenerToRemove)
          Take a particular listener out of the set.
 void visit(GeneralEventSource.Invoke functor)
          This causes an invoker to be called once per object in the set.
 void visit(GeneralEventSource.InvokeWithException functor)
          This causes an invoker to be called once per object in the set.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

listeners

private java.util.Collection listeners
The master listener collection.


isIterating

private boolean isIterating
This is set true when iterating, and will cause requests to add, remove, or clear to be "queued up" and carried out after the iteration completes.


queuedEvents

private java.util.LinkedList queuedEvents
This holds QueuedEvent objects so we can "replay" at the end of an iteration what was requested. I chose LinkedList to ensure that I can manage this as a proper queue.

Constructor Detail

GeneralEventSource

public GeneralEventSource()
Create the basic object.

Method Detail

add

public void add(java.util.EventListener newListener)
Call this to add a listener for invocations.

Parameters:
newListener - listener to add (duplicates harmless)

remove

public void remove(java.util.EventListener listenerToRemove)
Take a particular listener out of the set.

Parameters:
listenerToRemove -

visit

public void visit(GeneralEventSource.Invoke functor)
This causes an invoker to be called once per object in the set. Note that this does NOT thread out separate calls!

Parameters:
functor - Object to be invoked per entry in the set.

visit

public void visit(GeneralEventSource.InvokeWithException functor)
           throws java.lang.Exception
This causes an invoker to be called once per object in the set. Note that this does NOT thread out separate calls!

Parameters:
functor - Object to be invoked per entry in the set.
Throws:
java.lang.Exception

clear

public void clear()
Discard all listeners.


processQueuedEvents

private void processQueuedEvents()


Copyright © 1995, 2004 Limitless Knowledge Association, All Rights Reserved.