home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / applets / collectn / filterin.jav < prev    next >
Encoding:
Text File  |  1995-10-22  |  3.8 KB  |  155 lines

  1. /*
  2.   File: FilteringEnumeration.java
  3.  
  4.   Originally written by Doug Lea and released into the public domain. 
  5.   Thanks for the assistance and support of Sun Microsystems Labs, Agorics 
  6.   Inc, Loral, and everyone contributing, testing, and using this code.
  7.  
  8.   History:
  9.   Date     Who                What
  10.   22Oct95  dl@cs.oswego.edu   Created.
  11.  
  12. */
  13.   
  14. package collections;
  15.  
  16. import java.util.Enumeration;
  17. import java.util.NoSuchElementException;
  18.  
  19. /**
  20.  *
  21.  * FilteringEnumerations allow you to filter out elements from
  22.  * other enumerations before they are seen by their `consumers'
  23.  * (i.e., the callers of `nextElement').
  24.  * <P>
  25.  * FilteringEnumerations work as wrappers around other Enumerations.
  26.  * To build one, you need an existing Enumeration (perhaps one
  27.  * from coll.elements(), for some Collection coll), and a Predicate
  28.  * object (i.e., implementing interface Predicate). 
  29.  * For example, if you want to screen out everything but Panel
  30.  * objects from a collection coll that might hold things other than Panels,
  31.  * write something of the form:
  32.  * <PRE>
  33.  * Enumeration e = coll.elements();
  34.  * Enumeration panels = FilteringEnumeration(e, IsPanel);
  35.  * while (panels.hasMoreElements()) 
  36.  *  doSomethingWith((Panel)(panels.nextElement()));
  37.  * </PRE>
  38.  * To use this, you will also need to write a little class of the form:
  39.  * <PRE>
  40.  * class IsPanel implements Predicate {
  41.  *  boolean predicate(Object v) { return (v instanceof Panel); }
  42.  * }
  43.  * </PRE>
  44.  * @see collections.Predicate#predicate
  45.  * @author Doug Lea
  46.  * @version 0.93
  47.  *
  48.  * <P> For an introduction to this package see <A HREF="index.html"> Overview </A>.
  49.  *
  50. **/
  51.  
  52. public class FilteringEnumeration implements Enumeration {
  53.  
  54. // instance variables
  55.  
  56. /**
  57.  * The enumeration we are wrapping
  58. **/
  59.  
  60.   private Enumeration src_;
  61.  
  62. /**
  63.  * The screening predicate
  64. **/
  65.  
  66.   private Predicate pred_;
  67.  
  68. /**
  69.  * The sense of the predicate. False means to invert
  70. **/
  71.  
  72.   private boolean sign_;
  73.  
  74. /**
  75.  * The next element to hand out
  76. **/
  77.  
  78.   private Object  nextElement_;
  79.  
  80. /**
  81.  * True if we have a next element 
  82. **/
  83.  
  84.   private boolean haveNext_;
  85.  
  86. /**
  87.  * Make a Filter using src for the elements, and p as the screener,
  88.  * selecting only those elements of src for which p is true
  89. **/
  90.  
  91.   public FilteringEnumeration(Enumeration src, Predicate p) { this(src, p, true); }
  92.  
  93. /**
  94.  * Make a Filter using src for the elements, and p as the screener,
  95.  * selecting only those elements of src for which p.predicate(v) == sense.
  96.  * A value of true for sense selects only values for which p.predicate
  97.  * is true. A value of false selects only those for which it is false.
  98. **/
  99.   public FilteringEnumeration(Enumeration src, Predicate p, boolean sense) {
  100.     src_ = src;
  101.     pred_ = p;
  102.     sign_ = sense;
  103.     haveNext_ = false;
  104.     findNext();
  105.   }
  106.  
  107. /**
  108.  * Implements java.util.Enumeration.hasMoreElements
  109. **/
  110.  
  111.   public synchronized boolean hasMoreElements() { return haveNext_; }
  112.  
  113. /**
  114.  * Implements java.util.Enumeration.nextElement.
  115. **/
  116.   public synchronized Object nextElement() {
  117.     if (!hasMoreElements())
  118.       throw new NoSuchElementException("exhausted enumeration");
  119.     else {
  120.       Object result = nextElement_;
  121.       findNext();
  122.       return result;
  123.     }
  124.   }
  125.  
  126. /**
  127.  * Traverse through src_ elements finding one passing predicate
  128. **/
  129.   private void findNext() {
  130.     haveNext_ = false;
  131.     nextElement_ = null;
  132.     for (;;) {
  133.       if (!src_.hasMoreElements()) 
  134.         return;
  135.       else {
  136.         try {
  137.           Object v = src_.nextElement();
  138.           if (pred_.predicate(v) == sign_) {
  139.             haveNext_ = true;
  140.             nextElement_ = v;
  141.             return;
  142.           }
  143.         }
  144.         catch (NoSuchElementException ex) {
  145.           return;
  146.         }
  147.       }
  148.     }
  149.   }
  150.  
  151.  
  152. }
  153.   
  154.  
  155.