001    package org.apache.ojb.broker.util;
002    
003    /* Copyright 2002-2005 The Apache Software Foundation
004     *
005     * Licensed under the Apache License, Version 2.0 (the "License");
006     * you may not use this file except in compliance with the License.
007     * You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    import java.util.Hashtable;
019    
020    /**
021     * this class can be used to build two-way lookup tables.
022     * It provides lookup from keys to values and the inverse
023     * lookup from values to keys.
024     *
025     * @author Thomas Mahler
026     * @version $Id: DoubleHashtable.java,v 1.1 2007-08-24 22:17:36 ewestfal Exp $
027     */
028    public class DoubleHashtable
029    {
030        /**
031         * the table for key lookups.
032         */
033        private Hashtable keyTable;
034    
035        /**
036         * the table for value lookups.
037         */
038        private Hashtable valueTable;
039    
040        /**
041         * public default constructor.
042         */
043        public DoubleHashtable()
044        {
045            keyTable = new Hashtable();
046            valueTable = new Hashtable();
047        }
048    
049        /**
050         * put a (key, value) pair into the table.
051         * @param key the key object.
052         * @param value the value object.
053         */
054        public void put(Object key, Object value)
055        {
056            keyTable.put(key, value);
057            valueTable.put(value, key);
058        }
059    
060        /**
061         * lookup a value from the table by its key.
062         * @param key the key object
063         * @return the associated value object
064         */
065        public Object getValueByKey(Object key)
066        {
067            return keyTable.get(key);
068        }
069    
070        /**
071         * lookup a key from the table by its value.
072         * @param value the value object
073         * @return the associated key object
074         */
075        public Object getKeyByValue(Object value)
076        {
077            return valueTable.get(value);
078        }
079    
080        /**
081         * remove a (key, value)-entry by its key
082         * @param key the key object
083         */
084        public void removeByKey(Object key)
085        {
086            Object value = keyTable.remove(key);
087            if (value != null)
088            {
089                valueTable.remove(value);
090            }
091        }
092    
093        /**
094         * remove a (key, value)-entry by its value
095         * @param value the value object
096         */
097        public void removeByValue(Object value)
098        {
099            Object key = valueTable.remove(value);
100            if (key != null)
101            {
102                keyTable.remove(key);
103            }
104        }
105    
106    }