KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > archie > impl > MultiValueNode


1 package org.sapia.archie.impl;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.Iterator JavaDoc;
5 import java.util.List JavaDoc;
6 import java.util.Map JavaDoc;
7
8 import org.sapia.archie.AbstractNode;
9 import org.sapia.archie.Entry;
10 import org.sapia.archie.NameParser;
11 import org.sapia.archie.NamePart;
12 import org.sapia.archie.NodeFactory;
13 import org.sapia.archie.ProcessingException;
14
15
16 /**
17  * An instance of this class tolerates multiple values under the same name.
18  *
19  * @author Yanick Duchesne
20  *
21  * <dl>
22  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2003 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
23  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
24  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
25  * </dl>
26  */

27 public class MultiValueNode extends AbstractNode {
28   protected Map JavaDoc _valueLists;
29
30   protected MultiValueNode(Map JavaDoc children, Map JavaDoc values, NodeFactory fac)
31                     throws ProcessingException {
32     super(children, fac);
33     _valueLists = values;
34   }
35
36   protected MultiValueNode(NameParser parser, Map JavaDoc children, Map JavaDoc values, NodeFactory fac)
37   throws ProcessingException {
38     super(parser, children, fac);
39     _valueLists = values;
40   }
41
42   /**
43    * This method calls onSelect() and onRead() successively, before returning
44    * the a value.
45    *
46    * @see #onSelect(List)
47    * @see #onRead(NamePart, Object)
48    *
49    * @see org.sapia.archie.Node#getValue(NamePart)
50    */

51   public Object JavaDoc getValue(NamePart name) {
52     List JavaDoc values = (List JavaDoc) _valueLists.get(name);
53
54     if (values == null) {
55       return null;
56     }
57     
58     Object JavaDoc selected = onSelect(values);
59     if(selected != null){
60       selected = onRead(name, selected);
61     }
62     return selected;
63   }
64   
65   /**
66    * @see org.sapia.archie.Node#removeValue(org.sapia.archie.NamePart)
67    */

68   public Object JavaDoc removeValue(NamePart name) {
69     Object JavaDoc val = getValue(name);
70     _valueLists.remove(name);
71     return val;
72   }
73   
74   /**
75    * @see org.sapia.archie.Node#getEntries()
76    */

77   public Iterator JavaDoc getEntries() {
78     Map.Entry JavaDoc entry;
79     Iterator JavaDoc itr = _valueLists.entrySet().iterator();
80     List JavaDoc values;
81     List JavaDoc entries = new ArrayList JavaDoc();
82     while(itr.hasNext()){
83       entry = (Map.Entry JavaDoc)itr.next();
84       values = (List JavaDoc)entry.getValue();
85       for(int i = 0; i < values.size(); i++){
86         entries.add(new Entry(entry.getKey().toString(), values.get(i)));
87       }
88     }
89     return entries.iterator();
90   }
91   
92   /**
93    * @see org.sapia.archie.Node#putValue(NamePart, Object, boolean)
94    */

95   public boolean putValue(NamePart name, Object JavaDoc value, boolean overwrite) {
96     List JavaDoc values = (List JavaDoc) _valueLists.get(name);
97
98     if (values == null) {
99       values = new ArrayList JavaDoc();
100       _valueLists.put(name, values);
101     }
102
103     if (!overwrite && (values.size() != 0)) {
104       return false;
105     }
106
107     Object JavaDoc toBind = onWrite(name, value);
108     if(toBind != null){
109       values.add(toBind);
110     }
111     return true;
112   }
113
114   /**
115    * @see org.sapia.archie.Node#getValueNames()
116    */

117   public Iterator JavaDoc getValueNames() {
118     return _valueLists.keySet().iterator();
119   }
120
121   /**
122    * @see org.sapia.archie.Node#getValueCount()
123    */

124   public int getValueCount() {
125     return _valueLists.size();
126   }
127   
128   /**
129    * @see org.sapia.archie.Node#getChildrenCount()
130    */

131   public int getChildrenCount() {
132     return _valueLists.size();
133   }
134
135   /**
136    * @see org.sapia.archie.Node#getChildrenNames()
137    */

138   public Iterator JavaDoc getChildrenNames() {
139     return _valueLists.keySet().iterator();
140   }
141
142   /**
143    * @see org.sapia.archie.Node#getChildren()
144    */

145   public Iterator JavaDoc getChildren() {
146     return super.getChildren();
147   }
148
149   /**
150    * Selects a given value, from the given list, and returns it.
151    * This template method is internally called by <code>getValue(...)</code>.
152    *
153    * @param values the list of values to select from.
154    * @return an item from the given list of values.
155    *
156    * @see #getValue(NamePart)
157    */

158   protected Object JavaDoc onSelect(List JavaDoc values) {
159     if (values.size() > 0) {
160       Object JavaDoc toReturn = values.remove(0);
161       values.add(toReturn);
162
163       return toReturn;
164     }
165
166     return null;
167   }
168   
169   /**
170    * This method is internally called by <code>getValue(...)</code>.
171    *
172    * @param np the name under which the selected object is bound.
173    * @param selected the selected object.
174    * @return the object to return to the application (the selected object,
175    * or a substitute).
176    *
177    * @see #getValue(NamePart)
178    */

179   protected Object JavaDoc onRead(NamePart np, Object JavaDoc selected){
180     return selected;
181   }
182   
183   /**
184    * Called prior to bind the given object to the given
185    * name part.
186    *
187    * @param np the <code>NamePart</code> under which the given object
188    * is to be bound.
189    * @param toBind the object to bind.
190    * @return the object to bind (can be substituted).
191    */

192   protected Object JavaDoc onWrite(NamePart np, Object JavaDoc toBind){
193     return toBind;
194   }
195
196   static class ValueIterator implements Iterator JavaDoc {
197     Iterator JavaDoc _lists;
198     Iterator JavaDoc _current;
199
200     ValueIterator(Iterator JavaDoc lists) {
201       _lists = lists;
202     }
203
204     public boolean hasNext() {
205       if (_current != null) {
206         if (_current.hasNext()) {
207           return true;
208         } else if (_lists.hasNext()) {
209           _current = ((List JavaDoc) _lists.next()).iterator();
210
211           return _current.hasNext();
212         } else {
213           return false;
214         }
215       } else if (_lists.hasNext()) {
216         _current = ((List JavaDoc) _lists.next()).iterator();
217
218         return hasNext();
219       }
220
221       return false;
222     }
223
224     public Object JavaDoc next() {
225       if (_current == null) {
226         if (hasNext()) {
227           return _current.next();
228         }
229       }
230
231       return _current.next();
232     }
233
234     public void remove() {
235     }
236   }
237 }
238
Popular Tags