KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > bridge > util > BridgeCollections


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10
11 package org.mmbase.bridge.util;
12
13 import java.util.*;
14 import org.mmbase.bridge.*;
15
16 /**
17  * Analogon of {@link java.util.Collections}. Methods speak for themselves.
18  *
19  *
20  * @author Michiel Meeuwissen
21  * @version $Id: BridgeCollections.java,v 1.4 2006/01/19 17:00:14 pierre Exp $
22  * @since MMBase-1.8
23  */

24
25 public abstract class BridgeCollections {
26
27     /**
28      * Makes a BridgeList unmodifiable.
29      */

30     public static final BridgeList unmodifiableBridgeList(BridgeList bridgeList) {
31         return new UnmodifiableBridgeList(bridgeList);
32     }
33
34     /**
35      * Makes a NodeList unmodifiable.
36      */

37     public static final NodeList unmodifiableNodeList(NodeList nodeList) {
38         return new UnmodifiableNodeList(nodeList);
39     }
40
41     /**
42      * Makes a NodeManagerList unmodifiable.
43      */

44     public static final NodeManagerList unmodifiableNodeManagerList(NodeManagerList nodeList) {
45         return new UnmodifiableNodeManagerList(nodeList);
46     }
47
48     /**
49      * Makes a RelationManagerList unmodifiable.
50      */

51     public static final RelationManagerList unmodifiableRelationManagerList(RelationManagerList nodeList) {
52         return new UnmodifiableRelationManagerList(nodeList);
53     }
54
55     /**
56      * Makes a RelationList unmodifiable.
57      */

58     public static final RelationList unmodifiableRelationList(RelationList relationList) {
59         return new UnmodifiableRelationList(relationList);
60     }
61
62     /**
63      * Makes a StringList unmodifiable.
64      */

65     public static final StringList unmodifiableStringList(StringList stringList) {
66         return new UnmodifiableStringList(stringList);
67     }
68
69     public static final BridgeList EMPTY_BRIDGELIST = new EmptyBridgeList();
70     public static final NodeList EMPTY_NODELIST = new EmptyNodeList();
71     public static final NodeManagerList EMPTY_NODEMANAGERLIST = new EmptyNodeManagerList();
72     public static final RelationManagerList EMPTY_RELATIONMANAGERLIST = new EmptyRelationManagerList();
73     public static final RelationList EMPTY_RELATIONLIST = new EmptyRelationList();
74     public static final StringList EMPTY_STRINGLIST = new EmptyStringList();
75
76     // IMPLEMENTATIONS follow below.
77

78     /* --------------------------------------------------------------------------------
79      * Unmodifiable iterators
80      */

81     static class UnmodifiableListIterator implements ListIterator {
82         final protected ListIterator i;
83         UnmodifiableListIterator(ListIterator i) {
84             this.i = i;
85         }
86         public boolean hasNext() { return i.hasNext(); }
87         public boolean hasPrevious() { return i.hasPrevious(); }
88         public Object JavaDoc next() { return i.next(); }
89         public Object JavaDoc previous() { return i.previous(); }
90         public int nextIndex() { return i.nextIndex(); }
91         public int previousIndex() { return i.previousIndex(); }
92         public void remove() { throw new UnsupportedOperationException JavaDoc(); }
93         public void add(Object JavaDoc o) { throw new UnsupportedOperationException JavaDoc(); }
94         public void set(Object JavaDoc o) { throw new UnsupportedOperationException JavaDoc(); }
95     }
96
97     static class UnmodifiableNodeIterator extends UnmodifiableListIterator implements NodeIterator {
98         UnmodifiableNodeIterator(NodeIterator i) {
99             super(i);
100         }
101         public Node nextNode() { return ((NodeIterator) i).nextNode(); }
102         public Node previousNode() { return ((NodeIterator) i).previousNode(); }
103     }
104
105     static class UnmodifiableNodeManagerIterator extends UnmodifiableNodeIterator implements NodeManagerIterator {
106         UnmodifiableNodeManagerIterator(NodeManagerIterator i) {
107             super(i);
108         }
109         public NodeManager nextNodeManager() { return ((NodeManagerIterator) i).nextNodeManager(); }
110         public NodeManager previousNodeManager() { return ((NodeManagerIterator) i).previousNodeManager(); }
111     }
112
113     static class UnmodifiableRelationManagerIterator extends UnmodifiableNodeManagerIterator implements RelationManagerIterator {
114         UnmodifiableRelationManagerIterator(RelationManagerIterator i) {
115             super(i);
116         }
117         public RelationManager nextRelationManager() { return ((RelationManagerIterator) i).nextRelationManager(); }
118         public RelationManager previousRelationManager() { return ((RelationManagerIterator) i).previousRelationManager(); }
119     }
120
121     static class UnmodifiableRelationIterator extends UnmodifiableNodeIterator implements RelationIterator {
122         UnmodifiableRelationIterator(RelationIterator i) {
123             super(i);
124         }
125         public Relation nextRelation() { return ((RelationIterator) i).nextRelation(); }
126         public Relation previousRelation() { return ((RelationIterator) i).previousRelation(); }
127     }
128
129     static class UnmodifiableStringIterator extends UnmodifiableListIterator implements StringIterator {
130         UnmodifiableStringIterator(StringIterator i) {
131             super(i);
132         }
133         public String JavaDoc nextString() { return ((StringIterator) i).nextString(); }
134         public String JavaDoc previousString() { return ((StringIterator) i).previousString(); }
135
136     }
137
138     /* --------------------------------------------------------------------------------
139      * Unmodifiable Lists.
140      */

141     static class UnmodifiableBridgeList implements BridgeList {
142         final List c;
143         final BridgeList parent ; // just to expose properties to sublists.
144

145         UnmodifiableBridgeList() {
146             c = EMPTY_BRIDGELIST;
147             parent = null;
148         }
149
150         UnmodifiableBridgeList(BridgeList c) {
151             if (c == null) { throw new NullPointerException JavaDoc(); }
152             this.c = c;
153             this.parent = null;
154         }
155
156         UnmodifiableBridgeList(List c, BridgeList parent) {
157             if (c == null) { throw new NullPointerException JavaDoc(); }
158             this.c = c;
159             this.parent = parent;
160         }
161
162         public int size() { return c.size(); }
163         public boolean isEmpty() { return c.isEmpty(); }
164         public boolean contains(Object JavaDoc o) { return c.contains(o); }
165         public Object JavaDoc[] toArray() { return c.toArray(); }
166         public Object JavaDoc[] toArray(Object JavaDoc[] a) { return c.toArray(a); }
167         public String JavaDoc toString() { return c.toString(); }
168         public ListIterator listIterator(final int s) { return new UnmodifiableListIterator(c.listIterator(s)); }
169         public ListIterator listIterator() { return listIterator(0); }
170         public Iterator iterator() { return listIterator(); }
171         public boolean add(Object JavaDoc o){ throw new UnsupportedOperationException JavaDoc(); }
172         public void add(int i, Object JavaDoc o) { throw new UnsupportedOperationException JavaDoc(); }
173         public Object JavaDoc set(int i, Object JavaDoc o) { throw new UnsupportedOperationException JavaDoc(); }
174         public boolean remove(Object JavaDoc o) { throw new UnsupportedOperationException JavaDoc(); }
175         public Object JavaDoc remove(int i) { throw new UnsupportedOperationException JavaDoc(); }
176         public boolean containsAll(Collection coll) { return c.containsAll(coll); }
177         public boolean addAll(Collection coll) { throw new UnsupportedOperationException JavaDoc(); }
178         public boolean addAll(int i, Collection coll) { throw new UnsupportedOperationException JavaDoc(); }
179         public boolean removeAll(Collection coll) { throw new UnsupportedOperationException JavaDoc(); }
180         public boolean retainAll(Collection coll) { throw new UnsupportedOperationException JavaDoc(); }
181         public void clear() { throw new UnsupportedOperationException JavaDoc(); }
182         public Object JavaDoc get(int i) { return c.get(i); }
183
184         public Object JavaDoc getProperty(Object JavaDoc key) {
185             if (parent != null) return parent.getProperty(key);
186             return ((BridgeList) c).getProperty(key);
187         }
188
189         public void setProperty(Object JavaDoc key, Object JavaDoc value) { throw new UnsupportedOperationException JavaDoc(); }
190         public void sort() { throw new UnsupportedOperationException JavaDoc(); }
191         public void sort(Comparator comparator) { throw new UnsupportedOperationException JavaDoc(); }
192
193         public List subList(int fromIndex, int toIndex) {
194             return new UnmodifiableBridgeList(c.subList(fromIndex, toIndex), parent != null ? parent : (BridgeList) c);
195         }
196
197         public int lastIndexOf(Object JavaDoc o) { return c.lastIndexOf(o); }
198         public int indexOf(Object JavaDoc o) { return c.indexOf(o); }
199         public boolean equals(Object JavaDoc o) { return c.equals(o); }
200         public int hashCode() { return c.hashCode(); }
201     }
202
203     static class UnmodifiableNodeList extends UnmodifiableBridgeList implements NodeList {
204
205         UnmodifiableNodeList(NodeList nodeList) {
206             super(nodeList);
207         }
208
209         public Node getNode(int index) {
210             return ((NodeList) c).getNode(index);
211         }
212
213         public NodeIterator nodeIterator() {
214             return new UnmodifiableNodeIterator(((NodeList)c).nodeIterator());
215         }
216
217         public NodeList subNodeList(int fromIndex, int toIndex) {
218             return new UnmodifiableNodeList(((NodeList) c).subNodeList(fromIndex, toIndex));
219         }
220     }
221
222     static class UnmodifiableNodeManagerList extends UnmodifiableNodeList implements NodeManagerList {
223
224         UnmodifiableNodeManagerList(NodeManagerList nodeManagerList) {
225             super(nodeManagerList);
226         }
227
228         public NodeManager getNodeManager(int index) {
229             return ((NodeManagerList) c).getNodeManager(index);
230         }
231
232         public NodeManagerIterator nodeManagerIterator() {
233             return new UnmodifiableNodeManagerIterator(((NodeManagerList)c).nodeManagerIterator());
234         }
235     }
236
237     static class UnmodifiableRelationManagerList extends UnmodifiableNodeManagerList implements RelationManagerList {
238
239         UnmodifiableRelationManagerList(RelationManagerList relationManagerList) {
240             super(relationManagerList);
241         }
242
243         public RelationManager getRelationManager(int index) {
244             return ((RelationManagerList) c).getRelationManager(index);
245         }
246
247         public RelationManagerIterator relationManagerIterator() {
248             return new UnmodifiableRelationManagerIterator(((RelationManagerList)c).relationManagerIterator());
249         }
250     }
251
252     static class UnmodifiableRelationList extends UnmodifiableNodeList implements RelationList {
253
254         UnmodifiableRelationList(RelationList relationList) {
255             super(relationList);
256         }
257
258         public Relation getRelation(int index) {
259             return ((RelationList) c).getRelation(index);
260         }
261
262         public RelationIterator relationIterator() {
263             return new UnmodifiableRelationIterator(((RelationList)c).relationIterator());
264         }
265
266         public RelationList subRelationList(int fromIndex, int toIndex) {
267             return new UnmodifiableRelationList(((RelationList) c).subRelationList(fromIndex, toIndex));
268         }
269     }
270
271     static class UnmodifiableStringList extends UnmodifiableBridgeList implements StringList {
272
273         UnmodifiableStringList(StringList stringList) {
274             super(stringList);
275         }
276
277         public String JavaDoc getString(int index) {
278             return ((StringList) c).getString(index);
279         }
280
281         public StringIterator stringIterator() {
282             return new UnmodifiableStringIterator(((StringList)c).stringIterator());
283         }
284     }
285
286     /* --------------------------------------------------------------------------------
287      * Empty (and unmodifiable) Lists.
288      */

289     static class EmptyBridgeList extends UnmodifiableBridgeList {
290         EmptyBridgeList() { }
291
292         private static final Object JavaDoc[] EMPTY = new Object JavaDoc[] {};
293         public final int size() { return 0; }
294         public final boolean isEmpty() { return true; }
295         public final boolean contains(Object JavaDoc o) { return false; }
296         public final boolean containsAll(Collection col) { return col.isEmpty(); }
297         public final Object JavaDoc[] toArray() { return EMPTY; }
298         public String JavaDoc toString() { return "[]"; }
299         public final ListIterator listIterator(int c) { return Collections.EMPTY_LIST.listIterator(c); }
300         public Object JavaDoc get(int i) { throw new IndexOutOfBoundsException JavaDoc(); }
301         public Object JavaDoc getProperty(Object JavaDoc key) { return null; }
302         public List subList(int fromIndex, int toIndex) { throw new IndexOutOfBoundsException JavaDoc(); }
303         public int lastIndexOf(Object JavaDoc o) { return -1; }
304         public int indexOf(Object JavaDoc o) { return -1; }
305         public boolean equals(Object JavaDoc o) { return Collections.EMPTY_LIST.equals(o); }
306         public int hashCode() { return Collections.EMPTY_LIST.hashCode(); }
307     }
308
309     static class EmptyNodeList extends EmptyBridgeList implements NodeList {
310
311         public final Node getNode(int index) {
312             throw new IndexOutOfBoundsException JavaDoc("Index: "+index);
313         }
314
315         public final NodeIterator nodeIterator() {
316             return new UnmodifiableNodeIterator(null) {
317                 public boolean hasNext() { return false; }
318                 public boolean hasPrevious() { return false; }
319                 public Node nextNode() { throw new NoSuchElementException(); }
320                 public Node previousNode() { throw new NoSuchElementException(); }
321                 public Object JavaDoc next() { throw new NoSuchElementException(); }
322                 public Object JavaDoc previous() { throw new NoSuchElementException(); }
323             };
324         }
325
326         public NodeList subNodeList(int fromIndex, int toIndex) {
327             if (fromIndex == 0 && toIndex == 0) return this;
328             throw new IndexOutOfBoundsException JavaDoc();
329         }
330     }
331
332     static class EmptyRelationList extends EmptyNodeList implements RelationList {
333
334         public Relation getRelation(int index) {
335             throw new IndexOutOfBoundsException JavaDoc("Index: "+index);
336         }
337
338         public RelationIterator relationIterator() {
339             return new UnmodifiableRelationIterator(null) {
340                 public boolean hasNext() { return false; }
341                 public boolean hasPrevious() { return false; }
342                 public Relation nextRelation() { throw new NoSuchElementException(); }
343                 public Relation previousRelation() { throw new NoSuchElementException(); }
344                 public Node nextNode() { throw new NoSuchElementException(); }
345                 public Node previousNode() { throw new NoSuchElementException(); }
346                 public Object JavaDoc next() { throw new NoSuchElementException(); }
347                 public Object JavaDoc previous() { throw new NoSuchElementException(); }
348             };
349         }
350
351         public RelationList subRelationList(int fromIndex, int toIndex) {
352             if (fromIndex == 0 && toIndex == 0) return this;
353             throw new IndexOutOfBoundsException JavaDoc();
354         }
355     }
356
357     static class EmptyNodeManagerList extends EmptyNodeList implements NodeManagerList {
358
359         public NodeManager getNodeManager(int index) {
360             throw new IndexOutOfBoundsException JavaDoc("Index: "+index);
361         }
362
363         public NodeManagerIterator nodeManagerIterator() {
364             return new UnmodifiableNodeManagerIterator(null) {
365                 public boolean hasNext() { return false; }
366                 public boolean hasPrevious() { return false; }
367                 public NodeManager nextNodeManager() { throw new NoSuchElementException(); }
368                 public NodeManager previousNodeManager() { throw new NoSuchElementException(); }
369                 public Node nextNode() { throw new NoSuchElementException(); }
370                 public Node previousNode() { throw new NoSuchElementException(); }
371                 public Object JavaDoc next() { throw new NoSuchElementException(); }
372                 public Object JavaDoc previous() { throw new NoSuchElementException(); }
373             };
374         }
375     }
376
377     static class EmptyRelationManagerList extends EmptyNodeManagerList implements RelationManagerList {
378
379         public RelationManager getRelationManager(int index) {
380             throw new IndexOutOfBoundsException JavaDoc("Index: "+index);
381         }
382
383         public RelationManagerIterator relationManagerIterator() {
384             return new UnmodifiableRelationManagerIterator(null) {
385                 public boolean hasNext() { return false; }
386                 public boolean hasPrevious() { return false; }
387                 public RelationManager nextRelationManager() { throw new NoSuchElementException(); }
388                 public RelationManager previousRelationManager() { throw new NoSuchElementException(); }
389                 public NodeManager nextNodeManager() { throw new NoSuchElementException(); }
390                 public NodeManager previousNodeManager() { throw new NoSuchElementException(); }
391                 public Node nextNode() { throw new NoSuchElementException(); }
392                 public Node previousNode() { throw new NoSuchElementException(); }
393                 public Object JavaDoc next() { throw new NoSuchElementException(); }
394                 public Object JavaDoc previous() { throw new NoSuchElementException(); }
395             };
396         }
397     }
398
399     static class EmptyStringList extends EmptyBridgeList implements StringList {
400         public String JavaDoc getString(int index) {
401             throw new IndexOutOfBoundsException JavaDoc("Index: "+index);
402         }
403
404         public StringIterator stringIterator() {
405             return new UnmodifiableStringIterator(null) {
406                 public boolean hasNext() { return false; }
407                 public boolean hasPrevious() { return false; }
408                 public String JavaDoc nextString() { throw new NoSuchElementException(); }
409                 public String JavaDoc previousString() { throw new NoSuchElementException(); }
410             };
411         }
412     }
413
414 }
415
Popular Tags