01: package org.apache.ojb.broker.util;
02:
03: /* Copyright 2002-2005 The Apache Software Foundation
04: *
05: * Licensed under the Apache License, Version 2.0 (the "License");
06: * you may not use this file except in compliance with the License.
07: * You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17:
18: import java.util.Hashtable;
19:
20: /**
21: * this class can be used to build two-way lookup tables.
22: * It provides lookup from keys to values and the inverse
23: * lookup from values to keys.
24: *
25: * @author Thomas Mahler
26: * @version $Id: DoubleHashtable.java,v 1.4.2.1 2005/12/21 22:27:47 tomdz Exp $
27: */
28: public class DoubleHashtable {
29: /**
30: * the table for key lookups.
31: */
32: private Hashtable keyTable;
33:
34: /**
35: * the table for value lookups.
36: */
37: private Hashtable valueTable;
38:
39: /**
40: * public default constructor.
41: */
42: public DoubleHashtable() {
43: keyTable = new Hashtable();
44: valueTable = new Hashtable();
45: }
46:
47: /**
48: * put a (key, value) pair into the table.
49: * @param key the key object.
50: * @param value the value object.
51: */
52: public void put(Object key, Object value) {
53: keyTable.put(key, value);
54: valueTable.put(value, key);
55: }
56:
57: /**
58: * lookup a value from the table by its key.
59: * @param key the key object
60: * @return the associated value object
61: */
62: public Object getValueByKey(Object key) {
63: return keyTable.get(key);
64: }
65:
66: /**
67: * lookup a key from the table by its value.
68: * @param value the value object
69: * @return the associated key object
70: */
71: public Object getKeyByValue(Object value) {
72: return valueTable.get(value);
73: }
74:
75: /**
76: * remove a (key, value)-entry by its key
77: * @param key the key object
78: */
79: public void removeByKey(Object key) {
80: Object value = keyTable.remove(key);
81: if (value != null) {
82: valueTable.remove(value);
83: }
84: }
85:
86: /**
87: * remove a (key, value)-entry by its value
88: * @param value the value object
89: */
90: public void removeByValue(Object value) {
91: Object key = valueTable.remove(value);
92: if (key != null) {
93: keyTable.remove(key);
94: }
95: }
96:
97: }
|