01: package org.jreform.internal;
02:
03: import java.util.Collection;
04: import java.util.Map;
05: import java.util.Set;
06:
07: /**
08: * A map that returns a default value instead of null when a key is not found.
09: *
10: * @author armandino (at) gmail.com
11: */
12: class DefaultValueMap<K, V> implements Map<K, V> {
13: private Map<K, V> map;
14: private V defaultValue;
15:
16: DefaultValueMap(Map<K, V> map, V defaultValue) {
17: this .map = map;
18: this .defaultValue = defaultValue;
19: }
20:
21: public void clear() {
22: map.clear();
23: }
24:
25: public boolean containsKey(Object key) {
26: return map.containsKey(key);
27: }
28:
29: public boolean containsValue(Object value) {
30: return map.containsValue(value);
31: }
32:
33: public Set<Entry<K, V>> entrySet() {
34: return map.entrySet();
35: }
36:
37: public boolean equals(Object o) {
38: return map.equals(o);
39: }
40:
41: public V get(Object key) {
42: // NOTE: key.toString() is a workaround for jsp EL which allows
43: // passing arbitrary objects (e.g. Integer) as keys.
44:
45: V value = map.get(key.toString());
46: return value == null ? defaultValue : value;
47: }
48:
49: public int hashCode() {
50: return map.hashCode();
51: }
52:
53: public boolean isEmpty() {
54: return map.isEmpty();
55: }
56:
57: public Set<K> keySet() {
58: return map.keySet();
59: }
60:
61: public V put(K key, V value) {
62: return map.put(key, value);
63: }
64:
65: public void putAll(Map<? extends K, ? extends V> t) {
66: map.putAll(t);
67: }
68:
69: public V remove(Object key) {
70: return map.remove(key);
71: }
72:
73: public int size() {
74: return map.size();
75: }
76:
77: public Collection<V> values() {
78: return map.values();
79: }
80:
81: @Override
82: public String toString() {
83: return map.toString();
84: }
85:
86: }
|