eu.webtoolkit.jwt
Class AbstractSignal

java.lang.Object
  extended by eu.webtoolkit.jwt.AbstractSignal
Direct Known Subclasses:
AbstractEventSignal, Signal, Signal1, Signal2, Signal3, Signal4, Signal5, Signal6

public abstract class AbstractSignal
extends java.lang.Object

Abstract base class for signals.

Signals are used to relay events to event listeners, implementing in this way the Observer pattern.


Nested Class Summary
static class AbstractSignal.Connection
          A signal connection.
 
Constructor Summary
protected AbstractSignal()
           
 
Method Summary
abstract  AbstractSignal.Connection addListener(WObject listenerOwner, Signal.Listener listener)
          Adds a listener for this signal.
protected  int getListenerCount()
          Returns the number of connected listeners.
protected  java.util.ArrayList<eu.webtoolkit.jwt.SignalImpl.Listener> getListeners()
          Returns the connected listeners.
 boolean isBlocked()
          Returns whether this signal is blocked.
 boolean isConnected()
          Returns whether at least one event listener has been connected to this signal.
abstract  void removeListener(Signal.Listener listener)
          Removes a listener.
 void setBlocked(boolean blocked)
          Blocks or unblocks this signal.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AbstractSignal

protected AbstractSignal()
Method Detail

isConnected

public boolean isConnected()
Returns whether at least one event listener has been connected to this signal.

Returns:
whether at least one event listener has been connected to this signal.

getListenerCount

protected int getListenerCount()
Returns the number of connected listeners.

Returns:
the number of connected listeners.

getListeners

protected java.util.ArrayList<eu.webtoolkit.jwt.SignalImpl.Listener> getListeners()
Returns the connected listeners.

Returns:
the connected listeners.

isBlocked

public boolean isBlocked()
Returns whether this signal is blocked.

Returns:
whether this signal is blocked.
See Also:
setBlocked(boolean)

setBlocked

public void setBlocked(boolean blocked)
Blocks or unblocks this signal.

While a signal is blocked, it will not trigger any of its connected event listeners.

Parameters:
blocked -

addListener

public abstract AbstractSignal.Connection addListener(WObject listenerOwner,
                                                      Signal.Listener listener)
Adds a listener for this signal.

Each listener will be notified when the signal is triggered.

An owner object may be passed when the listener is implemented using an (anonymous) inner class. In that case the owner object should be the enclosing object of the listener object, and this is used to bind the lifetime of the listener. To avoid the owner object from not being garbage collected when it is no longer used, only the owner object will add a reference to the listener, while the signal will use a weak reference.

This avoids the most common reason for memory leaks in Java implementations of the Observer pattern: the owner object will not get garbage collected because of the (anonymous) listener object having a reference to it, even if the receiver object is no longer referenced from anywhere. When the owner object is not null, the listener is stored using a strong reference in the owner object, and using a weak reference in the signal.

Parameters:
listenerOwner - if not null, the enclosing object for a listener implemented using an inner class
listener - the listener
Returns:
a connection object that may be used to control the connection

removeListener

public abstract void removeListener(Signal.Listener listener)
Removes a listener.

Parameters:
listener - a listener that was previously added.