KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jetspeed > om > registry > base > CategoryIterator


1 /*
2  * Copyright 2000-2001,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.jetspeed.om.registry.base;
18
19 import java.util.Iterator JavaDoc;
20 import java.util.SortedMap JavaDoc;
21 import java.util.HashMap JavaDoc;
22
23 import java.util.NoSuchElementException JavaDoc;
24 import java.lang.UnsupportedOperationException JavaDoc;
25 import java.lang.IllegalStateException JavaDoc;
26
27 import org.apache.jetspeed.om.registry.*;
28
29
30 /**
31  * CategoryIterator - iterators over category treemap/hashmap allowing dups
32  *
33  *
34  * @author <a HREF="mailto:taylor@apache.org">David Sean Taylor</a>
35  * @version $Id: CategoryIterator.java,v 1.3 2004/02/23 03:08:26 jford Exp $
36  */

37 public class CategoryIterator implements Iterator JavaDoc
38 {
39     protected SortedMap JavaDoc map = null;
40     protected String JavaDoc key;
41     protected Iterator JavaDoc mapIterator = null;
42     protected Iterator JavaDoc bucketIterator = null;
43     protected boolean iteratingMaps = true;
44     protected HashMap JavaDoc bucket = null;
45     protected PortletEntry portlet = null;
46     protected boolean findall = false;
47     protected String JavaDoc category = "";
48     protected String JavaDoc group = "";
49
50     public String JavaDoc getCategory()
51     {
52         return category;
53     }
54
55     public String JavaDoc getGroup()
56     {
57         return group;
58     }
59
60     public CategoryIterator(SortedMap JavaDoc map, String JavaDoc key)
61     {
62         this.map = map;
63         this.key = key;
64         findall = (this.key == null || this.key.equals(""));
65         if (findall)
66             this.map = map;
67         else
68             this.map = map.tailMap(key);
69         this.mapIterator = this.map.entrySet().iterator();
70         this.bucketIterator = null;
71         this.bucket = null;
72     }
73
74     private CategoryIterator()
75     {}
76
77     public boolean hasNext()
78     {
79         if (iteratingMaps)
80         {
81             if (mapIterator.hasNext() == false)
82                 return false;
83             return filter();
84         }
85
86         if (bucketIterator.hasNext())
87             return getPortletEntry();
88
89         // reached end of bucket, try next map
90
if (mapIterator.hasNext())
91         {
92             return filter();
93         }
94         return false; //reached end of maps
95
}
96
97     protected boolean filter()
98     {
99         java.util.Map.Entry entry = (java.util.Map.Entry)mapIterator.next();
100         String JavaDoc entryKey = (String JavaDoc)entry.getKey();
101         int pos = entryKey.indexOf('.');
102         this.category = "";
103         if (-1 == pos)
104         {
105             this.group = entryKey;
106         }
107         else
108         {
109             this.group = entryKey.substring(0, pos);
110             int length = entryKey.length();
111             if (length > pos + 1)
112                 this.category = entryKey.substring(pos + 1, length);
113         }
114
115         if (!findall && !entryKey.startsWith(this.key))
116             return false; // end of criteria
117

118         bucket = (HashMap JavaDoc)entry.getValue();
119
120         bucketIterator = bucket.entrySet().iterator();
121         iteratingMaps = false;
122         if (bucketIterator.hasNext() == false)
123             return false;
124         return getPortletEntry();
125     }
126
127
128     protected boolean getPortletEntry()
129     {
130         java.util.Map.Entry entry = (java.util.Map.Entry)bucketIterator.next();
131         if (null == entry)
132             return false;
133
134         this.portlet = (PortletEntry)entry.getValue();
135
136         return true;
137     }
138
139     public void remove() throws IllegalStateException JavaDoc, UnsupportedOperationException JavaDoc
140     {
141         throw new UnsupportedOperationException JavaDoc("The remove() method is not supported");
142     }
143
144
145     public Object JavaDoc next() throws NoSuchElementException JavaDoc
146     {
147         return portlet;
148     }
149
150 }
151
Popular Tags