View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.el;
18  
19  import java.util.Collection;
20  import java.util.Enumeration;
21  import java.util.HashMap;
22  import java.util.Map;
23  import java.util.Set;
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: bayard $
38   **/
39  
40  public abstract class EnumeratedMap
41    implements Map
42  {
43    //-------------------------------------
44    // Member variables
45    //-------------------------------------
46  
47    Map mMap;
48  
49    //-------------------------------------
50    public void clear ()
51    {
52      throw new UnsupportedOperationException ();
53    }
54  
55    //-------------------------------------
56    public boolean containsKey (Object pKey)
57    {
58      return getValue (pKey) != null;
59    }
60  
61    //-------------------------------------
62    public boolean containsValue (Object pValue)
63    {
64      return getAsMap ().containsValue (pValue);
65    }
66  
67    //-------------------------------------
68    public Set entrySet ()
69    {
70      return getAsMap ().entrySet ();
71    }
72  
73    //-------------------------------------
74    public Object get (Object pKey)
75    {
76      return getValue (pKey);
77    }
78  
79    //-------------------------------------
80    public boolean isEmpty ()
81    {
82      return !enumerateKeys ().hasMoreElements ();
83    }
84  
85    //-------------------------------------
86    public Set keySet ()
87    {
88      return getAsMap ().keySet ();
89    }
90  
91    //-------------------------------------
92    public Object put (Object pKey, Object pValue)
93    {
94      throw new UnsupportedOperationException ();
95    }
96  
97    //-------------------------------------
98    public void putAll (Map pMap)
99    {
100     throw new UnsupportedOperationException ();
101   }
102 
103   //-------------------------------------
104   public Object remove (Object pKey)
105   {
106     throw new UnsupportedOperationException ();
107   }
108 
109   //-------------------------------------
110   public int size ()
111   {
112     return getAsMap ().size ();
113   }
114 
115   //-------------------------------------
116   public Collection 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 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 getValue (Object 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 getAsMap ()
152   {
153     if (mMap != null) {
154       return mMap;
155     }
156     else {
157       Map 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 convertToMap ()
171   {
172     Map ret = new HashMap ();
173     for (Enumeration e = enumerateKeys (); e.hasMoreElements (); ) {
174       Object key = e.nextElement ();
175       Object value = getValue (key);
176       ret.put (key, value);
177     }
178     return ret;
179   }
180 
181   //-------------------------------------
182 }