KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.*;
14 import org.mmbase.bridge.*;
15
16 /**
17  * A Map representing a Node. This class can be used if you need a bridge Node object to look like a
18  * Map (where the keys are the fields).
19  *
20  * This object is also still a Node object.
21  *
22  * @author Michiel Meeuwissen
23  * @version $Id: NodeMap.java,v 1.1 2005/12/27 21:50:50 michiel Exp $
24  * @since MMBase-1.8
25  */

26
27 public class NodeMap extends NodeWrapper implements Map {
28
29     /**
30      * @param node The Node which is wrapped, and is presented as a Map.
31      */

32     public NodeMap(Node node) {
33         super(node);
34     }
35
36     // javadoc inherited
37
public void clear() {
38         // the fields of a node are fixed by it's nodemanager.
39
throw new UnsupportedOperationException JavaDoc("You cannot remove fields from a Node.");
40     }
41
42     // javadoc inherited
43
public boolean containsKey(Object JavaDoc key) {
44         return getNodeManager().hasField((String JavaDoc) key);
45     }
46
47     // javadoc inherited
48
// code copied from AbstractMap
49
public boolean containsValue(Object JavaDoc value) {
50         Iterator i = entrySet().iterator();
51         if (value==null) {
52             while (i.hasNext()) {
53                 Entry e = (Entry) i.next();
54                 if (e.getValue()==null) {
55                     return true;
56                 }
57             }
58         } else {
59             while (i.hasNext()) {
60                 Entry e = (Entry) i.next();
61                 if (value.equals(e.getValue())) {
62                     return true;
63                 }
64             }
65         }
66         return false;
67     }
68
69     // javadoc inherited
70
public Object JavaDoc remove(Object JavaDoc key) {
71         throw new UnsupportedOperationException JavaDoc("You cannot remove fields from a Node.");
72     }
73
74     // javadoc inherited
75
public Set entrySet() {
76         return new AbstractSet() {
77                 FieldList fields = getNodeManager().getFields();
78                 public Iterator iterator() {
79                     return new Iterator() {
80                             FieldIterator i = fields.fieldIterator();
81                             public boolean hasNext() { return i.hasNext();}
82                             public Object JavaDoc next() {
83                                 return new Map.Entry() {
84                                         Field field = i.nextField();
85                                         public Object JavaDoc getKey() {
86                                             return field.getName();
87                                         }
88                                         public Object JavaDoc getValue() {
89                                             return NodeMap.this.getValue(field.getName());
90                                         }
91                                         public Object JavaDoc setValue(Object JavaDoc value) {
92                                             Object JavaDoc r = getValue();
93                                             NodeMap.this.setValue(field.getName(), value);
94                                             return r;
95                                         }
96                                     };
97                             }
98                             public void remove() {
99                                 throw new UnsupportedOperationException JavaDoc("You cannot remove fields from a Node.");
100                             }
101                         };
102                 }
103                 public int size() {
104                     return fields.size();
105                 }
106             };
107     }
108
109     // javadoc inherited
110
// todo: could be modifiable?
111
public Collection values() {
112         return new AbstractCollection() {
113                 FieldList fields = getNodeManager().getFields();
114                 public Iterator iterator() {
115                     return new Iterator() {
116                             FieldIterator i = fields.fieldIterator();
117                             public boolean hasNext() { return i.hasNext();}
118                             public Object JavaDoc next() {
119                                 Field field = i.nextField();
120                                 return NodeMap.this.getValue(field.getName());
121                             }
122                             public void remove() {
123                                 throw new UnsupportedOperationException JavaDoc("You cannot remove fields from a Node.");
124                             }
125                         };
126                 }
127                 public int size() {
128                     return fields.size();
129                 }
130             };
131     }
132
133     // javadoc inherited
134
public Set keySet() {
135         return new AbstractSet() {
136                 FieldList fields = getNodeManager().getFields();
137                 public Iterator iterator() {
138                     return new Iterator() {
139                             FieldIterator i = fields.fieldIterator();
140                             public boolean hasNext() { return i.hasNext();}
141                             public Object JavaDoc next() {
142                                 Field field = i.nextField();
143                                 return field.getName();
144                             }
145                             public void remove() {
146                                 throw new UnsupportedOperationException JavaDoc("You cannot remove fields from a Node.");
147                             }
148                         };
149                 }
150                 public int size() {
151                     return fields.size();
152                 }
153             };
154     }
155
156     // javadoc inherited
157
public void putAll(Map map) {
158         Iterator i = map.entrySet().iterator();
159         while (i.hasNext()) {
160             Entry e = (Entry) i.next();
161             setValue((String JavaDoc) e.getKey(), e.getValue());
162         }
163     }
164
165     // javadoc inherited
166
public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value) {
167         Object JavaDoc r = getValue((String JavaDoc) key);
168         setValue((String JavaDoc) key, value);
169         return r;
170     }
171
172     // javadoc inherited
173
public Object JavaDoc get(Object JavaDoc key) {
174         return getValue((String JavaDoc) key);
175     }
176
177     // javadoc inherited
178
public boolean isEmpty() {
179         return false;
180     }
181
182     // javadoc inherited
183
public int size() {
184         return getNodeManager().getFields().size();
185     }
186 }
187
188
Popular Tags