package com.digi.config.util; import java.util.Enumeration; import java.util.Vector; /** *

Vector2 implements a 2-columns vector object. The 2 columns contain a key and * a value similiar to Hashtable, however the entries in a Vector2 object is * ordered. Vector2 implements mixed interface definitions like Vector and * Hashtable. * * @see java.util.Hashtable * @see java.util.Vector */ public class Vector2 { /** key column.*/ private Vector key; /** value column.*/ private Vector val; /** Constructs an empty vector.*/ public Vector2() { key=new Vector(); val=new Vector(); } /** * Constructs an empty Vector2 with the specified storage capacity. * @param initialCapacity initial storage capacity of the vector. */ public Vector2(int initialCapacity) { key=new Vector(initialCapacity); val=new Vector(initialCapacity); } /** * Constructs an empty Vector2 with the specified storage capacity and * specified capacity increment. * @param initialCapacity initial storage capacity of the vector. * @param capacityIncrement how much to increase the elemnt's size by. */ public Vector2(int initialCapacity, int capacityIncrement) { key=new Vector(initialCapacity, capacityIncrement); val=new Vector(initialCapacity, capacityIncrement); } /** * Adds the key/value pair as the last elements of the vector. * @param inKey the key object to insert at the specified position. * @param inVal the value object to insert at the specified position. */ final synchronized void addElement(Object inKey, Object inVal) { key.addElement(inKey); val.addElement(inVal); } /** * Returns the current capacity of the vector. */ public final int capacity() { return key.capacity(); } /** * Removes all key/value elements in the vector. The vector becomes empty. */ public final synchronized void clear() { key.removeAllElements(); val.removeAllElements(); } /** * Returns true if the collection contains inVal element in the value column. * @param inVal the value to search for. */ public final synchronized boolean contains(Object inVal) { return (val.indexOf(inVal)!=-1); } /** * Returns true if the collection contains inKey element in the key column. * @param inKey the key to search for. */ public final synchronized boolean containsKey(Object inKey) { return (key.indexOf(inKey)!=-1); } /** * Returns an enumeration of the value elements. */ public final synchronized Enumeration elements() { return val.elements(); } /** * Ensures that the vector has at lease the specified capacity. * @param minCapacity the desired minimum capacity. */ public final synchronized void ensureCapacity(int minCapacity) { key.ensureCapacity(minCapacity); val.ensureCapacity(minCapacity); } /** * Gets the value object associated with the specified key in the vector. * @param inKey the key to search for. * @return the value element for the specified key or null of the key * is not defined. */ public final synchronized Object get(Object inKey) { int keyIdx=key.indexOf(inKey); Object retObj=null; try { retObj=keyIdx==-1?null:val.elementAt(keyIdx); } catch (Exception e) { } return retObj; } /** * Inserts an key/value pair elements at the specified index. Elements with * an index greater than or equal to the current index are moved up. * @param inKey the key object to insert at the specified position. * @param inVal the value object to insert at the specified position. * @param index the specified index. * @exception ArrayIndexOutOfBoundsException If an invalid index was given. */ public final synchronized void insertElementAt (Object inKey, Object inVal, int index) throws ArrayIndexOutOfBoundsException { key.insertElementAt(inKey, index); val.insertElementAt(inVal, index); } /** * Returns true if the collection contains no values. */ public final boolean isEmpty() { return key.isEmpty(); } /** * Returns the key element at the specified index. * @param index the index of the desired key object. * @exception ArrayIndexOutOfBoundsException If an invalid index was given. */ public final synchronized Object keyElementAt(int index) throws ArrayIndexOutOfBoundsException { return key.elementAt(index); } /** * Searches for the specified key object, starting from the first position and * returns and index to it. * @param inKey the key object to search. * @return the index of the key in the vector, or -1 if it was not found. */ public final int keyIndexOf(Object inKey) { return key.indexOf(inKey); } /** * Searches backward for the specified key object, starting from the last position and * returns and index to it. * @param inKey the key object to search. * @return the index of the key in the vector, or -1 if it was not found. */ public final int keyLastIndexOf(Object inKey) { return key.lastIndexOf(inKey); } /** * Returns an enumeration of the key elements. */ public final synchronized Enumeration keys() { return key.elements(); } /** * Puts the specified value element into the vector, using the specified key. * The value element can be retrieved by doing a get() with the same key. The key and * the value element can not be null. * @param inKey the specified key in the vector. * @param inVal the value element to be put. */ public final synchronized void put(Object inKey, Object inVal) { key.addElement(inKey); val.addElement(inVal); } /** * Deletes the key/val elements that matches the inKey. If the inKey * occurs more than once, only the first is removed. If the inKey is * not an element, return false. * @param inKey the key of the element to be deleted. * @return true if elemnt was removed, otherwise returns false. */ public final synchronized boolean removeElement(Object inKey) { int index=key.indexOf(inKey); if (index!=-1) { try { key.removeElementAt(index); val.removeElementAt(index); } catch (Exception e) { } } return (index!=-1); } /** * Deletes the key/val elements at the specified index. Elements with an index * greater than the current index are moved down. * @param index the elements to remove * @exception ArrayIndexOutOfBoundsException If an invalid index was given. */ public final synchronized void removeElementAt(int index) throws ArrayIndexOutOfBoundsException { key.removeElementAt(index); val.removeElementAt(index); } /** * Sets the element at the specified index to be the sepcified key/value. * The previous key/value pair at the index position is discarded. * @param inKey the key object to set at the specified position. * @param inVal the value object to set at the specified position. * @param index the specified index. * @exception ArrayIndexOutOfBoundsException If an invalid index was given. */ public final synchronized void setElementAt(Object inKey, Object inVal, int index) throws ArrayIndexOutOfBoundsException { key.setElementAt(inKey, index); val.setElementAt(inVal, index); } /** * Sets the size of the vector. If the size shrinks, the extra elements * (at the end of the vector) are lost. If the size increases, the new * elements are set to null. * @param newSize the new size of the vector. */ public final synchronized void setSize(int newSize) { key.setSize(newSize); val.setSize(newSize); } /** * Returns the number of elements in the vector. */ public final int size() { return key.size(); } public String toString() { return "in Vector2"; } /** * Trims the vector's capacity down to size. */ public final synchronized void trimToSize() { key.trimToSize(); val.trimToSize(); } /** * Returns the value element at the specified index. * @param index the index of the desired value object. * @exception ArrayIndexOutOfBoundsException If an invalid index was given. */ public final synchronized Object valueElementAt(int index) throws ArrayIndexOutOfBoundsException { return val.elementAt(index); } /** * Searches for the specified value object, starting from the first position and * returns and index to it. * @param inVal the value object to search. * @return the index of the value in the vector, or -1 if it was not found. */ public final int valueIndexOf(Object inVal) { return val.indexOf(inVal); } /** * Searches backward for the specified value object, starting from the last position and * returns and index to it. * @param inVal the value object to search. * @return the index of the value in the vector, or -1 if it was not found. */ public final int valueLastIndexOf(Object inVal) { return val.lastIndexOf(inVal); } }