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.
  • Field Details

    • SIZE_PROPERTY

      public static final String SIZE_PROPERTY
      Bound property name for set size changes.
      See Also:
    • CONTENT_PROPERTY

      public static final String CONTENT_PROPERTY
      Bound property name for set content changes.
      See Also:
  • 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>