1 /*
   2  * $Id$
   3  *
   4  * Copyright (c) 2002, 2009, Oracle and/or its affiliates. All rights reserved.
   5  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   6  *
   7  * This code is free software; you can redistribute it and/or modify it
   8  * under the terms of the GNU General Public License version 2 only, as
   9  * published by the Free Software Foundation.  Oracle designates this
  10  * particular file as subject to the "Classpath" exception as provided
  11  * by Oracle in the LICENSE file that accompanied this code.
  12  *
  13  * This code is distributed in the hope that it will be useful, but WITHOUT
  14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  16  * version 2 for more details (a copy is included in the LICENSE file that
  17  * accompanied this code).
  18  *
  19  * You should have received a copy of the GNU General Public License version
  20  * 2 along with this work; if not, write to the Free Software Foundation,
  21  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  22  *
  23  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  24  * or visit www.oracle.com if you need additional information or have any
  25  * questions.
  26  */
  27 package com.sun.javatest.util;
  28 
  29 import java.util.Vector;
  30 
  31 /**
  32  * A map-like structure which has two side-by-side ordered sets of Objects in pairs.
  33  * This is basically a map structure except that it is always ordered and has a less
  34  * strict an idea of key and value.  The terms key and value are still used to
  35  * differentiate data from the two sets.  The primary advantage to using this class
  36  * is that you can do lookups either by key or value, in case where translation in
  37  * both directions is necessary.
  38  */
  39 public class OrderedTwoWayTable {
  40     public OrderedTwoWayTable() {
  41         keys = new Vector<>();
  42         values = new Vector<>();
  43     }
  44 
  45     /**
  46      * Put an object in the table.
  47      * @param key the key for this object
  48      * @param value the object to be stored in the table
  49      */
  50     public synchronized void put(Object key, Object value) {
  51         keys.add(key);
  52         values.add(value);
  53     }
  54 
  55     /**
  56      * Get the position of the key within the table.
  57      * @param key the key whose position is required
  58      * @return the position of the key within the table, or -1 if not found
  59      */
  60     public synchronized int getKeyIndex(Object key) {
  61         return findIndex(keys, key);
  62     }
  63 
  64     /**
  65      * Get the position of a value within the table.
  66      * @param value the value whose position is required
  67      * @return the position of the value within the table, or -1 if not found
  68      */
  69     public synchronized int getValueIndex(Object value) {
  70         return findIndex(values, value);
  71     }
  72 
  73     /**
  74      * Get the value at the given index.
  75      * @param index the index of the required value
  76      * @return the value at the given index, or null if not found or index out of bounds.
  77      */
  78     public synchronized Object getValueAt(int index) {
  79         if (index < values.size())
  80             return values.elementAt(index);
  81         else
  82             return null;
  83     }
  84 
  85     /**
  86      * Get the key at the given index.
  87      * @param index the index of the given key
  88      * @return the value at the given index, null if not found or index out of bounds.
  89      */
  90     public synchronized Object getKeyAt(int index) {
  91         if (index < keys.size())
  92             return keys.elementAt(index);
  93         else
  94             return null;
  95     }
  96 
  97     /**
  98      * Get the number of pairs in the table.
  99      * @return the numbver of pairs on the table
 100      */
 101     public synchronized int getSize() {
 102         return keys.size();
 103     }
 104 
 105     /**
 106      * Remove the object at a specified index.
 107      * @param index the index of the entry to be removed.
 108      */
 109     public synchronized void remove(int index) {
 110         if (index >= keys.size())
 111             return;
 112 
 113         keys.removeElementAt(index);
 114         values.removeElementAt(index);
 115     }
 116 
 117     /**
 118      * Get the index of the target in the table.
 119      * This is a reference equality search.
 120      * @param data the vector in which to search
 121      * @param target the object to search for
 122      * @return the index of the target in the vector, or -1 if not found
 123      */
 124     protected int findIndex(Vector data, Object target) {
 125         for (int i = 0; i < data.size(); i++)
 126             if (data.elementAt(i) == target)
 127                 return i;
 128 
 129         return -1;
 130     }
 131 
 132     private Vector<Object> keys, values;
 133 }
 134 
 135