1 /**
2  * The utillib library.
3  * More information is available at http://www.jinchess.com/.
4  * Copyright (C) 2003 Alexander Maryanovsky.
5  * All rights reserved.
6  *
7  * The utillib library is free software; you can redistribute
8  * it and/or modify it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either version 2 of the
10  * License, or (at your option) any later version.
11  *
12  * The utillib library is distributed in the hope that it will
13  * be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with utillib library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21 
22 package free.util;
23 
24 import java.util.Enumeration;
25 import java.util.NoSuchElementException;
26 
27 
28 /**
29  * An implementation of the <code>Enumeration</code> interface which delegates
30  * to another <code>Enumeration</code>, but only returns elements which pass
31  * the {@link #accept(Object)} method.
32  */
33 
34 public abstract class FilteringEnumeration implements Enumeration{
35 
36 
37 
38   /**
39    * The delegate enumeration.
40    */
41 
42   private final Enumeration delegate;
43 
44 
45 
46   /**
47    * The next element we'll return. This is set by the <code>findNext</code>
48    * method.
49    */
50 
51   private Object next = null;
52 
53 
54 
55   /**
56    * Creates a new <code>FilteringEnumeration</code> object with the specified
57    * delegate.
58    */
59 
FilteringEnumeration(Enumeration delegate)60   public FilteringEnumeration(Enumeration delegate){
61     this.delegate = delegate;
62   }
63 
64 
65 
66   /**
67    * Finds the next element in the delegate enumeration which passes
68    * <code>accept</code> and puts it in <code>next</code>.
69    */
70 
findNext()71   private void findNext(){
72     if (next != null)
73       return;
74 
75     while (delegate.hasMoreElements()){
76       Object element = delegate.nextElement();
77       if (accept(element)){
78         next = element;
79         break;
80       }
81     }
82   }
83 
84 
85 
86   /**
87    * Returns whether there are more elements in this <code>Enumeration</code>.
88    */
89 
hasMoreElements()90   public boolean hasMoreElements(){
91     findNext();
92 
93     return next != null;
94   }
95 
96 
97 
98   /**
99    * Returns the next element in the delegate enumeration which passes the
100    * <code>accept</code> method.
101    */
102 
nextElement()103   public Object nextElement() throws NoSuchElementException{
104     findNext();
105 
106     if (next == null)
107       throw new NoSuchElementException();
108 
109     Object result = next;
110     next = null;
111     return result;
112   }
113 
114 
115 
116   /**
117    * Returns whether the specified object passes the filter.
118    */
119 
accept(Object element)120   public abstract boolean accept(Object element);
121 
122 
123 
124 }