Class WritableList<E>

  • Type Parameters:
    E - the type of the elements in this list
    All Implemented Interfaces:
    java.lang.Iterable<E>, java.util.Collection<E>, java.util.List<E>, IObservable, IObservableCollection<E>, IObservableList<E>

    public class WritableList<E>
    extends ObservableList<E>
    Mutable observable list backed by an ArrayList.

    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
    • Constructor Detail

      • WritableList

        public WritableList()
        Creates an empty writable list in the default realm with a null element type.
      • WritableList

        public WritableList​(Realm realm)
        Creates an empty writable list with a null element type.
        Parameters:
        realm - the observable's realm
      • WritableList

        public WritableList​(java.util.List<E> toWrap,
                            java.lang.Object elementType)
        Constructs a new instance with the default realm. Note that for backwards compatibility reasons, the contents of the created WritableList will change with the contents of the given list. If this is not desired, WritableList(Collection, Object) should be used by casting the first argument to Collection.
        Parameters:
        toWrap - The java.util.List to wrap
        elementType - can be null
      • WritableList

        public WritableList​(java.util.Collection<E> collection,
                            java.lang.Object elementType)
        Constructs a new instance in the default realm containing the elements of the given collection. Changes to the given collection after calling this method do not affect the contents of the created WritableList.
        Parameters:
        collection - the collection to copy
        elementType - can be null
        Since:
        1.2
      • WritableList

        public WritableList​(Realm realm,
                            java.util.List<E> toWrap,
                            java.lang.Object elementType)
        Creates a writable list containing elements of the given type, wrapping an existing client-supplied list. Note that for backwards compatibility reasons, the contents of the created WritableList will change with the contents of the given list. If this is not desired, WritableList(Realm, Collection, Object) should be used by casting the second argument to Collection.
        Parameters:
        realm - the observable's realm
        toWrap - The java.util.List to wrap
        elementType - can be null
      • WritableList

        public WritableList​(Realm realm,
                            java.util.Collection<E> collection,
                            java.lang.Object elementType)
        Constructs a new instance in the default realm containing the elements of the given collection. Changes to the given collection after calling this method do not affect the contents of the created WritableList.
        Parameters:
        realm - the observable's realm
        collection - the collection to copy
        elementType - can be null
        Since:
        1.2
    • Method Detail

      • move

        public E move​(int oldIndex,
                      int newIndex)
        Description copied from class: ObservableList
        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>
        Overrides:
        move in class ObservableList<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.
        Since:
        1.1
        See Also:
        ListDiffVisitor.handleMove(int, int, Object), ListDiff.accept(ListDiffVisitor)
      • add

        public boolean add​(E element)
        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 ObservableList<E>
      • add

        public void add​(int index,
                        E element)
        Specified by:
        add in interface java.util.List<E>
        Overrides:
        add in class ObservableList<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 ObservableList<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 ObservableList<E>
      • 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 ObservableList<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 ObservableList<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 ObservableList<E>
      • clear

        public void clear()
        Specified by:
        clear in interface java.util.Collection<E>
        Specified by:
        clear in interface java.util.List<E>
        Overrides:
        clear in class ObservableList<E>
      • withElementType

        public static <T> WritableList<T> withElementType​(java.lang.Object elementType)
        Parameters:
        elementType - can be null
        Returns:
        new list with the default realm.
      • addListener

        protected void addListener​(java.lang.Object listenerType,
                                   IObservablesListener listener)
        Parameters:
        listenerType - arbitrary object to identify a type of the listener
        listener - the listener to add; not null
      • removeListener

        protected void removeListener​(java.lang.Object listenerType,
                                      IObservablesListener listener)
        Parameters:
        listenerType - arbitrary object to identify a type of the listener
        listener - the listener to remove; not null
      • hasListeners

        protected boolean hasListeners()
      • firstListenerAdded

        protected void firstListenerAdded()
      • lastListenerRemoved

        protected void lastListenerRemoved()
      • getRealm

        public Realm getRealm()
        Returns:
        Returns the realm.
      • clone

        protected java.lang.Object clone()
                                  throws java.lang.CloneNotSupportedException
        Overrides:
        clone in class java.lang.Object
        Throws:
        java.lang.CloneNotSupportedException