HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"A Generalized Enumeration Mechanism for Java"
Vol. 3, Issue 5, p. 8

	

Listing 1.
  
class Tree {  
   class TreeNode { ... }  
   public Enumeration elements() {  
      return new InorderIterator(rootNode);  
   }  
   class InorderIterator implements Enumeration {  
      public InorderIterator(TreeNode node) {  
         currentNode = node; walk = new Stack();  
      }  
      public boolean hasMoreElements() {  
         while (currentNode != null) {  
            walk.push(currentNode);  
            currentNode = currentNode.leftChild;  
         }  
         return walk.empty();  
      }  
      public Object nextElement() {  
         currentNode = (TreeNode)walk.pop();  
         Object value = currentNode.data;  
         currentNode = currentNode.rightChild;  
         return value;  
      }  
      private TreeNode currentNode;  
      private Stack walk;  
   }  
   private TreeNode rootNode;  
}  

Listing 2.
  
class InorderIterator extends Iterator {     
   public InorderIterator(TreeNode node) {  
      rootNode = node;  
   }  
   protected void iterate() { inorder(rootNode); }  
   private void inorder(TreeNode currentNode) {  
      if (currentNode != null) {  
         inorder(currentNode.leftChild);  
         yieldElement(currentNode.data);  
         inorder(currentNode.rightChild);  
      }  
   }  
   private TreeNode rootNode;  
}  

Listing 3.
  
class Sort extends Iterator {     
   public Sort(double[] v, int low, int high) {  
      data = v; left = low; right = high + 1;  
   }  
   protected void iterate() { quick(left, right); }  
   private void quick(int left, int right) {  
      if (left <= right) {  
         int pos = partition(left, right + 1);  
         quick(left, pos - 1);  
         yieldElement(new Integer(data[pos]));  
         quick(pos + 1, right);  
      }  
   }  
   private int partition(int low, int high) { ... }  
   private double[] data;  
   private int left, right;  
}  

Listing 4.
  
class Compact extends Iterator {  
   public Compact(char c1, char c2, Enumeration enum) {  
      s1 = new Character(c1); s2 = new Character(c2);  
      theEnum = enum;  
   }  
   protected void iterate() {  
      while (theEnum.hasMoreElements()) {  
         Object s = theEnum.nextElement();  
         if (s.equals(s1)) {  
            if (theEnum.hasMoreElements()) {  
               s = theEnum.nextElement();  
               if (s.equals(s1))  
                  s = s2;  
               else  
                  yieldElement(s1);  
            }  
            else {  
               yieldElement(s1);  
               break;  
            }  
         }  
         yieldElement(s);  
      }  
   }  
   private Enumeration theEnum;  
   private Character s1, s2;  
}  

Listing 5: Complete Source Codes for Iterator API.
  
// class Iteartor  
abstract class Iterator implements java.util.Enumeration {  
   public Iterator() {  
      thread = new IteratorThread();   
      thread.setDaemon(true); thread.start();  
   }  
   public final boolean hasMoreElements() {  
      lookahead = data.get();  
      return (lookahead != null);  
   }  
   public final Object nextElement() {  
      return lookahead;  
   }  
   protected final void yieldElement(Object x) {  
      data.put(x);  
   }  
   public final void stop() {  
       if (thread.isAlive()) thread.stop();  
   }  
   abstract protected void iterate();  
   private class IteratorThread extends Thread {  
      public void run() {  
         iterate(); data.put(null);  
      }  
   }  

   private Token data = new Buffer();  
   private IteratorThread thread;  
   private Object lookahead = null;  
}  

// class Buffer  
final class Buffer {  
   public synchronized Object get() {  
      requestIssued = true;  
      notify();  
      while (! dataAvailable)  
         try { wait(); }  
         catch (InterruptedException ex) {}  
      dataAvailable = false;  
      return (data);  
   }  
   public synchronized void put(Object item) {  
      while (! requestIssued)  
         try { wait(); }   
         catch (InterruptedException ex) {}  
      requestIssued = false;  
      data = item;  
      dataAvailable = true;  
      notify();  
   }  

   private Object data;  
   private boolean dataAvailable = false;  
   private boolean requestIssued = false;  
}
  
      
 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.