org.jgrapht.util

## Class PrefetchIterator<E>

- java.lang.Object
- - org.jgrapht.util.PrefetchIterator<E>

- All Implemented Interfaces:
    Enumeration<E>, Iterator<E>

      public class PrefetchIterator<E>
      extends Object
      implements Iterator<E>, Enumeration<E>

  Utility class to help implement an iterator/enumerator in which the hasNext() method needs to calculate the next elements ahead of time.

  Many classes which implement an iterator face a common problem: if there is no easy way to calculate hasNext() other than to call getNext(), then they save the result for fetching in the next call to getNext(). This utility helps in doing just that.

  Usage: The new iterator class will hold this class as a member variable and forward the hasNext() and next() to it. When creating an instance of this class, you supply it with a functor that is doing the real job of calculating the next element.

          //This class supllies enumeration of integer till 100.
          public class IteratorExample implements Enumeration{
          private int counter=0;
          private PrefetchIterator nextSupplier;

              IteratorExample()
              {
                  nextSupplier = new PrefetchIterator(new PrefetchIterator.NextElementFunctor(){

                      public Object nextElement() throws NoSuchElementException {
                          counter++;
                          if (counter <= 100)
                              throw new NoSuchElementException();
                          else
                              return new Integer(counter);
                      }

                  });
              }
              //forwarding to nextSupplier and return its returned value
              public boolean hasMoreElements() {
                  return this.nextSupplier.hasMoreElements();
              }
          //  forwarding to nextSupplier and return its returned value
              public Object nextElement() {
                  return this.nextSupplier.nextElement();
              }
        }

  Author:
    Assaf_Lehr

- - ### Nested Class Summary

    Nested Classes
    Modifier and Type | Class and Description
    --- | ---
    `static interface` | `PrefetchIterator.NextElementFunctor<EE>`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `PrefetchIterator(PrefetchIterator.NextElementFunctor<E> aEnum)`

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `boolean` | `hasMoreElements()` If (isGetNextLastResultUpToDate==true) returns true else 1.
    `boolean` | `hasNext()`
    `boolean` | `isEnumerationStartedEmpty()` Tests whether the enumeration started as an empty one.
    `E` | `next()`
    `E` | `nextElement()` 1.
    `void` | `remove()` Always throws UnsupportedOperationException.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### PrefetchIterator

          public PrefetchIterator(PrefetchIterator.NextElementFunctor<E> aEnum)

  - ### Method Detail

    - #### nextElement

          public E nextElement()

      1. Retrieves the saved value or calculates it if it does not exist 2. Changes isGetNextLastResultUpToDate to false. (Because it does not save the NEXT element now; it saves the current one!)
      Specified by:
        `nextElement` in interface `Enumeration<E>`

    - #### hasMoreElements

          public boolean hasMoreElements()

      If (isGetNextLastResultUpToDate==true) returns true else 1. calculates getNext() and saves it 2. sets isGetNextLastResultUpToDate to true.
      Specified by:
        `hasMoreElements` in interface `Enumeration<E>`

    - #### isEnumerationStartedEmpty

          public boolean isEnumerationStartedEmpty()

      Tests whether the enumeration started as an empty one. It does not matter if it hasMoreElements() now, only at initialization time. Efficiency: if nextElements(), hasMoreElements() were never used, it activates the hasMoreElements() once. Else it is immediately(O(1))

    - #### hasNext

          public boolean hasNext()

      Specified by:
        `hasNext` in interface `Iterator<E>`

    - #### next

          public E next()

      Specified by:
        `next` in interface `Iterator<E>`

    - #### remove

          public void remove()
                      throws UnsupportedOperationException

      Always throws UnsupportedOperationException.
      Specified by:
        `remove` in interface `Iterator<E>`
      Throws:
        `UnsupportedOperationException`

Copyright © 2016. All rights reserved.