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: package org.mmbase.bridge.util;
011:
012: import java.util.*;
013:
014: import org.mmbase.bridge.*;
015: import org.mmbase.bridge.implementation.AbstractNodeList;
016: import org.mmbase.util.Casting;
017:
018: public abstract class AbstractCollectionNodeList<E extends Node>
019: extends AbstractBridgeList<E> {
020:
021: protected Cloud cloud;
022: protected final NodeManager nodeManager;
023: protected final List wrappedCollection;
024:
025: public AbstractCollectionNodeList(Collection<Node> c,
026: NodeManager nodeManager) {
027: this .nodeManager = nodeManager;
028: this .cloud = nodeManager.getCloud();
029: this .wrappedCollection = convertedList(c, cloud);
030: }
031:
032: public AbstractCollectionNodeList(Collection<Node> c, Cloud cloud) {
033: this .nodeManager = null;
034: this .cloud = cloud;
035: this .wrappedCollection = convertedList(c, cloud);
036: }
037:
038: public AbstractCollectionNodeList(Collection c) {
039: this (c, (Cloud) null);
040: }
041:
042: @Override
043: public int size() {
044: return wrappedCollection.size();
045: }
046:
047: @Override
048: public E get(int index) {
049: return (E) convert(wrappedCollection.get(index), index);
050: }
051:
052: @Override
053: public E set(int index, E o) {
054: E prev = get(index);
055: wrappedCollection.set(index, o);
056: return prev;
057: }
058:
059: @Override
060: public boolean add(E o) {
061: if (o == null)
062: throw new IllegalArgumentException();
063: return super .add(o);
064: }
065:
066: public Collection<Node> getCollection() {
067: return wrappedCollection;
068: }
069:
070: private static List<Node> convertedList(Collection<Node> c,
071: Cloud cloud) {
072: if (c instanceof List) {
073: return (List<Node>) c;
074: } else {
075: if (cloud == null) {
076: cloud = ContextProvider.getDefaultCloudContext()
077: .getCloud("mmbase", "class", null);
078: }
079: NodeList l = cloud.createNodeList();
080: l.addAll(c);
081: return l;
082: }
083: }
084:
085: protected Object convert(Object o, int index) {
086: if (o instanceof Node || o == null) {
087: return o;
088: }
089: Node node = null;
090: if (o instanceof String) { // a string indicates a nodemanager by name
091: node = getCloud().getNodeManager((String) o);
092: } else if (o instanceof Map) {
093: if (nodeManager != null) {
094: node = new MapNode((Map) o, nodeManager);
095: } else {
096: node = new MapNode((Map) o, getCloud());
097: }
098: } else {
099: if (!(wrappedCollection instanceof NodeList)) {
100: // last desperate try, depend on a nodelist of cloud (that know how to convert core objects..)
101: // hackery
102: node = AbstractNodeList.convertWithBridgeToNode(cloud,
103: nodeManager, o);
104: } else {
105: // even more desperate!
106: node = getCloud().getNode(Casting.toString(o));
107: }
108: }
109: wrappedCollection.set(index, node);
110: return node;
111: }
112:
113: protected Cloud getCloud() {
114: if (cloud == null) {
115: cloud = ContextProvider.getDefaultCloudContext().getCloud(
116: "mmbase", "class", null);
117: }
118: return cloud;
119: }
120:
121: }
|