de.matthiasmann.twl.utils
Class CallbackSupport

java.lang.Object
  extended by de.matthiasmann.twl.utils.CallbackSupport

public class CallbackSupport
extends java.lang.Object

Callback list management functions

The callback lists are implemented as arrays which are reallocated on each change. This allows to add/remove callbacks while the old list is used to invoke callbacks.

An empty list is represented by null instead of an empty array.


Method Summary
static
<T> T[]
addCallbackToList(T[] curList, T callback, java.lang.Class<T> clazz)
          Adds a new callback to the list.
static
<T> int
findCallbackPosition(T[] list, T callback)
          Locates a callback in the list.
static
<T extends java.lang.Enum<T>>
void
fireCallbacks(CallbackWithReason<?>[] callbacks, T reason)
          Executes all callbacks in the list.
static void fireCallbacks(java.lang.Runnable[] callbacks)
          Executes all callbacks in the list.
static
<T> T[]
removeCallbackFromList(T[] curList, int index)
          Removes a callback from the list.
static
<T> T[]
removeCallbackFromList(T[] curList, T callback)
          Removes a callback from the list.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

addCallbackToList

public static <T> T[] addCallbackToList(T[] curList,
                                        T callback,
                                        java.lang.Class<T> clazz)
Adds a new callback to the list.

Does not check if the callback is already in the list.

Type Parameters:
T - The type of the callback list
Parameters:
curList - the current callback list - can be null
callback - the callback to be added
clazz - the element class of the callback list used to allocate a new array
Returns:
a new callback list with the new callback appended at the end
Throws:
java.lang.NullPointerException - when callback or clazz is null

findCallbackPosition

public static <T> int findCallbackPosition(T[] list,
                                           T callback)
Locates a callback in the list.

Type Parameters:
T - The type of the callback list
Parameters:
list - the current callback list - can be null
callback - the callback to locate
Returns:
the index if found otherwise -1

removeCallbackFromList

public static <T> T[] removeCallbackFromList(T[] curList,
                                             int index)
Removes a callback from the list.

The new callback list will have the same element type as the passed in list.

Type Parameters:
T - The type of the callback list
Parameters:
curList - the current callback list
index - the index of the callback to remove
Returns:
the new callback list without the specified entry or null if it was the only entry
Throws:
java.lang.NullPointerException - when curList is null

removeCallbackFromList

public static <T> T[] removeCallbackFromList(T[] curList,
                                             T callback)
Removes a callback from the list.

The new callback list will have the same element type as the passed in list.

Type Parameters:
T - The type of the callback list
Parameters:
curList - the current callback list - can be null
callback - the callback to remove
Returns:
the passed in callback list (curList) when the callback was not found, the new callback list without the specified entry or null if it was the only entry

fireCallbacks

public static void fireCallbacks(java.lang.Runnable[] callbacks)
Executes all callbacks in the list.

Parameters:
callbacks - callback list - can be null

fireCallbacks

public static <T extends java.lang.Enum<T>> void fireCallbacks(CallbackWithReason<?>[] callbacks,
                                                               T reason)
Executes all callbacks in the list.

Type Parameters:
T - the type of the reason enum
Parameters:
callbacks - callback list - can be null
reason - the reson to pass to each callback