Class AbstractObservableList<E>

  • Type Parameters:
    E - the list element type
    All Implemented Interfaces:
    java.lang.Iterable<E>, java.util.Collection<E>, java.util.List<E>, IObservable, IObservableCollection<E>, IObservableList<E>
    Direct Known Subclasses:
    ComputedList, MultiList

    public abstract class AbstractObservableList<E>
    extends java.util.AbstractList<E>
    implements IObservableList<E>
    Subclasses should override at least get(int index) and size().

    This class is thread safe. All state accessing methods must be invoked from the current realm. Methods for adding and removing listeners may be invoked from any thread.

    Since:
    1.0
    Implementation Note:
    If methods are added to the interface which this class implements then implementations of those methods must be added to this class.
    • Constructor Detail

      • AbstractObservableList

        public AbstractObservableList​(Realm realm)
        Parameters:
        realm - the realm; not null
      • AbstractObservableList

        public AbstractObservableList()
    • Method Detail

      • hasListeners

        protected boolean hasListeners()
        Returns whether this observable list has any registered listeners.
        Returns:
        whether this observable list has any registered listeners.
        Since:
        1.2
      • isStale

        public boolean isStale()
        Description copied from interface: IObservable
        Returns whether the state of this observable is stale and is expected to change soon. A non-stale observable that becomes stale will notify its stale listeners. A stale object that becomes non-stale does so by changing its state and notifying its change listeners, it does not notify its stale listeners about becoming non-stale. Clients that do not expect asynchronous changes may ignore staleness of observable objects.
        Specified by:
        isStale in interface IObservable
        Returns:
        true if this observable's state is stale and will change soon.
      • addListChangeListener

        public void addListChangeListener​(IListChangeListener<? super E> listener)
        Description copied from interface: IObservableList
        Adds the given list change listener to the list of list change listeners.
        Specified by:
        addListChangeListener in interface IObservableList<E>
        Parameters:
        listener - the change listener to add; not null
      • removeListChangeListener

        public void removeListChangeListener​(IListChangeListener<? super E> listener)
        Description copied from interface: IObservableList
        Removes the given list change listener from the list of list change listeners. Has no effect if the given listener is not registered as a list change listener.
        Specified by:
        removeListChangeListener in interface IObservableList<E>
        Parameters:
        listener - the change listener to remove; not null
      • fireListChange

        protected void fireListChange​(ListDiff<E> diff)
      • addChangeListener

        public void addChangeListener​(IChangeListener listener)
        Description copied from interface: IObservable
        Adds the given change listener to the list of change listeners. Change listeners are notified about changes of the state of this observable in a generic way, without specifying the change that happened. To get the changed state, a change listener needs to query for the current state of this observable.
        Specified by:
        addChangeListener in interface IObservable
        Parameters:
        listener - the listener to add; not null
      • removeChangeListener

        public void removeChangeListener​(IChangeListener listener)
        Description copied from interface: IObservable
        Removes the given change listener from the list of change listeners. Has no effect if the given listener is not registered as a change listener.
        Specified by:
        removeChangeListener in interface IObservable
        Parameters:
        listener - the listener to remove; not null
      • addStaleListener

        public void addStaleListener​(IStaleListener listener)
        Description copied from interface: IObservable
        Adds the given stale listener to the list of stale listeners. Stale listeners are notified when an observable object becomes stale, not when is becomes non-stale.
        Specified by:
        addStaleListener in interface IObservable
        Parameters:
        listener - the listener to add; not null
        See Also:
        IObservable.isStale()
      • removeStaleListener

        public void removeStaleListener​(IStaleListener listener)
        Description copied from interface: IObservable
        Removes the given stale listener from the list of stale listeners. Has no effect if the given listener is not registered as a stale listener.
        Specified by:
        removeStaleListener in interface IObservable
        Parameters:
        listener - the listener to remove; not null
      • addDisposeListener

        public void addDisposeListener​(IDisposeListener listener)
        Description copied from interface: IObservable
        Adds the given dispose listener to the list of dispose listeners. Dispose listeners are notified when an observable has been disposed.
        Specified by:
        addDisposeListener in interface IObservable
        Parameters:
        listener - the listener to add
        Since:
        1.2
      • removeDisposeListener

        public void removeDisposeListener​(IDisposeListener listener)
        Description copied from interface: IObservable
        Removes the given dispose listener from the list of dispose listeners. Has no effect if the given listener is not registered as a dispose listener.
        Specified by:
        removeDisposeListener in interface IObservable
        Parameters:
        listener - the listener to remove
        Since:
        1.2
      • fireChange

        protected void fireChange()
        Fires change event. Must be invoked from the current realm.
      • fireStale

        protected void fireStale()
        Fires stale event. Must be invoked from the current realm.
      • firstListenerAdded

        protected void firstListenerAdded()
      • lastListenerRemoved

        protected void lastListenerRemoved()
      • isDisposed

        public boolean isDisposed()
        Description copied from interface: IObservable
        Returns whether the observable has been disposed
        Specified by:
        isDisposed in interface IObservable
        Returns:
        whether the observable has been disposed
        Since:
        1.2
      • dispose

        public void dispose()
        Description copied from interface: IObservable
        Disposes of this observable object, removing all listeners registered with this object, and all listeners this object might have registered on other objects.
        Specified by:
        dispose in interface IObservable
      • size

        public final int size()
        Specified by:
        size in interface java.util.Collection<E>
        Specified by:
        size in interface IObservableList<E>
        Specified by:
        size in interface java.util.List<E>
        Specified by:
        size in class java.util.AbstractCollection<E>
      • doGetSize

        protected abstract int doGetSize()
        Returns:
        the size
      • isEmpty

        public boolean isEmpty()
        Specified by:
        isEmpty in interface java.util.Collection<E>
        Specified by:
        isEmpty in interface IObservableList<E>
        Specified by:
        isEmpty in interface java.util.List<E>
        Overrides:
        isEmpty in class java.util.AbstractCollection<E>
      • contains

        public boolean contains​(java.lang.Object o)
        Specified by:
        contains in interface java.util.Collection<E>
        Specified by:
        contains in interface IObservableList<E>
        Specified by:
        contains in interface java.util.List<E>
        Overrides:
        contains in class java.util.AbstractCollection<E>
      • iterator

        public java.util.Iterator<E> iterator()
        Specified by:
        iterator in interface java.util.Collection<E>
        Specified by:
        iterator in interface IObservableList<E>
        Specified by:
        iterator in interface java.lang.Iterable<E>
        Specified by:
        iterator in interface java.util.List<E>
        Overrides:
        iterator in class java.util.AbstractList<E>
      • toArray

        public java.lang.Object[] toArray()
        Specified by:
        toArray in interface java.util.Collection<E>
        Specified by:
        toArray in interface IObservableList<E>
        Specified by:
        toArray in interface java.util.List<E>
        Overrides:
        toArray in class java.util.AbstractCollection<E>
      • toArray

        public <T> T[] toArray​(T[] a)
        Specified by:
        toArray in interface java.util.Collection<E>
        Specified by:
        toArray in interface IObservableList<E>
        Specified by:
        toArray in interface java.util.List<E>
        Overrides:
        toArray in class java.util.AbstractCollection<E>
      • add

        public boolean add​(E o)
        Specified by:
        add in interface java.util.Collection<E>
        Specified by:
        add in interface IObservableList<E>
        Specified by:
        add in interface java.util.List<E>
        Overrides:
        add in class java.util.AbstractList<E>
      • move

        public E move​(int oldIndex,
                      int newIndex)
        Moves the element located at oldIndex to newIndex. This method is equivalent to calling add(newIndex, remove(oldIndex)).

        Subclasses should override this method to deliver list change notification for the remove and add operations in the same ListChangeEvent, as this allows ListDiff.accept(ListDiffVisitor) to recognize the operation as a move.

        Specified by:
        move in interface IObservableList<E>
        Parameters:
        oldIndex - the element's position before the move. Must be within the range 0 <= oldIndex < size().
        newIndex - the element's position after the move. Must be within the range 0 <= newIndex < size().
        Returns:
        the element that was moved.
        Throws:
        java.lang.IndexOutOfBoundsException - if either argument is out of range ( 0 <= index < size()).
        Since:
        1.1
        See Also:
        ListDiffVisitor.handleMove(int, int, Object), ListDiff.accept(ListDiffVisitor)
      • remove

        public boolean remove​(java.lang.Object o)
        Specified by:
        remove in interface java.util.Collection<E>
        Specified by:
        remove in interface IObservableList<E>
        Specified by:
        remove in interface java.util.List<E>
        Overrides:
        remove in class java.util.AbstractCollection<E>
      • containsAll

        public boolean containsAll​(java.util.Collection<?> c)
        Specified by:
        containsAll in interface java.util.Collection<E>
        Specified by:
        containsAll in interface IObservableList<E>
        Specified by:
        containsAll in interface java.util.List<E>
        Overrides:
        containsAll in class java.util.AbstractCollection<E>
      • addAll

        public boolean addAll​(java.util.Collection<? extends E> c)
        Specified by:
        addAll in interface java.util.Collection<E>
        Specified by:
        addAll in interface IObservableList<E>
        Specified by:
        addAll in interface java.util.List<E>
        Overrides:
        addAll in class java.util.AbstractCollection<E>
      • addAll

        public boolean addAll​(int index,
                              java.util.Collection<? extends E> c)
        Specified by:
        addAll in interface IObservableList<E>
        Specified by:
        addAll in interface java.util.List<E>
        Overrides:
        addAll in class java.util.AbstractList<E>
      • removeAll

        public boolean removeAll​(java.util.Collection<?> c)
        Specified by:
        removeAll in interface java.util.Collection<E>
        Specified by:
        removeAll in interface IObservableList<E>
        Specified by:
        removeAll in interface java.util.List<E>
        Overrides:
        removeAll in class java.util.AbstractCollection<E>
      • retainAll

        public boolean retainAll​(java.util.Collection<?> c)
        Specified by:
        retainAll in interface java.util.Collection<E>
        Specified by:
        retainAll in interface IObservableList<E>
        Specified by:
        retainAll in interface java.util.List<E>
        Overrides:
        retainAll in class java.util.AbstractCollection<E>
      • equals

        public boolean equals​(java.lang.Object o)
        Specified by:
        equals in interface java.util.Collection<E>
        Specified by:
        equals in interface IObservableList<E>
        Specified by:
        equals in interface java.util.List<E>
        Overrides:
        equals in class java.util.AbstractList<E>
      • hashCode

        public int hashCode()
        Specified by:
        hashCode in interface java.util.Collection<E>
        Specified by:
        hashCode in interface IObservableList<E>
        Specified by:
        hashCode in interface java.util.List<E>
        Overrides:
        hashCode in class java.util.AbstractList<E>
      • indexOf

        public int indexOf​(java.lang.Object o)
        Specified by:
        indexOf in interface IObservableList<E>
        Specified by:
        indexOf in interface java.util.List<E>
        Overrides:
        indexOf in class java.util.AbstractList<E>
      • lastIndexOf

        public int lastIndexOf​(java.lang.Object o)
        Specified by:
        lastIndexOf in interface IObservableList<E>
        Specified by:
        lastIndexOf in interface java.util.List<E>
        Overrides:
        lastIndexOf in class java.util.AbstractList<E>
      • getRealm

        public Realm getRealm()
        Description copied from interface: IObservable
        Returns the realm for this observable. Unless otherwise specified, getters and setters must be accessed from within this realm. Listeners will be within this realm when they receive events from this observable.

        Because observables can only be accessed from within one realm, and they always fire events on that realm, their state can be observed in an incremental way. It is always safe to call getters of an observable from within a change listener attached to that observable.

        Specified by:
        getRealm in interface IObservable
        Returns:
        the realm