1 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 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 get(int index) { 68 return convert(wrappedCollection.get(index), index); 69 } 70 71 public Object set(int index, Object o) { 72 return wrappedCollection.set(index, o); 73 } 74 75 77 protected Object convert(Object o, int index) { 78 if (o instanceof Node || o == null) { 79 return o; 80 } 81 Node node = null; 82 if (o instanceof String ) { node = cloud.getNodeManager((String )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 NodeList nl = cloud.createNodeList(); nl.add(o); 95 node = nl.getNode(0); 96 } else { 97 node = cloud.getNode(Casting.toString(o)); 99 } 100 } 101 set(index, node); 102 return node; 103 } 104 105 108 public Node getNode(int index) { 109 return (Node)get(index); 110 } 111 112 115 public NodeList subNodeList(int fromIndex, int toIndex) { 116 return new CollectionNodeList(subList(fromIndex, toIndex), cloud); 117 } 118 119 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 |