KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > standard > lang > jstl > EnumeratedMap


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

16
17 package org.apache.taglibs.standard.lang.jstl;
18
19 import java.util.Collection JavaDoc;
20 import java.util.Enumeration JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Map JavaDoc;
23 import java.util.Set JavaDoc;
24
25 /**
26  *
27  * <p>This is a Map implementation driven by a data source that only
28  * provides an enumeration of keys and a getValue(key) method. This
29  * class must be subclassed to implement those methods.
30  *
31  * <p>Some of the methods may incur a performance penalty that
32  * involves enumerating the entire data source. In these cases, the
33  * Map will try to save the results of that enumeration, but only if
34  * the underlying data source is immutable.
35  *
36  * @author Nathan Abramson - Art Technology Group
37  * @version $Change: 181177 $$DateTime: 2001/06/26 08:45:09 $$Author: pierred $
38  **/

39
40 public abstract class EnumeratedMap
41   implements Map JavaDoc
42 {
43   //-------------------------------------
44
// Member variables
45
//-------------------------------------
46

47   Map JavaDoc mMap;
48
49   //-------------------------------------
50
public void clear ()
51   {
52     throw new UnsupportedOperationException JavaDoc ();
53   }
54
55   //-------------------------------------
56
public boolean containsKey (Object JavaDoc pKey)
57   {
58     return getValue (pKey) != null;
59   }
60
61   //-------------------------------------
62
public boolean containsValue (Object JavaDoc pValue)
63   {
64     return getAsMap ().containsValue (pValue);
65   }
66
67   //-------------------------------------
68
public Set JavaDoc entrySet ()
69   {
70     return getAsMap ().entrySet ();
71   }
72
73   //-------------------------------------
74
public Object JavaDoc get (Object JavaDoc pKey)
75   {
76     return getValue (pKey);
77   }
78
79   //-------------------------------------
80
public boolean isEmpty ()
81   {
82     return !enumerateKeys ().hasMoreElements ();
83   }
84
85   //-------------------------------------
86
public Set JavaDoc keySet ()
87   {
88     return getAsMap ().keySet ();
89   }
90
91   //-------------------------------------
92
public Object JavaDoc put (Object JavaDoc pKey, Object JavaDoc pValue)
93   {
94     throw new UnsupportedOperationException JavaDoc ();
95   }
96
97   //-------------------------------------
98
public void putAll (Map JavaDoc pMap)
99   {
100     throw new UnsupportedOperationException JavaDoc ();
101   }
102
103   //-------------------------------------
104
public Object JavaDoc remove (Object JavaDoc pKey)
105   {
106     throw new UnsupportedOperationException JavaDoc ();
107   }
108
109   //-------------------------------------
110
public int size ()
111   {
112     return getAsMap ().size ();
113   }
114
115   //-------------------------------------
116
public Collection JavaDoc values ()
117   {
118     return getAsMap ().values ();
119   }
120
121   //-------------------------------------
122
// Abstract methods
123
//-------------------------------------
124
/**
125    *
126    * Returns an enumeration of the keys
127    **/

128   public abstract Enumeration JavaDoc enumerateKeys ();
129
130   //-------------------------------------
131
/**
132    *
133    * Returns true if it is possible for this data source to change
134    **/

135   public abstract boolean isMutable ();
136
137   //-------------------------------------
138
/**
139    *
140    * Returns the value associated with the given key, or null if not
141    * found.
142    **/

143   public abstract Object JavaDoc getValue (Object JavaDoc pKey);
144
145   //-------------------------------------
146
/**
147    *
148    * Converts the MapSource to a Map. If the map is not mutable, this
149    * is cached
150    **/

151   public Map JavaDoc getAsMap ()
152   {
153     if (mMap != null) {
154       return mMap;
155     }
156     else {
157       Map JavaDoc m = convertToMap ();
158       if (!isMutable ()) {
159     mMap = m;
160       }
161       return m;
162     }
163   }
164
165   //-------------------------------------
166
/**
167    *
168    * Converts to a Map
169    **/

170   Map JavaDoc convertToMap ()
171   {
172     Map JavaDoc ret = new HashMap JavaDoc ();
173     for (Enumeration JavaDoc e = enumerateKeys (); e.hasMoreElements (); ) {
174       Object JavaDoc key = e.nextElement ();
175       Object JavaDoc value = getValue (key);
176       ret.put (key, value);
177     }
178     return ret;
179   }
180
181   //-------------------------------------
182
}
183
Popular Tags