Class TreeNodeChildren.Iter

java.lang.Object
org.apache.sis.metadata.TreeNodeChildren.Iter
All Implemented Interfaces:
Iterator<TreeTable.Node>
Enclosing class:
TreeNodeChildren

private final class TreeNodeChildren.Iter extends Object implements Iterator<TreeTable.Node>
The iterator over the elements in the enclosing TreeNodeChildren collection. Each element is identified by its index in the PropertyAccessor, together with its position in its sub-iterator when the metadata property is a collection.

Implementation note

It could be cheaper to not take an iterator for the properties that are collections, and instead just increment a "sub-index" from 0 to the collection size. It would be cheaper because we don't really need to extract the values of those collections (i.e. the nextValue field is not really needed). Nevertheless we prefer (for now) the iterator approach anyway because it makes easier to implement the remove() method and has the desired side-effect to check for concurrent modifications. It also keeps the nextValue field up-to-date in case we would like to use it in a future SIS version. We do that on the assumption that sub-iterators are cheap since they are ArrayList iterators in the majority of cases.
  • Field Details

    • nextInAccessor

      private int nextInAccessor
      Index in TreeNodeChildren.accessor of the next element to be returned by next(), or PropertyAccessor.count() if we have reached the end of the list.
    • previousInAccessor

      private int previousInAccessor
      Index in TreeNodeChildren.accessor of the element returned by the last call to next(), or -1 if none.
    • isNextVerified

      private boolean isNextVerified
      true if we have verified the value at nextInAccessor index for non-null or non-empty element.
    • nextValue

      private Object nextValue
      The value of the node to be returned by the next() method. This value is computed ahead of time by hasNext() since we need that information in order to determine if the value needs to be skipped or not.
      Note: Actually we don't really need to keep this value, since it is not used outside the hasNext() method. But we keep it for allowing the next() method to opportunistically update the TreeNode.cachedValue field.
    • subIterator

      private Iterator<?> subIterator
      If the call to next() found a collection, the iterator over the elements in that collection. Otherwise null. A non-null value (even if that sub-iterator has no next elements) means that nextValue is an element of that sub-iteration.
    • subIndex

      private int subIndex
      Position of the nextValue in the subIterator, or -1 if the sub-iterator is null.
    • modCountCheck

      private int modCountCheck
      The value of TreeNodeChildren.modCount at construction time or after the last change done by this iterator. Used for concurrent modification checks.
      Note: Actually this iterator should be robust to most concurrent modifications. But we check anyway in order to prevent concurrent modifications in user code, in case a future SIS version become more sensitive to such changes.
  • Constructor Details

    • Iter

      Iter()
      Creates a new iterator.
  • Method Details

    • checkConcurrentModification

      final void checkConcurrentModification()
      Throws ConcurrentModificationException if an unexpected change has been detected.
    • hasNext

      public boolean hasNext()
      Ensures that nextInAccessor is valid. If the index has not been validated, then this method moves the iterator to the next valid element, starting at the current nextInAccessor value.
      Specified by:
      hasNext in interface Iterator<TreeTable.Node>
      Returns:
      true on success, or false if we reached the end of the iteration.
    • next

      public TreeTable.Node next()
      Returns the node for the metadata property at the current nextInAccessor. The value of this property is initially nextValue, but this may change at any time if the user modifies the underlying metadata object.
      Specified by:
      next in interface Iterator<TreeTable.Node>
    • remove

      public void remove()
      Clears the element returned by the last call to next(). Whether the cleared element is considered removed or not depends on the value policy and on the element type. With the default NON_EMPTY policy, the effect is a removal.
      Specified by:
      remove in interface Iterator<TreeTable.Node>