Coverage Report - org.apache.ojb.broker.util.DoubleHashtable
 
Classes in this File Line Coverage Branch Coverage Complexity
DoubleHashtable
N/A
N/A
1.333
 
 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;
 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.1 2007-08-24 22:17:36 ewestfal Exp $
 27  
  */
 28  
 public class DoubleHashtable
 29  
 {
 30  
     /**
 31  
      * the table for key lookups.
 32  
      */
 33  
     private Hashtable keyTable;
 34  
 
 35  
     /**
 36  
      * the table for value lookups.
 37  
      */
 38  
     private Hashtable valueTable;
 39  
 
 40  
     /**
 41  
      * public default constructor.
 42  
      */
 43  
     public DoubleHashtable()
 44  
     {
 45  
         keyTable = new Hashtable();
 46  
         valueTable = new Hashtable();
 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 key, Object 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 getValueByKey(Object 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 getKeyByValue(Object 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 key)
 85  
     {
 86  
         Object 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 value)
 98  
     {
 99  
         Object key = valueTable.remove(value);
 100  
         if (key != null)
 101  
         {
 102  
             keyTable.remove(key);
 103  
         }
 104  
     }
 105  
 
 106  
 }