Class NodeList

  • All Implemented Interfaces:
    ReactiveValue

    public class NodeList
    extends NodeFeature
    implements ReactiveValue
    A state node feature that structures data as a list.

    The list works as a reactive value with regards to its structure. A Computation will get a dependency on this list for any read operation that depends on the list structure, such as querying the length, iterating the list or finding the index of an item. Accessing an item by index does not create a dependency. The Computation is invalidated when items are added, removed, reordered or replaced. It is not invalidated when the contents of an item is updated since all items are expected to be either immutable or reactive values of their own.

    Since:
    1.0
    Author:
    Vaadin Ltd
    • Constructor Detail

      • NodeList

        public NodeList​(int id,
                        StateNode node)
        Creates a new list.
        Parameters:
        id - the id of the list
        node - the node of the list
    • Method Detail

      • length

        public int length()
        Gets the number of items in this list.
        Returns:
        the number of items
      • get

        public Object get​(int index)
        Gets the item at the given index.
        Parameters:
        index - the index
        Returns:
        the item at the index
      • set

        public void set​(int index,
                        Object value)
        Sets the value at the given index.
        Parameters:
        index - the index
        value - the value to set
      • add

        public void add​(int index,
                        Object item)
        Shorthand for adding the given item at the given index. This method delegates to splice(int, int, JsArray) which updates the list contents and fires the appropriate event.
        Parameters:
        index - the index where the item should be added
        item - the new item to add
      • splice

        public void splice​(int index,
                           int remove)
        Removes a number of items at the given index. This causes a ListSpliceEvent to be fired.
        Parameters:
        index - the index at which do do the operation
        remove - the number of items to remove
      • splice

        public final void splice​(int index,
                                 int remove,
                                 JsArray<?> add)
        Removes and adds a number of items at the given index.

        This causes a ListSpliceEvent to be fired.

        Parameters:
        index - the index at which do do the operation
        remove - the number of items to remove
        add - an array of new items to add
      • getDebugJson

        public elemental.json.JsonValue getDebugJson()
        Description copied from class: NodeFeature
        Gets a JSON object representing the contents of this feature. Only intended for debugging purposes.
        Specified by:
        getDebugJson in class NodeFeature
        Returns:
        a JSON representation
      • convert

        public elemental.json.JsonValue convert​(Function<Object,​elemental.json.JsonValue> converter)
        Description copied from class: NodeFeature
        Convert the feature values into a JsonValue using provided converter for the values stored in the feature (i.e. primitive types, StateNodes).
        Specified by:
        convert in class NodeFeature
        Parameters:
        converter - converter to convert values stored in the feature
        Returns:
        resulting converted value
      • addSpliceListener

        public elemental.events.EventRemover addSpliceListener​(ListSpliceListener listener)
        Adds a listener that will be notified when the list structure changes.
        Parameters:
        listener - the list change listener
        Returns:
        an event remover that can be used for removing the added listener
      • addReactiveValueChangeListener

        public elemental.events.EventRemover addReactiveValueChangeListener​(ReactiveValueChangeListener reactiveValueChangeListener)
        Description copied from interface: ReactiveValue
        Adds a listener that has a dependency to this value, and should be notified when this value changes.
        Specified by:
        addReactiveValueChangeListener in interface ReactiveValue
        Parameters:
        reactiveValueChangeListener - the listener to add
        Returns:
        an event remover that can be used for removing the added listener
      • forEach

        public void forEach​(JsCollections.ForEachCallback<Object> callback)
        Iterates all values in this list.
        Parameters:
        callback - the callback to invoke for each value
      • hasBeenCleared

        public boolean hasBeenCleared()
        Returns true if the list instance has been cleared at some point.
        Returns:
        true if the list instance has been cleared