KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.mmbase.bridge.*;
14 import org.mmbase.bridge.implementation.BasicList;
15 import java.util.*;
16 import org.mmbase.util.Casting;
17 import org.mmbase.util.logging.*;
18
19 /**
20  * A (fixed-size) list of nodes, based on a Collection of Nodes. If the collection is a List it
21  * will mirror its' changes. Otherwise, no, because the complete collection is inserted in its own
22  * one.
23  *
24  * @author Michiel Meeuwissen
25  * @version $Id: CollectionNodeList.java,v 1.3 2005/12/30 10:38:10 michiel Exp $
26  * @since MMBase-1.8
27  */

28 public class CollectionNodeList extends AbstractBridgeList implements NodeList {
29
30     private static final Logger log = Logging.getLoggerInstance(CollectionNodeList.class);
31     protected Cloud cloud;
32     protected NodeManager nodeManager = null;
33
34     protected final List wrappedCollection;
35
36
37
38     public CollectionNodeList(Collection c, NodeManager nodeManager) {
39         this.nodeManager = nodeManager;
40         this.cloud = nodeManager.getCloud();
41         this.wrappedCollection = convertedList(c, cloud);
42     }
43
44
45     public CollectionNodeList(Collection c, Cloud cloud) {
46         this.cloud = cloud;
47         this.wrappedCollection = convertedList(c, cloud);
48     }
49     public CollectionNodeList(Collection c) {
50         this(c, ContextProvider.getDefaultCloudContext().getCloud("mmbase", "class", null));
51     }
52
53     private static List convertedList(Collection c, Cloud cloud) {
54         if (c instanceof List) {
55             return (List) c;
56         } else {
57             NodeList l = cloud.createNodeList();
58             l.addAll(c);
59             return l;
60         }
61     }
62
63
64     public int size() {
65         return wrappedCollection.size();
66     }
67     public Object JavaDoc get(int index) {
68         return convert(wrappedCollection.get(index), index);
69     }
70
71     public Object JavaDoc set(int index, Object JavaDoc o) {
72         return wrappedCollection.set(index, o);
73     }
74
75     /**
76      */

77     protected Object JavaDoc convert(Object JavaDoc o, int index) {
78         if (o instanceof Node || o == null) {
79             return o;
80         }
81         Node node = null;
82         if (o instanceof String JavaDoc) { // a string indicates a nodemanager by name
83
node = cloud.getNodeManager((String JavaDoc)o);
84         } else if (o instanceof Map) {
85             if (nodeManager != null) {
86                 node = new MapNode((Map) o, nodeManager);
87             } else {
88                 node = new MapNode((Map) o, cloud);
89             }
90         } else {
91             if (! (wrappedCollection instanceof NodeList)) {
92                 // last desperate try, depend on a nodelist of cloud (that know how to convert core objects..)
93
NodeList nl = cloud.createNodeList(); // hackery
94
nl.add(o);
95                 node = nl.getNode(0);
96             } else {
97                 // even more desperate!
98
node = cloud.getNode(Casting.toString(o));
99             }
100         }
101         set(index, node);
102         return node;
103     }
104
105     /**
106      *
107      */

108     public Node getNode(int index) {
109         return (Node)get(index);
110     }
111
112     /**
113      *
114      */

115     public NodeList subNodeList(int fromIndex, int toIndex) {
116         return new CollectionNodeList(subList(fromIndex, toIndex), cloud);
117     }
118
119     /**
120      *
121      */

122     public NodeIterator nodeIterator() {
123         return new BasicNodeIterator();
124     }
125
126
127     protected class BasicNodeIterator extends BasicIterator implements NodeIterator {
128
129         public Node nextNode() {
130             return (Node)next();
131         }
132
133         public Node previousNode() {
134             return (Node)previous();
135         }
136     }
137 }
138
Popular Tags