KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > broker > util > DoubleHashtable


1 package org.apache.ojb.broker.util;
2
3 /* Copyright 2002-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * 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 JavaDoc;
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     /**
31      * the table for key lookups.
32      */

33     private Hashtable JavaDoc keyTable;
34
35     /**
36      * the table for value lookups.
37      */

38     private Hashtable JavaDoc valueTable;
39
40     /**
41      * public default constructor.
42      */

43     public DoubleHashtable()
44     {
45         keyTable = new Hashtable JavaDoc();
46         valueTable = new Hashtable JavaDoc();
47     }
48
49     /**
50      * put a (key, value) pair into the table.
51      * @param key the key object.
52      * @param value the value object.
53      */

54     public void put(Object JavaDoc key, Object JavaDoc value)
55     {
56         keyTable.put(key, value);
57         valueTable.put(value, key);
58     }
59
60     /**
61      * lookup a value from the table by its key.
62      * @param key the key object
63      * @return the associated value object
64      */

65     public Object JavaDoc getValueByKey(Object JavaDoc key)
66     {
67         return keyTable.get(key);
68     }
69
70     /**
71      * lookup a key from the table by its value.
72      * @param value the value object
73      * @return the associated key object
74      */

75     public Object JavaDoc getKeyByValue(Object JavaDoc value)
76     {
77         return valueTable.get(value);
78     }
79
80     /**
81      * remove a (key, value)-entry by its key
82      * @param key the key object
83      */

84     public void removeByKey(Object JavaDoc key)
85     {
86         Object JavaDoc value = keyTable.remove(key);
87         if (value != null)
88         {
89             valueTable.remove(value);
90         }
91     }
92
93     /**
94      * remove a (key, value)-entry by its value
95      * @param value the value object
96      */

97     public void removeByValue(Object JavaDoc value)
98     {
99         Object JavaDoc key = valueTable.remove(value);
100         if (key != null)
101         {
102             keyTable.remove(key);
103         }
104     }
105
106 }
107
Popular Tags