1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.page.document.impl;
18
19 import java.util.Comparator;
20 import java.util.Iterator;
21 import java.util.List;
22 import java.util.Map;
23 import java.util.TreeMap;
24 import java.util.regex.Pattern;
25
26 import org.apache.commons.collections.map.LRUMap;
27 import org.apache.jetspeed.page.document.Node;
28 import org.apache.jetspeed.page.document.NodeSet;
29
30 /***
31 * NodeSetImpl
32 *
33 * @author <a href="mailto:rwatler@apache.org">Randy Watler</a>
34 * @version $Id$
35 */
36 public class NodeSetImpl implements NodeSet
37 {
38 public static final NodeSetImpl EMPTY_NODE_SET = new NodeSetImpl();
39
40 private static final Map patternCache = new LRUMap(128);
41
42 private Map nodes;
43 private Comparator comparator;
44
45 public NodeSetImpl(List nodes, Comparator comparator)
46 {
47 this.nodes = new TreeMap(comparator);
48 Object[] nodeToCopy = nodes.toArray();
49 for (int ix = 0; ix < nodeToCopy.length; ix++)
50 {
51 Node node = (Node)nodeToCopy[ix];
52 if (!this.nodes.containsKey( node.getName()))
53 {
54 this.nodes.put(node.getName(), node);
55 }
56 }
57 this.comparator = comparator;
58 }
59
60 public NodeSetImpl(List nodes)
61 {
62 this(nodes, null);
63 }
64
65 public NodeSetImpl(Comparator comparator)
66 {
67 this.comparator = comparator;
68 }
69
70 public NodeSetImpl(NodeSet nodeSet)
71 {
72 this((nodeSet instanceof NodeSetImpl) ? ((NodeSetImpl)nodeSet).comparator : (Comparator)null);
73 }
74
75 public NodeSetImpl()
76 {
77 }
78
79 /***
80 * getCachedPattern
81 *
82 * @param regex pattern
83 * @return cached pattern
84 */
85 private Pattern getCachedPattern(String regex)
86 {
87 synchronized (patternCache)
88 {
89 if (patternCache.containsKey(regex))
90 {
91 return (Pattern)patternCache.get(regex);
92 }
93 else
94 {
95 Pattern pattern = Pattern.compile(regex);
96 patternCache.put(regex, pattern);
97 return pattern;
98 }
99 }
100 }
101
102
103
104
105 public void add(Node node)
106 {
107 if (nodes == null)
108 {
109 nodes = new TreeMap(comparator);
110 }
111 if (!nodes.containsKey(node.getName()))
112 {
113 nodes.put(node.getName(), node);
114 }
115 }
116
117
118
119
120 public Node get(String name)
121 {
122 if (nodes != null)
123 {
124 return (Node)nodes.get(name);
125 }
126 return null;
127 }
128
129
130
131
132 public Iterator iterator()
133 {
134 if (nodes == null)
135 {
136 nodes = new TreeMap(comparator);
137 }
138 return nodes.values().iterator();
139 }
140
141
142
143
144 public NodeSet subset(String type)
145 {
146 NodeSetImpl subset = new NodeSetImpl(comparator);
147 Iterator nodeItr = iterator();
148 while (nodeItr.hasNext())
149 {
150 Node node = (Node) nodeItr.next();
151 if (node.getType().equals(type))
152 {
153 subset.add(node);
154 }
155 }
156 return subset;
157 }
158
159
160
161
162 public NodeSet inclusiveSubset(String regex)
163 {
164 Pattern pattern = getCachedPattern(regex);
165 NodeSetImpl subset = new NodeSetImpl(comparator);
166 Iterator nodeItr = iterator();
167 while (nodeItr.hasNext())
168 {
169 Node node = (Node) nodeItr.next();
170 if (pattern.matcher(node.getName()).matches())
171 {
172 subset.add(node);
173 }
174 }
175 return subset;
176 }
177
178
179
180
181 public NodeSet exclusiveSubset(String regex)
182 {
183 Pattern pattern = getCachedPattern(regex);
184 NodeSetImpl subset = new NodeSetImpl(comparator);
185 Iterator nodeItr = iterator();
186 while (nodeItr.hasNext())
187 {
188 Node node = (Node) nodeItr.next();
189 if (!pattern.matcher(node.getName()).matches())
190 {
191 subset.add(node);
192 }
193 }
194 return subset;
195 }
196
197
198
199
200 public int size()
201 {
202 if (nodes != null)
203 {
204 return nodes.size();
205 }
206 return 0;
207 }
208
209
210
211
212 public boolean contains(Node node)
213 {
214 if (nodes != null)
215 {
216 return nodes.containsValue(node);
217 }
218 return false;
219 }
220
221
222
223
224 public boolean isEmpty()
225 {
226 if (nodes != null)
227 {
228 return nodes.isEmpty();
229 }
230 return true;
231 }
232 }