Package ghidra.util.datastruct

# Class ListenerSet<T>

java.lang.Object
ghidra.util.datastruct.ListenerSet<T>

Type Parameters:
  `T` - the listener type

public class ListenerSet<T> extends Object
A data structure meant to be used to hold listeners. This class has a few benefits:

- Clients supply the class of the listeners being stored. Then, clients make use of a Java `Proxy` object to sends events by calling the desired method directly on the proxy.
- This class is thread safe, allowing adding and removing listeners while events are being fired.
- Weak or strong references may be used seamlessly by passing the correct constructor value.

Some restrictions:

- Exception handling is currently done by storing the first exception encountered while processing events. Any exception encountered while notifying a listener does not stop follow-on listeners from getting notified.
- Listener classes are restricted to using methods with a void return type, as there is currently no way to return values back to the client when notifying.
- The insertion order of listeners is not maintained, which means that event notification may take place in an arbitrary order.

An example use of this class to fire events could look like this:

         ListenerSet<ActionListener> listeners = new ListenerSet(ActionListener.class);
         ActionEvent event = new ActionEvent(this, 1, "Event");
         listeners.invoke().actionPerformed(event);

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `ListenerSet(Class<T> iface, boolean isWeak)`
  Constructs a listener set that is backed by weak references.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `boolean`
  `add(T e)`
  `void`
  `clear()`
  `T`
  `getProxy()`
  Returns the proxy used by this class.
  `T`
  `invoke()`
  Returns the proxy object.
  `boolean`
  `remove(T e)`
  `void`
  `setErrorHandler(ListenerErrorHandler errorHandler)`
  `int`
  `size()`
  `String`
  `toString()`

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

- ## Constructor Details

  - ### ListenerSet

    public ListenerSet(Class<T> iface, boolean isWeak)
    Constructs a listener set that is backed by weak references.
    Parameters:
      `iface` - the listener class type.
      `isWeak` - true signals to use weak storage for the listeners. If using weak storage, clients must keep a reference to the listener or it will eventually be removed from this data structure when garbage collected.

- ## Method Details

  - ### invoke

    public T invoke()
    Returns the proxy object. Using this is the same as calling `getProxy()`. Use this method to make the client call more readable.
    Returns:
      the proxy

  - ### getProxy

    public T getProxy()
    Returns the proxy used by this class. Using `invoke()` is preferred for better readability.
    Returns:
      the proxy

  - ### toString

    public String toString()
    Overrides:
      `toString` in class `Object`

  - ### add

    public boolean add(T e)

  - ### remove

    public boolean remove(T e)

  - ### clear

    public void clear()

  - ### size

    public int size()

  - ### setErrorHandler

    public void setErrorHandler(ListenerErrorHandler errorHandler)