KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mortbay > util > MultiMap


1 // ========================================================================
2
// $Id: MultiMap.java,v 1.18 2004/12/15 02:13:51 gregwilkins Exp $
3
// Copyright 1999-2004 Mort Bay Consulting Pty. Ltd.
4
// ------------------------------------------------------------------------
5
// Licensed under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
8
// http://www.apache.org/licenses/LICENSE-2.0
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
// ========================================================================
15

16 package org.mortbay.util;
17
18 import java.util.Arrays JavaDoc;
19 import java.util.HashMap JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Map JavaDoc;
23
24 /* ------------------------------------------------------------ */
25 /** A multi valued Map.
26  * This Map specializes HashMap and provides methods
27  * that operate on multi valued items.
28  * <P>
29  * Implemented as a map of LazyList values
30  *
31  * @see LazyList
32  * @version $Id: MultiMap.java,v 1.18 2004/12/15 02:13:51 gregwilkins Exp $
33  * @author Greg Wilkins (gregw)
34  */

35 public class MultiMap extends HashMap JavaDoc implements Cloneable JavaDoc
36 {
37     /* ------------------------------------------------------------ */
38     /** Constructor.
39      */

40     public MultiMap()
41     {}
42     
43     /* ------------------------------------------------------------ */
44     /** Constructor.
45      * @param size Capacity of the map
46      */

47     public MultiMap(int size)
48     {
49         super(size);
50     }
51     
52     /* ------------------------------------------------------------ */
53     /** Constructor.
54      * @param map
55      */

56     public MultiMap(Map JavaDoc map)
57     {
58         super((map.size()*3)/2);
59         putAll(map);
60     }
61     
62     /* ------------------------------------------------------------ */
63     /** Get multiple values.
64      * Single valued entries are converted to singleton lists.
65      * @param name The entry key.
66      * @return Unmodifieable List of values.
67      */

68     public List JavaDoc getValues(Object JavaDoc name)
69     {
70         return LazyList.getList(super.get(name),true);
71     }
72     
73     /* ------------------------------------------------------------ */
74     /** Get a value from a multiple value.
75      * If the value is not a multivalue, then index 0 retrieves the
76      * value or null.
77      * @param name The entry key.
78      * @param i Index of element to get.
79      * @return Unmodifieable List of values.
80      */

81     public Object JavaDoc getValue(Object JavaDoc name,int i)
82     {
83         Object JavaDoc l=super.get(name);
84         if (i==0 && LazyList.size(l)==0)
85             return null;
86         return LazyList.get(l,i);
87     }
88     
89     
90     /* ------------------------------------------------------------ */
91     /** Get value as String.
92      * Single valued items are converted to a String with the toString()
93      * Object method. Multi valued entries are converted to a comma separated
94      * List. No quoting of commas within values is performed.
95      * @param name The entry key.
96      * @return String value.
97      */

98     public String JavaDoc getString(Object JavaDoc name)
99     {
100         Object JavaDoc l=super.get(name);
101         switch(LazyList.size(l))
102         {
103           case 0:
104               return null;
105           case 1:
106               Object JavaDoc o=LazyList.get(l,0);
107               return o==null?null:o.toString();
108           default:
109               StringBuffer JavaDoc values=new StringBuffer JavaDoc(128);
110               synchronized(values)
111               {
112                   for (int i=0; i<LazyList.size(l); i++)
113                   {
114                       Object JavaDoc e=LazyList.get(l,i);
115                       if (e!=null)
116                       {
117                           if (values.length()>0)
118                               values.append(',');
119                           values.append(e.toString());
120                       }
121                   }
122                   return values.toString();
123               }
124         }
125     }
126     
127     /* ------------------------------------------------------------ */
128     public Object JavaDoc get(Object JavaDoc name)
129     {
130         Object JavaDoc l=super.get(name);
131         switch(LazyList.size(l))
132         {
133           case 0:
134               return null;
135           case 1:
136               Object JavaDoc o=LazyList.get(l,0);
137               return o;
138           default:
139               return LazyList.getList(l,true);
140         }
141     }
142     
143     /* ------------------------------------------------------------ */
144     /** Put and entry into the map.
145      * @param name The entry key.
146      * @param value The entry value.
147      * @return The previous value or null.
148      */

149     public Object JavaDoc put(Object JavaDoc name, Object JavaDoc value)
150     {
151         return super.put(name,LazyList.add(null,value));
152     }
153
154     /* ------------------------------------------------------------ */
155     /** Put multi valued entry.
156      * @param name The entry key.
157      * @param values The List of multiple values.
158      * @return The previous value or null.
159      */

160     public Object JavaDoc putValues(Object JavaDoc name, List JavaDoc values)
161     {
162         return super.put(name,values);
163     }
164     
165     /* ------------------------------------------------------------ */
166     /** Put multi valued entry.
167      * @param name The entry key.
168      * @param values The String array of multiple values.
169      * @return The previous value or null.
170      */

171     public Object JavaDoc putValues(Object JavaDoc name, String JavaDoc[] values)
172     {
173         Object JavaDoc list=null;
174         for (int i=0;i<values.length;i++)
175             list=LazyList.add(list,values[i]);
176         return put(name,list);
177     }
178     
179     
180     /* ------------------------------------------------------------ */
181     /** Add value to multi valued entry.
182      * If the entry is single valued, it is converted to the first
183      * value of a multi valued entry.
184      * @param name The entry key.
185      * @param value The entry value.
186      */

187     public void add(Object JavaDoc name, Object JavaDoc value)
188     {
189         Object JavaDoc lo = super.get(name);
190         Object JavaDoc ln = LazyList.add(lo,value);
191         if (lo!=ln)
192             super.put(name,ln);
193     }
194
195     /* ------------------------------------------------------------ */
196     /** Add values to multi valued entry.
197      * If the entry is single valued, it is converted to the first
198      * value of a multi valued entry.
199      * @param name The entry key.
200      * @param values The List of multiple values.
201      */

202     public void addValues(Object JavaDoc name, List JavaDoc values)
203     {
204         Object JavaDoc lo = super.get(name);
205         Object JavaDoc ln = LazyList.addCollection(lo,values);
206         if (lo!=ln)
207             super.put(name,ln);
208     }
209     
210     /* ------------------------------------------------------------ */
211     /** Add values to multi valued entry.
212      * If the entry is single valued, it is converted to the first
213      * value of a multi valued entry.
214      * @param name The entry key.
215      * @param values The String array of multiple values.
216      */

217     public void addValues(Object JavaDoc name, String JavaDoc[] values)
218     {
219         Object JavaDoc lo = super.get(name);
220         Object JavaDoc ln = LazyList.addCollection(lo,Arrays.asList(values));
221         if (lo!=ln)
222             super.put(name,ln);
223     }
224     
225     /* ------------------------------------------------------------ */
226     /** Remove value.
227      * @param name The entry key.
228      * @param value The entry value.
229      * @return true if it was removed.
230      */

231     public boolean removeValue(Object JavaDoc name,Object JavaDoc value)
232     {
233         Object JavaDoc lo = super.get(name);
234         Object JavaDoc ln=lo;
235         int s=LazyList.size(lo);
236         if (s>0)
237         {
238             ln=LazyList.remove(lo,value);
239             if (ln==null)
240                 super.remove(name);
241             else
242                 super.put(name, ln);
243         }
244         return LazyList.size(ln)!=s;
245     }
246     
247     /* ------------------------------------------------------------ */
248     /** Put all contents of map.
249      * @param m Map
250      */

251     public void putAll(Map JavaDoc m)
252     {
253         Iterator JavaDoc i = m.entrySet().iterator();
254         boolean multi=m instanceof MultiMap;
255         while(i.hasNext())
256         {
257             Map.Entry JavaDoc entry =
258                 (Map.Entry JavaDoc)i.next();
259             if (multi)
260                 super.put(entry.getKey(),LazyList.clone(entry.getValue()));
261             else
262                 put(entry.getKey(),entry.getValue());
263         }
264     }
265
266     /* ------------------------------------------------------------ */
267     /**
268      * @return Map of String arrays
269      */

270     public Map JavaDoc toStringArrayMap()
271     {
272         HashMap JavaDoc map = new HashMap JavaDoc(size()*3/2);
273         
274         Iterator JavaDoc i = super.entrySet().iterator();
275         while(i.hasNext())
276         {
277             Map.Entry JavaDoc entry = (Map.Entry JavaDoc)i.next();
278             Object JavaDoc l = entry.getValue();
279             map.put(entry.getKey(),LazyList.toStringArray(l));
280         }
281         return map;
282     }
283     
284     /* ------------------------------------------------------------ */
285     public Object JavaDoc clone()
286     {
287         MultiMap mm = (MultiMap) super.clone();
288         
289         Iterator JavaDoc iter = mm.entrySet().iterator();
290         while (iter.hasNext())
291         {
292             Map.Entry JavaDoc entry = (Map.Entry JavaDoc)iter.next();
293             entry.setValue(LazyList.clone(entry.getValue()));
294         }
295         
296         return mm;
297     }
298 }
299
Popular Tags