001: /*
002:
003: This software is OSI Certified Open Source Software.
004: OSI Certified is a certification mark of the Open Source Initiative.
005:
006: The license (Mozilla version 1.0) can be read at the MMBase site.
007: See http://www.MMBase.org/license
008:
009: */
010:
011: package org.mmbase.bridge.util;
012:
013: import java.io.Serializable;
014: import java.util.*;
015: import org.mmbase.bridge.*;
016:
017: /**
018: * Analogon of {@link java.util.Collections}. Methods speak for themselves.
019: *
020: *
021: * @author Michiel Meeuwissen
022: * @version $Id: BridgeCollections.java,v 1.10 2007/02/24 21:57:50 nklasens Exp $
023: * @since MMBase-1.8
024: */
025:
026: public abstract class BridgeCollections {
027:
028: /**
029: * Makes a BridgeList unmodifiable.
030: */
031: public static final <E> BridgeList<E> unmodifiableBridgeList(
032: BridgeList<E> bridgeList) {
033: return new UnmodifiableBridgeList<E>(bridgeList);
034: }
035:
036: /**
037: * Makes a NodeList unmodifiable.
038: */
039: public static final NodeList unmodifiableNodeList(NodeList nodeList) {
040: return new UnmodifiableNodeList(nodeList);
041: }
042:
043: /**
044: * Makes a NodeManagerList unmodifiable.
045: */
046: public static final NodeManagerList unmodifiableNodeManagerList(
047: NodeManagerList nodeList) {
048: return new UnmodifiableNodeManagerList(nodeList);
049: }
050:
051: /**
052: * Makes a RelationManagerList unmodifiable.
053: */
054: public static final RelationManagerList unmodifiableRelationManagerList(
055: RelationManagerList nodeList) {
056: return new UnmodifiableRelationManagerList(nodeList);
057: }
058:
059: /**
060: * Makes a RelationList unmodifiable.
061: */
062: public static final RelationList unmodifiableRelationList(
063: RelationList relationList) {
064: return new UnmodifiableRelationList(relationList);
065: }
066:
067: /**
068: * Makes a StringList unmodifiable.
069: */
070: public static final StringList unmodifiableStringList(
071: StringList stringList) {
072: return new UnmodifiableStringList(stringList);
073: }
074:
075: public static final NodeList EMPTY_NODELIST = new EmptyNodeList();
076: public static final NodeManagerList EMPTY_NODEMANAGERLIST = new EmptyNodeManagerList();
077: public static final RelationManagerList EMPTY_RELATIONMANAGERLIST = new EmptyRelationManagerList();
078: public static final RelationList EMPTY_RELATIONLIST = new EmptyRelationList();
079: public static final StringList EMPTY_STRINGLIST = new EmptyStringList();
080:
081: // IMPLEMENTATIONS follow below.
082:
083: /* --------------------------------------------------------------------------------
084: * Unmodifiable iterators
085: */
086: static class UnmodifiableListIterator<E> implements ListIterator<E> {
087: final protected ListIterator<E> i;
088:
089: UnmodifiableListIterator(ListIterator<E> i) {
090: this .i = i;
091: }
092:
093: public boolean hasNext() {
094: return i.hasNext();
095: }
096:
097: public boolean hasPrevious() {
098: return i.hasPrevious();
099: }
100:
101: public E next() {
102: return i.next();
103: }
104:
105: public E previous() {
106: return i.previous();
107: }
108:
109: public int nextIndex() {
110: return i.nextIndex();
111: }
112:
113: public int previousIndex() {
114: return i.previousIndex();
115: }
116:
117: public void remove() {
118: throw new UnsupportedOperationException();
119: }
120:
121: public void add(Object o) {
122: throw new UnsupportedOperationException();
123: }
124:
125: public void set(Object o) {
126: throw new UnsupportedOperationException();
127: }
128: }
129:
130: static class UnmodifiableNodeIterator extends
131: UnmodifiableListIterator<Node> implements NodeIterator {
132: UnmodifiableNodeIterator(NodeIterator i) {
133: super (i);
134: }
135:
136: public Node nextNode() {
137: return ((NodeIterator) i).nextNode();
138: }
139:
140: public Node previousNode() {
141: return ((NodeIterator) i).previousNode();
142: }
143: }
144:
145: static class UnmodifiableNodeManagerIterator extends
146: UnmodifiableListIterator<NodeManager> implements
147: NodeManagerIterator {
148: UnmodifiableNodeManagerIterator(NodeManagerIterator i) {
149: super (i);
150: }
151:
152: public NodeManager nextNodeManager() {
153: return ((NodeManagerIterator) i).nextNodeManager();
154: }
155:
156: public NodeManager previousNodeManager() {
157: return ((NodeManagerIterator) i).previousNodeManager();
158: }
159: }
160:
161: static class UnmodifiableRelationManagerIterator extends
162: UnmodifiableListIterator<RelationManager> implements
163: RelationManagerIterator {
164: UnmodifiableRelationManagerIterator(RelationManagerIterator i) {
165: super (i);
166: }
167:
168: public RelationManager nextRelationManager() {
169: return ((RelationManagerIterator) i).nextRelationManager();
170: }
171:
172: public RelationManager previousRelationManager() {
173: return ((RelationManagerIterator) i)
174: .previousRelationManager();
175: }
176: }
177:
178: static class UnmodifiableRelationIterator extends
179: UnmodifiableListIterator<Relation> implements
180: RelationIterator {
181: UnmodifiableRelationIterator(RelationIterator i) {
182: super (i);
183: }
184:
185: public Relation nextRelation() {
186: return ((RelationIterator) i).nextRelation();
187: }
188:
189: public Relation previousRelation() {
190: return ((RelationIterator) i).previousRelation();
191: }
192: }
193:
194: static class UnmodifiableStringIterator extends
195: UnmodifiableListIterator<String> implements StringIterator {
196: UnmodifiableStringIterator(StringIterator i) {
197: super (i);
198: }
199:
200: public String nextString() {
201: return ((StringIterator) i).nextString();
202: }
203:
204: public String previousString() {
205: return ((StringIterator) i).previousString();
206: }
207:
208: }
209:
210: /* --------------------------------------------------------------------------------
211: * Unmodifiable Lists.
212: */
213: static class UnmodifiableBridgeList<E> implements BridgeList<E> {
214: final List<E> c;
215: final BridgeList<E> parent; // just to expose properties to sublists.
216:
217: UnmodifiableBridgeList() {
218: c = new EmptyBridgeList<E>() {
219: };
220: parent = null;
221: }
222:
223: UnmodifiableBridgeList(BridgeList<E> c) {
224: if (c == null) {
225: throw new NullPointerException();
226: }
227: this .c = c;
228: this .parent = null;
229: }
230:
231: UnmodifiableBridgeList(List<E> c, BridgeList<E> parent) {
232: if (c == null) {
233: throw new NullPointerException();
234: }
235: this .c = c;
236: this .parent = parent;
237: }
238:
239: public int size() {
240: return c.size();
241: }
242:
243: public boolean isEmpty() {
244: return c.isEmpty();
245: }
246:
247: public boolean contains(Object o) {
248: return c.contains(o);
249: }
250:
251: public Object[] toArray() {
252: return c.toArray();
253: }
254:
255: public <T> T[] toArray(T[] a) {
256: return c.toArray(a);
257: }
258:
259: @Override
260: public String toString() {
261: return c.toString();
262: }
263:
264: public ListIterator<E> listIterator(final int s) {
265: return new UnmodifiableListIterator<E>(c.listIterator(s));
266: }
267:
268: public ListIterator<E> listIterator() {
269: return listIterator(0);
270: }
271:
272: public Iterator<E> iterator() {
273: return listIterator();
274: }
275:
276: public boolean add(Object o) {
277: throw new UnsupportedOperationException();
278: }
279:
280: public void add(int i, Object o) {
281: throw new UnsupportedOperationException();
282: }
283:
284: public E set(int i, Object o) {
285: throw new UnsupportedOperationException();
286: }
287:
288: public boolean remove(Object o) {
289: throw new UnsupportedOperationException();
290: }
291:
292: public E remove(int i) {
293: throw new UnsupportedOperationException();
294: }
295:
296: public boolean containsAll(Collection<?> coll) {
297: return c.containsAll(coll);
298: }
299:
300: public boolean addAll(Collection<? extends E> coll) {
301: throw new UnsupportedOperationException();
302: }
303:
304: public boolean addAll(int i, Collection<? extends E> coll) {
305: throw new UnsupportedOperationException();
306: }
307:
308: public boolean removeAll(Collection<?> coll) {
309: throw new UnsupportedOperationException();
310: }
311:
312: public boolean retainAll(Collection<?> coll) {
313: throw new UnsupportedOperationException();
314: }
315:
316: public void clear() {
317: throw new UnsupportedOperationException();
318: }
319:
320: public E get(int i) {
321: return c.get(i);
322: }
323:
324: public Object getProperty(Object key) {
325: if (parent != null)
326: return parent.getProperty(key);
327: return ((BridgeList<E>) c).getProperty(key);
328: }
329:
330: public void setProperty(Object key, Object value) {
331: throw new UnsupportedOperationException();
332: }
333:
334: public void sort() {
335: throw new UnsupportedOperationException();
336: }
337:
338: public void sort(Comparator<? super E> comparator) {
339: throw new UnsupportedOperationException();
340: }
341:
342: public BridgeList<E> subList(int fromIndex, int toIndex) {
343: return new UnmodifiableBridgeList<E>(c.subList(fromIndex,
344: toIndex), parent != null ? parent
345: : (BridgeList<E>) c);
346: }
347:
348: public int lastIndexOf(Object o) {
349: return c.lastIndexOf(o);
350: }
351:
352: public int indexOf(Object o) {
353: return c.indexOf(o);
354: }
355:
356: @Override
357: public boolean equals(Object o) {
358: return c.equals(o);
359: }
360:
361: @Override
362: public int hashCode() {
363: return c.hashCode();
364: }
365: }
366:
367: static class UnmodifiableNodeList extends
368: UnmodifiableBridgeList<Node> implements NodeList {
369:
370: UnmodifiableNodeList(NodeList nodeList) {
371: super (nodeList);
372: }
373:
374: public Node getNode(int index) {
375: return ((NodeList) c).getNode(index);
376: }
377:
378: public NodeIterator nodeIterator() {
379: return new UnmodifiableNodeIterator(((NodeList) c)
380: .nodeIterator());
381: }
382:
383: public NodeList subNodeList(int fromIndex, int toIndex) {
384: return new UnmodifiableNodeList(((NodeList) c).subNodeList(
385: fromIndex, toIndex));
386: }
387: }
388:
389: static class UnmodifiableNodeManagerList extends
390: UnmodifiableBridgeList<NodeManager> implements
391: NodeManagerList {
392:
393: UnmodifiableNodeManagerList(NodeManagerList nodeManagerList) {
394: super (nodeManagerList);
395: }
396:
397: public NodeManager getNodeManager(int index) {
398: return ((NodeManagerList) c).getNodeManager(index);
399: }
400:
401: public NodeManagerIterator nodeManagerIterator() {
402: return new UnmodifiableNodeManagerIterator(
403: ((NodeManagerList) c).nodeManagerIterator());
404: }
405: }
406:
407: static class UnmodifiableRelationManagerList extends
408: UnmodifiableBridgeList<RelationManager> implements
409: RelationManagerList {
410:
411: UnmodifiableRelationManagerList(
412: RelationManagerList relationManagerList) {
413: super (relationManagerList);
414: }
415:
416: public RelationManager getRelationManager(int index) {
417: return ((RelationManagerList) c).getRelationManager(index);
418: }
419:
420: public RelationManagerIterator relationManagerIterator() {
421: return new UnmodifiableRelationManagerIterator(
422: ((RelationManagerList) c).relationManagerIterator());
423: }
424: }
425:
426: static class UnmodifiableRelationList extends
427: UnmodifiableBridgeList<Relation> implements RelationList {
428:
429: UnmodifiableRelationList(RelationList relationList) {
430: super (relationList);
431: }
432:
433: public Relation getRelation(int index) {
434: return ((RelationList) c).getRelation(index);
435: }
436:
437: public RelationIterator relationIterator() {
438: return new UnmodifiableRelationIterator(((RelationList) c)
439: .relationIterator());
440: }
441:
442: public RelationList subRelationList(int fromIndex, int toIndex) {
443: return new UnmodifiableRelationList(((RelationList) c)
444: .subRelationList(fromIndex, toIndex));
445: }
446: }
447:
448: static class UnmodifiableStringList extends
449: UnmodifiableBridgeList<String> implements StringList {
450:
451: UnmodifiableStringList(StringList stringList) {
452: super (stringList);
453: }
454:
455: public String getString(int index) {
456: return ((StringList) c).getString(index);
457: }
458:
459: public StringIterator stringIterator() {
460: return new UnmodifiableStringIterator(((StringList) c)
461: .stringIterator());
462: }
463: }
464:
465: /* --------------------------------------------------------------------------------
466: * Empty (and unmodifiable) Lists.
467: */
468: static abstract class EmptyBridgeList<E> extends AbstractList<E>
469: implements BridgeList<E>, Serializable {
470: EmptyBridgeList() {
471: }
472:
473: public final int size() {
474: return 0;
475: }
476:
477: public E get(int i) {
478: throw new IndexOutOfBoundsException();
479: }
480:
481: public boolean contains(Object obj) {
482: return false;
483: }
484:
485: public Object getProperty(Object key) {
486: return null;
487: }
488:
489: public BridgeList<E> subList(int fromIndex, int toIndex) {
490: throw new IndexOutOfBoundsException();
491: }
492:
493: public void setProperty(Object key, Object value) {
494: throw new UnsupportedOperationException();
495: }
496:
497: public void sort() {
498: throw new UnsupportedOperationException();
499: }
500:
501: public void sort(Comparator<? super E> comparator) {
502: throw new UnsupportedOperationException();
503: }
504: }
505:
506: static class EmptyNodeList extends EmptyBridgeList<Node> implements
507: NodeList {
508:
509: public final Node getNode(int index) {
510: throw new IndexOutOfBoundsException("Index: " + index);
511: }
512:
513: public final NodeIterator nodeIterator() {
514: return new UnmodifiableNodeIterator(null) {
515: @Override
516: public boolean hasNext() {
517: return false;
518: }
519:
520: @Override
521: public boolean hasPrevious() {
522: return false;
523: }
524:
525: @Override
526: public Node nextNode() {
527: throw new NoSuchElementException();
528: }
529:
530: @Override
531: public Node previousNode() {
532: throw new NoSuchElementException();
533: }
534:
535: @Override
536: public Node next() {
537: throw new NoSuchElementException();
538: }
539:
540: @Override
541: public Node previous() {
542: throw new NoSuchElementException();
543: }
544: };
545: }
546:
547: public NodeList subNodeList(int fromIndex, int toIndex) {
548: if (fromIndex == 0 && toIndex == 0)
549: return this ;
550: throw new IndexOutOfBoundsException();
551: }
552:
553: // Preserves singleton property
554: protected Object readResolve() {
555: return EMPTY_NODELIST;
556: }
557: }
558:
559: static class EmptyRelationList extends EmptyBridgeList<Relation>
560: implements RelationList {
561:
562: public Relation getRelation(int index) {
563: throw new IndexOutOfBoundsException("Index: " + index);
564: }
565:
566: public RelationIterator relationIterator() {
567: return new UnmodifiableRelationIterator(null) {
568: @Override
569: public boolean hasNext() {
570: return false;
571: }
572:
573: @Override
574: public boolean hasPrevious() {
575: return false;
576: }
577:
578: @Override
579: public Relation nextRelation() {
580: throw new NoSuchElementException();
581: }
582:
583: @Override
584: public Relation previousRelation() {
585: throw new NoSuchElementException();
586: }
587:
588: @Override
589: public Relation next() {
590: throw new NoSuchElementException();
591: }
592:
593: @Override
594: public Relation previous() {
595: throw new NoSuchElementException();
596: }
597: };
598: }
599:
600: public RelationList subRelationList(int fromIndex, int toIndex) {
601: if (fromIndex == 0 && toIndex == 0)
602: return this ;
603: throw new IndexOutOfBoundsException();
604: }
605:
606: protected Object readResolve() {
607: return EMPTY_RELATIONLIST;
608: }
609: }
610:
611: static class EmptyNodeManagerList extends
612: EmptyBridgeList<NodeManager> implements NodeManagerList {
613:
614: public NodeManager getNodeManager(int index) {
615: throw new IndexOutOfBoundsException("Index: " + index);
616: }
617:
618: public NodeManagerIterator nodeManagerIterator() {
619: return new UnmodifiableNodeManagerIterator(null) {
620: @Override
621: public boolean hasNext() {
622: return false;
623: }
624:
625: @Override
626: public boolean hasPrevious() {
627: return false;
628: }
629:
630: @Override
631: public NodeManager nextNodeManager() {
632: throw new NoSuchElementException();
633: }
634:
635: @Override
636: public NodeManager previousNodeManager() {
637: throw new NoSuchElementException();
638: }
639:
640: @Override
641: public NodeManager next() {
642: throw new NoSuchElementException();
643: }
644:
645: @Override
646: public NodeManager previous() {
647: throw new NoSuchElementException();
648: }
649: };
650: }
651:
652: protected Object readResolve() {
653: return EMPTY_NODEMANAGERLIST;
654: }
655: }
656:
657: static class EmptyRelationManagerList extends
658: EmptyBridgeList<RelationManager> implements
659: RelationManagerList {
660:
661: public RelationManager getRelationManager(int index) {
662: throw new IndexOutOfBoundsException("Index: " + index);
663: }
664:
665: public RelationManagerIterator relationManagerIterator() {
666: return new UnmodifiableRelationManagerIterator(null) {
667: @Override
668: public boolean hasNext() {
669: return false;
670: }
671:
672: @Override
673: public boolean hasPrevious() {
674: return false;
675: }
676:
677: @Override
678: public RelationManager nextRelationManager() {
679: throw new NoSuchElementException();
680: }
681:
682: @Override
683: public RelationManager previousRelationManager() {
684: throw new NoSuchElementException();
685: }
686:
687: @Override
688: public RelationManager next() {
689: throw new NoSuchElementException();
690: }
691:
692: @Override
693: public RelationManager previous() {
694: throw new NoSuchElementException();
695: }
696: };
697: }
698:
699: protected Object readResolve() {
700: return EMPTY_RELATIONMANAGERLIST;
701: }
702: }
703:
704: static class EmptyStringList extends EmptyBridgeList<String>
705: implements StringList {
706: public String getString(int index) {
707: throw new IndexOutOfBoundsException("Index: " + index);
708: }
709:
710: public StringIterator stringIterator() {
711: return new UnmodifiableStringIterator(null) {
712: @Override
713: public boolean hasNext() {
714: return false;
715: }
716:
717: @Override
718: public boolean hasPrevious() {
719: return false;
720: }
721:
722: @Override
723: public String nextString() {
724: throw new NoSuchElementException();
725: }
726:
727: @Override
728: public String previousString() {
729: throw new NoSuchElementException();
730: }
731: };
732: }
733:
734: protected Object readResolve() {
735: return EMPTY_STRINGLIST;
736: }
737: }
738:
739: }
|