001: /*
002: * The Apache Software License, Version 1.1
003: *
004: * Copyright (c) 1999 The Apache Software Foundation. All rights
005: * reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions
009: * are met:
010: *
011: * 1. Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The end-user documentation included with the redistribution, if
020: * any, must include the following acknowlegement:
021: * "This product includes software developed by the
022: * Apache Software Foundation (http://www.apache.org/)."
023: * Alternately, this acknowlegement may appear in the software itself,
024: * if and wherever such third-party acknowlegements normally appear.
025: *
026: * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
027: * Foundation" must not be used to endorse or promote products derived
028: * from this software without prior written permission. For written
029: * permission, please contact apache@apache.org.
030: *
031: * 5. Products derived from this software may not be called "Apache"
032: * nor may "Apache" appear in their names without prior written
033: * permission of the Apache Group.
034: *
035: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
036: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
037: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
038: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
039: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
040: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
041: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
042: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
043: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
044: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
045: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
046: * SUCH DAMAGE.
047: * ====================================================================
048: *
049: * This software consists of voluntary contributions made by many
050: * individuals on behalf of the Apache Software Foundation. For more
051: * information on the Apache Software Foundation, please see
052: * <http://www.apache.org/>.
053: *
054: */
055:
056: package org.jbpm.jpdl.el.impl;
057:
058: import java.util.Collection;
059: import java.util.Enumeration;
060: import java.util.HashMap;
061: import java.util.Map;
062: import java.util.Set;
063:
064: /**
065: *
066: * <p>This is a Map implementation driven by a data source that only
067: * provides an enumeration of keys and a getValue(key) method. This
068: * class must be subclassed to implement those methods.
069: *
070: * <p>Some of the methods may incur a performance penalty that
071: * involves enumerating the entire data source. In these cases, the
072: * Map will try to save the results of that enumeration, but only if
073: * the underlying data source is immutable.
074: *
075: * @author Nathan Abramson - Art Technology Group
076: * @version $Change: 181177 $$DateTime: 2001/06/26 08:45:09 $$Author$
077: **/
078:
079: public abstract class EnumeratedMap implements Map {
080: //-------------------------------------
081: // Member variables
082: //-------------------------------------
083:
084: Map mMap;
085:
086: //-------------------------------------
087: public void clear() {
088: throw new UnsupportedOperationException();
089: }
090:
091: //-------------------------------------
092: public boolean containsKey(Object pKey) {
093: return getValue(pKey) != null;
094: }
095:
096: //-------------------------------------
097: public boolean containsValue(Object pValue) {
098: return getAsMap().containsValue(pValue);
099: }
100:
101: //-------------------------------------
102: public Set entrySet() {
103: return getAsMap().entrySet();
104: }
105:
106: //-------------------------------------
107: public Object get(Object pKey) {
108: return getValue(pKey);
109: }
110:
111: //-------------------------------------
112: public boolean isEmpty() {
113: return !enumerateKeys().hasMoreElements();
114: }
115:
116: //-------------------------------------
117: public Set keySet() {
118: return getAsMap().keySet();
119: }
120:
121: //-------------------------------------
122: public Object put(Object pKey, Object pValue) {
123: throw new UnsupportedOperationException();
124: }
125:
126: //-------------------------------------
127: public void putAll(Map pMap) {
128: throw new UnsupportedOperationException();
129: }
130:
131: //-------------------------------------
132: public Object remove(Object pKey) {
133: throw new UnsupportedOperationException();
134: }
135:
136: //-------------------------------------
137: public int size() {
138: return getAsMap().size();
139: }
140:
141: //-------------------------------------
142: public Collection values() {
143: return getAsMap().values();
144: }
145:
146: //-------------------------------------
147: // Abstract methods
148: //-------------------------------------
149: /**
150: *
151: * Returns an enumeration of the keys
152: **/
153: public abstract Enumeration enumerateKeys();
154:
155: //-------------------------------------
156: /**
157: *
158: * Returns true if it is possible for this data source to change
159: **/
160: public abstract boolean isMutable();
161:
162: //-------------------------------------
163: /**
164: *
165: * Returns the value associated with the given key, or null if not
166: * found.
167: **/
168: public abstract Object getValue(Object pKey);
169:
170: //-------------------------------------
171: /**
172: *
173: * Converts the MapSource to a Map. If the map is not mutable, this
174: * is cached
175: **/
176: public Map getAsMap() {
177: if (mMap != null) {
178: return mMap;
179: } else {
180: Map m = convertToMap();
181: if (!isMutable()) {
182: mMap = m;
183: }
184: return m;
185: }
186: }
187:
188: //-------------------------------------
189: /**
190: *
191: * Converts to a Map
192: **/
193: Map convertToMap() {
194: Map ret = new HashMap();
195: for (Enumeration e = enumerateKeys(); e.hasMoreElements();) {
196: Object key = e.nextElement();
197: Object value = getValue(key);
198: ret.put(key, value);
199: }
200: return ret;
201: }
202:
203: //-------------------------------------
204: }
|