001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: * $Header:$
018: */
019: package org.apache.beehive.netui.script.el.util;
020:
021: import java.util.Collection;
022: import java.util.Enumeration;
023: import java.util.HashMap;
024: import java.util.Map;
025: import java.util.Set;
026: import java.util.Iterator;
027: import java.util.Collections;
028:
029: /**
030: * Facade used to put a {@link Map} API atop another object. For example, a sublcass might allow
031: * the {@link javax.servlet.http.HttpSession} to be treated like a Map for purposes of referencing session
032: * attributes from expressions.
033: */
034: public abstract class MapFacade implements Map {
035:
036: private Map _map = null;
037:
038: /**
039: * Subclasses should implement this method in order to get a value from the
040: * backing object.
041: * @param key the key of the object
042: * @return the value associated with the <code>key</code>.
043: */
044: protected abstract Object getValue(Object key);
045:
046: /**
047: * Subclasses should implement this method in order to set a value in
048: * the backing object.
049: * @param key the key of the object
050: * @param value the value to associate with the key
051: * @return the previous value associated with this key or <code>null</code> if no value was associated
052: */
053: protected abstract Object putValue(Object key, Object value);
054:
055: /**
056: * Get an {@link Enumeration} of the keys associated with a backing object. Note,
057: * an {@link Enumeration} is used here because many web-based attribute maps
058: * expose their list of keys only as enumeration objects.
059: * @return the {@link Enumeration} of keys available from the backing object
060: */
061: protected abstract Iterator getKeys();
062:
063: /*--------------------------------------------------------------------------
064:
065: Implementation of the {@link Map} interface.
066:
067: -------------------------------------------------------------------------- */
068: public void clear() {
069: }
070:
071: public boolean containsKey(Object key) {
072: return getValue(key) != null;
073: }
074:
075: public Object get(Object key) {
076: return getValue(key);
077: }
078:
079: public Object put(Object key, Object value) {
080: return putValue(key, value);
081: }
082:
083: public boolean containsValue(Object key) {
084: assert getMap() != null;
085: return getMap().containsValue(key);
086: }
087:
088: public Set entrySet() {
089: assert getMap() != null;
090: return Collections.unmodifiableSet(getMap().entrySet());
091: }
092:
093: public boolean isEmpty() {
094: assert getMap() != null;
095: return getMap().isEmpty();
096: }
097:
098: public Set keySet() {
099: assert getMap() != null;
100: return Collections.unmodifiableSet(getMap().keySet());
101: }
102:
103: public void putAll(Map t) {
104: assert getMap() != null;
105: getMap().putAll(t);
106: }
107:
108: public Object remove(Object key) {
109: assert getMap() != null;
110: return getMap().remove(key);
111: }
112:
113: public int size() {
114: assert getMap() != null;
115: return getMap().size();
116: }
117:
118: public Collection values() {
119: assert getMap() != null;
120: return Collections.unmodifiableCollection(getMap().values());
121: }
122:
123: private Map getMap() {
124: if (_map == null) {
125: _map = new HashMap();
126:
127: Iterator keys = getKeys();
128: assert keys != null;
129: while (keys.hasNext()) {
130: Object key = keys.next();
131: _map.put(key, getValue(key));
132: }
133: }
134: return _map;
135: }
136: }
|