KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lateralnz > common > util > FilteredIterator


1 /* ====================================================================
2  * The LateralNZ Software License, Version 1.0
3  *
4  * Copyright (c) 2003 LateralNZ. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by
21  * LateralNZ (http://www.lateralnz.org/) and other third parties."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. The names "LateralNZ" must not be used to endorse or promote
26  * products derived from this software without prior written
27  * permission. For written permission, please
28  * contact oss@lateralnz.org.
29  *
30  * 5. Products derived from this software may not be called "Panther",
31  * or "Lateral" or "LateralNZ", nor may "PANTHER" or "LATERAL" or
32  * "LATERALNZ" appear in their name, without prior written
33  * permission of LateralNZ.
34  *
35  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46  * SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of LateralNZ. For more
51  * information on Lateral, please see http://www.lateralnz.com/ or
52  * http://www.lateralnz.org
53  *
54  */

55 package org.lateralnz.common.util;
56
57 import java.util.HashMap JavaDoc;
58 import java.util.Set JavaDoc;
59 import java.util.Iterator JavaDoc;
60 import java.util.regex.Matcher JavaDoc;
61 import java.util.regex.Pattern JavaDoc;
62
63 /**
64  *
65  * @author jbriggs
66  */

67 public class FilteredIterator implements Iterator JavaDoc {
68   
69   private Pattern JavaDoc ipat;
70   private Pattern JavaDoc epat;
71   private Iterator JavaDoc iter;
72   
73   private boolean hasNextProcessed = false;
74   private boolean hasNext = false;
75   private Object JavaDoc next = null;
76   
77   public FilteredIterator(Set JavaDoc keyset, String JavaDoc incregex, String JavaDoc excregex) {
78     ipat = Pattern.compile(incregex);
79     epat = Pattern.compile(excregex);
80     iter = keyset.iterator();
81   }
82   
83   private Object JavaDoc getNext() {
84     String JavaDoc key;
85     while (iter.hasNext()) {
86       key = (String JavaDoc)iter.next();
87       Matcher JavaDoc imat = ipat.matcher(key);
88       Matcher JavaDoc emat = epat.matcher(key);
89       if (imat.matches() && !emat.matches()) {
90         return key;
91       }
92     }
93     return null;
94   }
95   
96   public boolean hasNext() {
97     if (!hasNextProcessed) {
98       next = getNext();
99       if (next != null) {
100         hasNext = true;
101         return true;
102       }
103     }
104     return hasNext;
105   }
106   
107   public Object JavaDoc next() {
108     hasNext = false;
109     if (next != null) {
110       Object JavaDoc rtn = next;
111       next = null;
112       return rtn;
113     }
114     else {
115       return getNext();
116     }
117   }
118   
119   public void remove() {
120     throw new UnsupportedOperationException JavaDoc();
121   }
122   
123   public static final void main(String JavaDoc[] args) throws Exception JavaDoc {
124     HashMap JavaDoc hm = new HashMap JavaDoc();
125     hm.put("a/test1", "");
126     hm.put("a/hello2", "");
127     hm.put("a/test4", "");
128     hm.put("a/tst5", "");
129     hm.put("b/", "");
130     hm.put("b/test18", "");
131     hm.put("b/test3", "");
132     hm.put("b/e/test19", "");
133     hm.put("c/test2", "");
134     hm.put("c/whattest52", "");
135     
136     FilteredIterator fi = new FilteredIterator(hm.keySet(), "b/[^/]*", "b/|");
137     while (fi.hasNext()) {
138       System.out.println(fi.next());
139     }
140   }
141 }
142
Popular Tags