001: /*
002: * Copyright 2005-2007 Noelios Consulting.
003: *
004: * The contents of this file are subject to the terms of the Common Development
005: * and Distribution License (the "License"). You may not use this file except in
006: * compliance with the License.
007: *
008: * You can obtain a copy of the license at
009: * http://www.opensource.org/licenses/cddl1.txt See the License for the specific
010: * language governing permissions and limitations under the License.
011: *
012: * When distributing Covered Code, include this CDDL HEADER in each file and
013: * include the License file at http://www.opensource.org/licenses/cddl1.txt If
014: * applicable, add the following below this CDDL HEADER, with the fields
015: * enclosed by brackets "[]" replaced with your own identifying information:
016: * Portions Copyright [yyyy] [name of copyright owner]
017: */
018:
019: package org.restlet.util;
020:
021: import java.util.Collection;
022: import java.util.Map;
023: import java.util.Set;
024: import java.util.TreeMap;
025:
026: /**
027: * Map wrapper. Modifiable map that delegates all methods to a wrapped map. This
028: * allows an easy subclassing.
029: *
030: * @author Jerome Louvel (contact@noelios.com)
031: * @see java.util.Collections
032: * @see java.util.List
033: */
034: public class WrapperMap<K, V> implements Map<K, V> {
035: /** The delegate map. */
036: private Map<K, V> delegate;
037:
038: /**
039: * Constructor.
040: */
041: public WrapperMap() {
042: this .delegate = null;
043: }
044:
045: /**
046: * Constructor.
047: *
048: * @param delegate
049: * The delegate list.
050: */
051: public WrapperMap(Map<K, V> delegate) {
052: this .delegate = delegate;
053: }
054:
055: /**
056: * Removes all mappings from this Map.
057: */
058: public void clear() {
059: getDelegate().clear();
060: }
061:
062: /**
063: * Returns true if this map contains a mapping for the specified key.
064: *
065: * @param key
066: * The key to look up.
067: * @return True if this map contains a mapping for the specified key.
068: */
069: public boolean containsKey(Object key) {
070: return getDelegate().containsKey(key);
071: }
072:
073: /**
074: * Returns true if this map maps one or more keys to the specified value.
075: *
076: * @param value
077: * The value to look up
078: * @return True if this map maps one or more keys to the specified value.
079: */
080: public boolean containsValue(Object value) {
081: return getDelegate().containsValue(value);
082: }
083:
084: /**
085: * Returns a set view of the mappings contained in this map.
086: *
087: * @return A set view of the mappings contained in this map.
088: */
089: public Set<Entry<K, V>> entrySet() {
090: return getDelegate().entrySet();
091: }
092:
093: /**
094: * Compares the specified object with this map for equality.
095: *
096: * @param o
097: * Object to be compared for equality with this map.
098: * @return True if the specified object is equal to this map.
099: */
100: public boolean equals(Object o) {
101: return getDelegate().equals(o);
102: }
103:
104: /**
105: * Returns the value to which this map maps the specified key.
106: *
107: * @param key
108: * Key whose associated value is to be returned.
109: * @return The value to which this map maps the specified key, or null if
110: * the map contains no mapping for this key.
111: */
112: public V get(Object key) {
113: return getDelegate().get(key);
114: }
115:
116: /**
117: * Returns the delegate list.
118: *
119: * @return The delegate list.
120: */
121: protected Map<K, V> getDelegate() {
122: if (this .delegate == null) {
123: this .delegate = new TreeMap<K, V>();
124: }
125:
126: return this .delegate;
127: }
128:
129: /**
130: * Returns the hash code value for this map.
131: *
132: * @return The hash code value for this map.
133: */
134: public int hashCode() {
135: return getDelegate().hashCode();
136: }
137:
138: /**
139: * Returns true if this map contains no key-value mappings.
140: *
141: * @return True if this map contains no key-value mappings.
142: */
143: public boolean isEmpty() {
144: return getDelegate().isEmpty();
145: }
146:
147: /**
148: * Returns a set view of the keys contained in this map.
149: *
150: * @return A set view of the keys contained in this map.
151: */
152: public Set<K> keySet() {
153: return getDelegate().keySet();
154: }
155:
156: /**
157: * Associates the specified value with the specified key in this map
158: * (optional operation).
159: *
160: * @param key
161: * Key with which the specified value is to be associated.
162: * @param value
163: * Value to be associated with the specified key.
164: * @return The previous value associated with specified key, or null if
165: * there was no mapping for key. A null return can also indicate
166: * that the map previously associated null with the specified key,
167: * if the implementation supports null values.
168: */
169: public V put(K key, V value) {
170: return getDelegate().put(key, value);
171: }
172:
173: /**
174: * Copies all of the mappings from the specified map to this map (optional
175: * operation).
176: *
177: * @param t
178: * Mappings to be stored in this map.
179: */
180: public void putAll(Map<? extends K, ? extends V> t) {
181: getDelegate().putAll(t);
182: }
183:
184: /**
185: * Removes the mapping for this key from this map if it is present (optional
186: * operation).
187: *
188: * @param key
189: * Key whose mapping is to be removed from the map.
190: * @return The previous value associated with specified key, or null if
191: * there was no mapping for key.
192: */
193: public V remove(Object key) {
194: return getDelegate().remove(key);
195: }
196:
197: /**
198: * Returns the number of key-value mappings in this map.
199: *
200: * @return The number of key-value mappings in this map.
201: */
202: public int size() {
203: return getDelegate().size();
204: }
205:
206: /**
207: * Returns a collection view of the values contained in this map.
208: *
209: * @return A collection view of the values contained in this map.
210: */
211: public Collection<V> values() {
212: return getDelegate().values();
213: }
214:
215: }
|