Package groovy.util

# Class ObservableSet<E>

java.lang.Object
groovy.util.ObservableSet<E>

All Implemented Interfaces:
  `Iterable<E>`, `Collection<E>`, `Set<E>`

public class ObservableSet<E> extends Object implements Set<E>
Set decorator that will trigger PropertyChangeEvents when a value changes.
An optional Closure may be specified and will work as a filter, if it returns true the property will trigger an event (if the value indeed changed), otherwise it won't. The Closure may receive 1 or 2 parameters, the single one being the value, the other one both the key and value, for example:

     // skip all properties whose value is a closure
     def set = new ObservableSet( {!(it instanceof Closure)} )
     <p/>
     // skip all properties whose name matches a regex
     def set = new ObservableSet( { name, value -> !(name =˜ /[A-Z+]/) } )

The current implementation will trigger specialized events in the following scenarios, you need not register a different listener as those events extend from PropertyChangeEvent

- ObservableSet.ElementAddedEvent - a new element is added to the set
- ObservableSet.ElementRemovedEvent - an element is removed from the set
- ObservableSet.ElementUpdatedEvent - an element changes value (same as regular PropertyChangeEvent)
- ObservableSet.ElementClearedEvent - all elements have been removed from the list
- ObservableSet.MultiElementAddedEvent - triggered by calling set.addAll()
- ObservableSet.MultiElementRemovedEvent - triggered by calling set.removeAll()/set.retainAll()

Bound properties

- `content` - read-only.
- `size` - read-only.

- ## Nested Class Summary

  Nested Classes
  Modifier and Type
  Class
  Description
  `static enum`
  `ObservableSet.ChangeType`
  Enumerates the specialized set change events.
  `static class`
  `ObservableSet.ElementAddedEvent`
  Event fired when one element is added.
  `static class`
  `ObservableSet.ElementClearedEvent`
  Event fired when the set is cleared.
  `static class`
  `ObservableSet.ElementEvent`
  Base event type for observable set content changes.
  `static class`
  `ObservableSet.ElementRemovedEvent`
  Event fired when one element is removed.
  `static class`
  `ObservableSet.MultiElementAddedEvent`
  Event fired when multiple elements are added.
  `static class`
  `ObservableSet.MultiElementRemovedEvent`
  Event fired when multiple elements are removed.
  `protected class`
  `ObservableSet.ObservableIterator<E>`
  Iterator wrapper that reports removals as observable set events.

- ## Field Summary

  Fields
  Modifier and Type
  Field
  Description
  `static final String`
  `CONTENT_PROPERTY`
  Bound property name for set content changes.
  `static final String`
  `SIZE_PROPERTY`
  Bound property name for set size changes.

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `ObservableSet()`
  Creates an observable set backed by a `HashSet`.
  `ObservableSet(Closure test)`
  Creates an observable set backed by a `HashSet`.
  `ObservableSet(Set<E> delegate)`
  Creates an observable set backed by the supplied delegate.
  `ObservableSet(Set<E> delegate, Closure test)`
  Creates an observable set backed by the supplied delegate.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `boolean`
  `add(E e)`
  `boolean`
  `addAll(Collection<? extends E> c)`
  `void`
  `addPropertyChangeListener(PropertyChangeListener listener)`
  Registers a listener for all observable set events.
  `void`
  `addPropertyChangeListener(String propertyName, PropertyChangeListener listener)`
  Registers a listener for a named bound property.
  `void`
  `clear()`
  `boolean`
  `contains(Object o)`
  `boolean`
  `containsAll(Collection<?> objects)`
  `protected void`
  `fireElementAddedEvent(Object element)`
  Fires a single-element added event.
  `protected void`
  `fireElementClearedEvent(List values)`
  Fires a cleared event containing removed values.
  `protected void`
  `fireElementEvent(ObservableSet.ElementEvent event)`
  Publishes an element event to registered listeners.
  `protected void`
  `fireElementRemovedEvent(Object element)`
  Fires a single-element removed event.
  `protected void`
  `fireMultiElementAddedEvent(List values)`
  Fires a multi-element added event.
  `protected void`
  `fireMultiElementRemovedEvent(List values)`
  Fires a multi-element removed event.
  `protected void`
  `fireSizeChangedEvent(int oldValue, int newValue)`
  Fires the bound size change event.
  `Set<E>`
  `getContent()`
  Returns an unmodifiable snapshot view of the backing set.
  `protected Set<E>`
  `getDelegateSet()`
  Returns the mutable backing set.
  `PropertyChangeListener[]`
  `getPropertyChangeListeners()`
  Returns listeners registered for all properties.
  `PropertyChangeListener[]`
  `getPropertyChangeListeners(String propertyName)`
  Returns listeners registered for a named property.
  `protected Closure`
  `getTest()`
  Returns the optional event filter closure.
  `boolean`
  `hasListeners(String propertyName)`
  Reports whether listeners are registered for a named property.
  `boolean`
  `isEmpty()`
  `Iterator<E>`
  `iterator()`
  `boolean`
  `remove(Object o)`
  `boolean`
  `removeAll(Collection<?> c)`
  `void`
  `removePropertyChangeListener(PropertyChangeListener listener)`
  Removes a listener registered for all properties.
  `void`
  `removePropertyChangeListener(String propertyName, PropertyChangeListener listener)`
  Removes a listener registered for a named property.
  `boolean`
  `retainAll(Collection<?> c)`
  `int`
  `size()`
  `Object[]`
  `toArray()`
  `<T> T[]`
  `toArray(T[] ts)`

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

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

  ### Methods inherited from interface java.util.Collection

  `parallelStream, removeIf, stream, toArray`

  ### Methods inherited from interface java.lang.Iterable

  `forEach`

  ### Methods inherited from interface java.util.Set

  `equals, hashCode, spliterator`

- ## Field Details

  - ### SIZE_PROPERTY

    public static final String SIZE_PROPERTY
    Bound property name for set size changes.
    See Also:
      - Constant Field Values

  - ### CONTENT_PROPERTY

    public static final String CONTENT_PROPERTY
    Bound property name for set content changes.
    See Also:
      - Constant Field Values

- ## Constructor Details

  - ### ObservableSet

    public ObservableSet()
    Creates an observable set backed by a `HashSet`.

  - ### ObservableSet

    public ObservableSet(Set<E> delegate)
    Creates an observable set backed by the supplied delegate.
    Parameters:
      `delegate` - the backing set

  - ### ObservableSet

    public ObservableSet(Closure test)
    Creates an observable set backed by a `HashSet`.
    Parameters:
      `test` - optional event filter

  - ### ObservableSet

    public ObservableSet(Set<E> delegate, Closure test)
    Creates an observable set backed by the supplied delegate.
    Parameters:
      `delegate` - the backing set
      `test` - optional event filter

- ## Method Details

  - ### getContent

    public Set<E> getContent()
    Returns an unmodifiable snapshot view of the backing set.
    Returns:
      the set content

  - ### getDelegateSet

    protected Set<E> getDelegateSet()
    Returns the mutable backing set.
    Returns:
      the delegate set

  - ### getTest

    protected Closure getTest()
    Returns the optional event filter closure.
    Returns:
      the event filter, or `null`

  - ### fireElementAddedEvent

    protected void fireElementAddedEvent(Object element)
    Fires a single-element added event.

  - ### fireMultiElementAddedEvent

    protected void fireMultiElementAddedEvent(List values)
    Fires a multi-element added event.

  - ### fireElementClearedEvent

    protected void fireElementClearedEvent(List values)
    Fires a cleared event containing removed values.

  - ### fireElementRemovedEvent

    protected void fireElementRemovedEvent(Object element)
    Fires a single-element removed event.

  - ### fireMultiElementRemovedEvent

    protected void fireMultiElementRemovedEvent(List values)
    Fires a multi-element removed event.

  - ### fireElementEvent

    protected void fireElementEvent(ObservableSet.ElementEvent event)
    Publishes an element event to registered listeners.

  - ### fireSizeChangedEvent

    protected void fireSizeChangedEvent(int oldValue, int newValue)
    Fires the bound size change event.

  - ### addPropertyChangeListener

    public void addPropertyChangeListener(PropertyChangeListener listener)
    Registers a listener for all observable set events.
    Parameters:
      `listener` - the listener to add

  - ### addPropertyChangeListener

    public void addPropertyChangeListener(String propertyName, PropertyChangeListener listener)
    Registers a listener for a named bound property.
    Parameters:
      `propertyName` - the property to observe
      `listener` - the listener to add

  - ### getPropertyChangeListeners

    public PropertyChangeListener[] getPropertyChangeListeners()
    Returns listeners registered for all properties.
    Returns:
      the registered listeners

  - ### getPropertyChangeListeners

    public PropertyChangeListener[] getPropertyChangeListeners(String propertyName)
    Returns listeners registered for a named property.
    Parameters:
      `propertyName` - the observed property name
    Returns:
      the registered listeners

  - ### removePropertyChangeListener

    public void removePropertyChangeListener(PropertyChangeListener listener)
    Removes a listener registered for all properties.
    Parameters:
      `listener` - the listener to remove

  - ### removePropertyChangeListener

    public void removePropertyChangeListener(String propertyName, PropertyChangeListener listener)
    Removes a listener registered for a named property.
    Parameters:
      `propertyName` - the observed property name
      `listener` - the listener to remove

  - ### hasListeners

    public boolean hasListeners(String propertyName)
    Reports whether listeners are registered for a named property.
    Parameters:
      `propertyName` - the property name to inspect
    Returns:
      `true` if listeners are registered

  - ### size

    public int size()
    Specified by:
      `size` in interface `Collection<E>`
    Specified by:
      `size` in interface `Set<E>`

  - ### isEmpty

    public boolean isEmpty()
    Specified by:
      `isEmpty` in interface `Collection<E>`
    Specified by:
      `isEmpty` in interface `Set<E>`

  - ### contains

    public boolean contains(Object o)
    Specified by:
      `contains` in interface `Collection<E>`
    Specified by:
      `contains` in interface `Set<E>`

  - ### iterator

    public Iterator<E> iterator()
    Specified by:
      `iterator` in interface `Collection<E>`
    Specified by:
      `iterator` in interface `Iterable<E>`
    Specified by:
      `iterator` in interface `Set<E>`

  - ### toArray

    public Object[] toArray()
    Specified by:
      `toArray` in interface `Collection<E>`
    Specified by:
      `toArray` in interface `Set<E>`

  - ### toArray

    public <T> T[] toArray(T[] ts)
    Specified by:
      `toArray` in interface `Collection<E>`
    Specified by:
      `toArray` in interface `Set<E>`

  - ### add

    public boolean add(E e)
    Specified by:
      `add` in interface `Collection<E>`
    Specified by:
      `add` in interface `Set<E>`

  - ### remove

    public boolean remove(Object o)
    Specified by:
      `remove` in interface `Collection<E>`
    Specified by:
      `remove` in interface `Set<E>`

  - ### containsAll

    public boolean containsAll(Collection<?> objects)
    Specified by:
      `containsAll` in interface `Collection<E>`
    Specified by:
      `containsAll` in interface `Set<E>`

  - ### addAll

    public boolean addAll(Collection<? extends E> c)
    Specified by:
      `addAll` in interface `Collection<E>`
    Specified by:
      `addAll` in interface `Set<E>`

  - ### retainAll

    public boolean retainAll(Collection<?> c)
    Specified by:
      `retainAll` in interface `Collection<E>`
    Specified by:
      `retainAll` in interface `Set<E>`

  - ### removeAll

    public boolean removeAll(Collection<?> c)
    Specified by:
      `removeAll` in interface `Collection<E>`
    Specified by:
      `removeAll` in interface `Set<E>`

  - ### clear

    public void clear()
    Specified by:
      `clear` in interface `Collection<E>`
    Specified by:
      `clear` in interface `Set<E>`