edu.brandeis.cs.steele.util
Class LookaheadEnumeration

java.lang.Object
  extended by edu.brandeis.cs.steele.util.LookaheadEnumeration
All Implemented Interfaces:
java.util.Enumeration

public class LookaheadEnumeration
extends java.lang.Object
implements java.util.Enumeration

A wrapper for objects that declared Enumerations that don't fully implement hasMoreElements, to bring them into conformance with the specification of that function. It's sometimes difficult to determine whether a next element exists without trying to generate it. (This is particularly true when reading elements from a stream.) Unfortunately, the Enumeration protocol distributes the work of determining whether another element exists, and supplying it, across two methods. A call that implements an enumerator that terminates on failure to generate must therefore cache the next result. This class can be used as a wrapper, to cache the result independently of the generator logic. LookaheadEnumeration.hasMoreElements returns false when hasMoreElements of the wrapped object returns false, or when nextElement of the wrapped class

An Enumeration that supplies the lines of a file until the file ends can be written thus:

 new LookaheadEnumeration(new Enumeration() {
   InputStream input = ...;
   public boolean hasMoreElements() { return true; }
   public Object nextElement() {
     String line = input.readLine();
     if (line == null) {
       throw new NoSuchElementException();
     }
     return line;
   }
 }
 

An Enumeration that generates the natural numbers below the first with the property p can be written thus:

 new LookaheadEnumeration(new Enumeration() {
   int n = 0;
   public boolean hasMoreElements() { return true; }
   public Object nextElement() {
     int value = n++;
     if (p(value)) {
       throw new NoSuchElementException();
     }
     return value;
   }
 }
 

Author:
Oliver Steele, steele@cs.brandeis.edu

Field Summary
protected  java.util.Enumeration ground
           
protected  boolean more
           
protected  java.lang.Object nextObject
           
protected  boolean peeked
           
 
Constructor Summary
LookaheadEnumeration(java.util.Enumeration ground)
           
 
Method Summary
 boolean hasMoreElements()
           
protected  void lookahead()
           
 java.lang.Object nextElement()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ground

protected java.util.Enumeration ground

peeked

protected boolean peeked

nextObject

protected java.lang.Object nextObject

more

protected boolean more
Constructor Detail

LookaheadEnumeration

public LookaheadEnumeration(java.util.Enumeration ground)
Method Detail

lookahead

protected void lookahead()

hasMoreElements

public boolean hasMoreElements()
Specified by:
hasMoreElements in interface java.util.Enumeration

nextElement

public java.lang.Object nextElement()
Specified by:
nextElement in interface java.util.Enumeration