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.util.*;
014:
015: import org.mmbase.bridge.*;
016:
017: /**
018: * A Map representing a Node. This class can be used if you need a bridge Node object to look like a
019: * Map (where the keys are the fields).
020: *
021: * This object is also still a Node object.
022: *
023: * @author Michiel Meeuwissen
024: * @version $Id: NodeMap.java,v 1.4 2007/02/11 20:05:50 nklasens Exp $
025: * @since MMBase-1.8
026: */
027:
028: public class NodeMap extends NodeWrapper implements Map<String, Object> {
029:
030: /**
031: * @param node The Node which is wrapped, and is presented as a Map.
032: */
033: public NodeMap(Node node) {
034: super (node);
035: }
036:
037: // javadoc inherited
038: public void clear() {
039: // the fields of a node are fixed by it's nodemanager.
040: throw new UnsupportedOperationException(
041: "You cannot remove fields from a Node.");
042: }
043:
044: // javadoc inherited
045: public boolean containsKey(Object key) {
046: if (key instanceof String) {
047: return getNodeManager().hasField((String) key);
048: } else {
049: return false;
050: }
051: }
052:
053: // javadoc inherited
054: // code copied from AbstractMap
055: public boolean containsValue(Object value) {
056: Iterator<Entry<String, Object>> i = entrySet().iterator();
057: if (value == null) {
058: while (i.hasNext()) {
059: Entry<String, Object> e = i.next();
060: if (e.getValue() == null) {
061: return true;
062: }
063: }
064: } else {
065: while (i.hasNext()) {
066: Entry<String, Object> e = i.next();
067: if (value.equals(e.getValue())) {
068: return true;
069: }
070: }
071: }
072: return false;
073: }
074:
075: // javadoc inherited
076: public Object remove(Object key) {
077: throw new UnsupportedOperationException(
078: "You cannot remove fields from a Node.");
079: }
080:
081: // javadoc inherited
082: public Set<Entry<String, Object>> entrySet() {
083: return new AbstractSet<Entry<String, Object>>() {
084: FieldList fields = getNodeManager().getFields();
085:
086: @Override
087: public Iterator<Entry<String, Object>> iterator() {
088: return new Iterator<Entry<String, Object>>() {
089: FieldIterator i = fields.fieldIterator();
090:
091: public boolean hasNext() {
092: return i.hasNext();
093: }
094:
095: public Entry<String, Object> next() {
096: return new Map.Entry<String, Object>() {
097: Field field = i.nextField();
098:
099: public String getKey() {
100: return field.getName();
101: }
102:
103: public Object getValue() {
104: return NodeMap.this .getValue(field
105: .getName());
106: }
107:
108: public Object setValue(Object value) {
109: Object r = getValue();
110: NodeMap.this .setValue(field.getName(),
111: value);
112: return r;
113: }
114: };
115: }
116:
117: public void remove() {
118: throw new UnsupportedOperationException(
119: "You cannot remove fields from a Node.");
120: }
121: };
122: }
123:
124: @Override
125: public int size() {
126: return fields.size();
127: }
128: };
129: }
130:
131: // javadoc inherited
132: // todo: could be modifiable?
133: public Collection<Object> values() {
134: return new AbstractCollection<Object>() {
135: FieldList fields = getNodeManager().getFields();
136:
137: @Override
138: public Iterator<Object> iterator() {
139: return new Iterator<Object>() {
140: FieldIterator i = fields.fieldIterator();
141:
142: public boolean hasNext() {
143: return i.hasNext();
144: }
145:
146: public Object next() {
147: Field field = i.nextField();
148: return NodeMap.this .getValue(field.getName());
149: }
150:
151: public void remove() {
152: throw new UnsupportedOperationException(
153: "You cannot remove fields from a Node.");
154: }
155: };
156: }
157:
158: @Override
159: public int size() {
160: return fields.size();
161: }
162: };
163: }
164:
165: // javadoc inherited
166: public Set<String> keySet() {
167: return new AbstractSet<String>() {
168: FieldList fields = getNodeManager().getFields();
169:
170: @Override
171: public Iterator<String> iterator() {
172: return new Iterator<String>() {
173: FieldIterator i = fields.fieldIterator();
174:
175: public boolean hasNext() {
176: return i.hasNext();
177: }
178:
179: public String next() {
180: Field field = i.nextField();
181: return field.getName();
182: }
183:
184: public void remove() {
185: throw new UnsupportedOperationException(
186: "You cannot remove fields from a Node.");
187: }
188: };
189: }
190:
191: @Override
192: public int size() {
193: return fields.size();
194: }
195: };
196: }
197:
198: // javadoc inherited
199: public void putAll(Map<? extends String, ? extends Object> map) {
200: for (java.util.Map.Entry<? extends String, ? extends Object> e : map
201: .entrySet()) {
202: put(e.getKey(), e.getValue());
203: }
204: }
205:
206: // javadoc inherited
207: public Object put(String key, Object value) {
208: Object r = getValue(key);
209: setValue(key, value);
210: return r;
211: }
212:
213: // javadoc inherited
214: public Object get(Object key) {
215: return getValue((String) key);
216: }
217:
218: // javadoc inherited
219: public boolean isEmpty() {
220: return false;
221: }
222:
223: // javadoc inherited
224: public int size() {
225: return getNodeManager().getFields().size();
226: }
227: }
|