Class TreeIterator<T>

  • All Implemented Interfaces:
    Iterator<T>


    public abstract class TreeIterator<T>
    extends Object
    implements Iterator<T>
    • Constructor Summary

      Constructors

      Constructor and Description
      TreeIterator(T parent, boolean includeRoot)
      Creates a new TreeIterator
    • Method Summary

      Modifier and Type Method and Description
      T getRoot()
      Returns the root this iterator is searching.
      boolean hasNext()
      Returns true if there are more values in this iterator.
      protected boolean isReturnValue(T node)
      Return true if this node can be returned by this iterator.
      protected abstract T[] listChildren(T parent)
      List the children of a parent this object should iterate over.
      T next()
      Returns the next node in this iterator.
      void remove()
      Throws an UnsupportedOperationException().
    • Constructor Detail

      • TreeIterator

        public TreeIterator(T parent,
                            boolean includeRoot)
        Creates a new TreeIterator
        Parameters:
        parent - the root to begin searching in.
        includeRoot - whether the first argument should be one of the values returned by this iterator
    • Method Detail

      • listChildren

        protected abstract T[] listChildren(T parent)
        List the children of a parent this object should iterate over. This method does not need to worry about filtering the children.

        (In fact too much filtering at this step can be a problem. For example: if a file iterator wants to only return files with a certain extension this method still needs to return directories, or the iterator won't search deep enough.)

        This may return null, or include null elements in the return array.

      • isReturnValue

        protected boolean isReturnValue(T node)
        Return true if this node can be returned by this iterator. This method acts like a filter: it may be called hundreds of times as we iterate over a tree until we find an acceptable value for next() to return.

        The implementation is simply to return true.

        Parameters:
        node - a possible return value for next()
        Returns:
        false if this is unacceptable and we have to keep searching.
      • getRoot

        public T getRoot()
        Returns the root this iterator is searching.
      • hasNext

        public boolean hasNext()
        Returns true if there are more values in this iterator.
      • next

        public T next()
        Returns the next node in this iterator.
      • remove

        public void remove()
        Throws an UnsupportedOperationException().