001: /***************************************************************
002: * This file is part of the [fleXive](R) project.
003: *
004: * Copyright (c) 1999-2007
005: * UCS - unique computing solutions gmbh (http://www.ucs.at)
006: * All rights reserved
007: *
008: * The [fleXive](R) project is free software; you can redistribute
009: * it and/or modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation;
011: * either version 2 of the License, or (at your option) any
012: * later version.
013: *
014: * The GNU General Public License can be found at
015: * http://www.gnu.org/copyleft/gpl.html.
016: * A copy is found in the textfile GPL.txt and important notices to the
017: * license from the author are found in LICENSE.txt distributed with
018: * these libraries.
019: *
020: * This library is distributed in the hope that it will be useful,
021: * but WITHOUT ANY WARRANTY; without even the implied warranty of
022: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
023: * GNU General Public License for more details.
024: *
025: * For further information about UCS - unique computing solutions gmbh,
026: * please see the company website: http://www.ucs.at
027: *
028: * For further information about [fleXive](R), please see the
029: * project website: http://www.flexive.org
030: *
031: *
032: * This copyright notice MUST APPEAR in all copies of the file!
033: ***************************************************************/package com.flexive.war;
034:
035: import java.util.Collection;
036: import java.util.Hashtable;
037: import java.util.Map;
038: import java.util.Set;
039:
040: /**
041: * This Hashtable finds its entries by comparing the hashtables keys with the given keys original value
042: * or its string representation.
043: * <p/>
044: * In other words, .get(new Integer(1)) will deliver the same result as .get('1') on a
045: * LookupHashtable<Integer,?>.
046: *
047: * @author Gregor Schober (gregor.schober@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
048: * @version $Rev: 1 $
049: */
050: public class LookupHashtable<K, V> implements Map<K, V>,
051: java.io.Serializable {
052:
053: private Hashtable<K, V> original;
054: private Hashtable<String, V> cast;
055:
056: public LookupHashtable(int initialCapacity) {
057: original = new Hashtable<K, V>(initialCapacity);
058: cast = new Hashtable<String, V>(initialCapacity);
059: }
060:
061: /**
062: * {@inheritDoc}
063: */
064: public int size() {
065: return original == null ? 0 : original.size();
066: }
067:
068: /**
069: * {@inheritDoc}
070: */
071: public boolean isEmpty() {
072: return original == null || original.isEmpty();
073: }
074:
075: /**
076: * {@inheritDoc}
077: */
078: public boolean containsKey(Object key) {
079: return original.containsKey(key) || cast.containsKey(key);
080: }
081:
082: /**
083: * {@inheritDoc}
084: */
085: public boolean containsValue(Object value) {
086: return original != null && original.containsValue(value);
087: }
088:
089: /**
090: * {@inheritDoc}
091: */
092: public V get(Object key) {
093: V result = original.get(key);
094: if (result == null) {
095: result = cast.get(String.valueOf(key));
096: }
097: return result;
098: }
099:
100: /**
101: * {@inheritDoc}
102: */
103: public V put(K k, V v) {
104: V oldValue = original.put(k, v);
105: if (!(k instanceof String)) {
106: cast.put(String.valueOf(k), v);
107: }
108: return oldValue;
109: }
110:
111: /**
112: * {@inheritDoc}
113: */
114: public V remove(Object key) {
115: V value = original.remove(key);
116: cast.remove(String.valueOf(key));
117: return value;
118: }
119:
120: /**
121: * {@inheritDoc}
122: */
123: public void putAll(Map<? extends K, ? extends V> map) {
124: original.putAll(map);
125: for (K key : map.keySet()) {
126: if (key instanceof String)
127: continue;
128: cast.put(String.valueOf(key), map.get(key));
129: }
130: }
131:
132: /**
133: * {@inheritDoc}
134: */
135: public void clear() {
136: original.clear();
137: cast.clear();
138: }
139:
140: /**
141: * {@inheritDoc}
142: */
143: public Set<K> keySet() {
144: return original.keySet();
145: }
146:
147: /**
148: * {@inheritDoc}
149: */
150: public Collection<V> values() {
151: return original.values();
152: }
153:
154: /**
155: * {@inheritDoc}
156: */
157: public Set<Entry<K, V>> entrySet() {
158: return original.entrySet();
159: }
160: }
|