com.alee.utils.collection
Class ValuesTable<K,V>

java.lang.Object
  extended by com.alee.utils.collection.ValuesTable<K,V>
All Implemented Interfaces:
java.io.Serializable

@XStreamConverter(value=ValuesTableConverter.class)
public class ValuesTable<K,V>
extends java.lang.Object
implements java.io.Serializable

This class allows you to create key-value table from which you can either request a key by value or a value by key. Both key and value should be unique and adding same key or value will replace existing one in the lists.

Author:
Mikle Garin
See Also:
Serialized Form

Field Summary
protected  java.util.List<K> keys
          Keys list.
protected  java.util.Map<V,K> keysByValues
          Keys by values map.
protected  java.util.List<V> values
          Values list.
protected  java.util.Map<K,V> valuesByKeys
          Values by keys map.
 
Constructor Summary
ValuesTable()
          Constructs an empty ValuesTable with initial capacity of ten.
ValuesTable(int initialCapacity)
          Constructs an empty ValuesTable with the specified initial capacity.
 
Method Summary
 void add(int index, K key, V value)
          Adds a single entry (key-value pair) into this ValuesTable at the specified index.
 void add(K key, V value)
          Adds a single entry (key-value pair) into this ValuesTable.
 void addAll(java.util.Map<K,V> data)
          Adds all Map entries into this ValuesTable.
 void addAll(ValuesTable<K,V> data)
          Adds all ValuesTable entries into this ValuesTable.
 boolean contains(K key)
          Returns whether this ValuesTable contains entry with specified key or not.
 boolean containsKey(K key)
          Returns whether this ValuesTable contains entry with specified key or not.
 boolean containsValue(V value)
          Returns whether this ValuesTable contains entry with specified value or not.
 V get(int index)
          Returns entry value at the specified index.
 V get(K key)
          Returns value from entry with specified key.
 K getKey(int index)
          Returns entry key at the specified index.
 K getKey(V value)
          Returns key from entry with specified value.
 java.util.List<K> getKeys()
          Returns copy of keys list.
 V getValue(int index)
          Returns entry value at the specified index.
 V getValue(K key)
          Returns value from entry with specified key.
 java.util.List<V> getValues()
          Returns copy of values list.
 int indexOf(K key)
          Returns index of entry with the specified key.
 int indexOfKey(K key)
          Returns index of entry with the specified key.
 int indexOfValue(V value)
          Returns index of entry with the specified value.
protected  java.lang.String outOfBoundsMsg(int index)
          Returns a message text for IndexOutOfBoundsException exception.
 void put(int index, K key, V value)
          Adds a single entry (key-value pair) into this ValuesTable at the specified index.
 void put(K key, V value)
          Adds a single entry (key-value pair) into this ValuesTable.
 void putAll(java.util.Map<K,V> data)
          Adds all Map entries into this ValuesTable.
 void putAll(ValuesTable<K,V> data)
          Adds all ValuesTable entries into this ValuesTable.
 void remove(int index)
          Removes entry at the specified index.
 void remove(K key)
          Removes entry with the specified key.
 void removeByKey(K key)
          Removes entry with the specified key.
 void removeByValue(V value)
          Removes entry with the specified value.
protected  void removeExistingEntry(int index)
          Removes entry at the specified index.
 int size()
          Returns ValuesTable size.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

keys

protected java.util.List<K> keys
Keys list.


values

protected java.util.List<V> values
Values list.


valuesByKeys

protected java.util.Map<K,V> valuesByKeys
Values by keys map.


keysByValues

protected java.util.Map<V,K> keysByValues
Keys by values map.

Constructor Detail

ValuesTable

public ValuesTable()
Constructs an empty ValuesTable with initial capacity of ten.


ValuesTable

public ValuesTable(int initialCapacity)
Constructs an empty ValuesTable with the specified initial capacity.

Parameters:
initialCapacity - initial table capacity
Method Detail

getKeys

public java.util.List<K> getKeys()
Returns copy of keys list.

Returns:
keys list copy

getValues

public java.util.List<V> getValues()
Returns copy of values list.

Returns:
values list copy

addAll

public void addAll(java.util.Map<K,V> data)
Adds all Map entries into this ValuesTable.

Parameters:
data - Map with entries

putAll

public void putAll(java.util.Map<K,V> data)
Adds all Map entries into this ValuesTable.

Parameters:
data - Map with entries

addAll

public void addAll(ValuesTable<K,V> data)
Adds all ValuesTable entries into this ValuesTable.

Parameters:
data - ValuesTable with entries

putAll

public void putAll(ValuesTable<K,V> data)
Adds all ValuesTable entries into this ValuesTable.

Parameters:
data - ValuesTable with entries

add

public void add(K key,
                V value)
Adds a single entry (key-value pair) into this ValuesTable.

Parameters:
key - entry key
value - entry value

put

public void put(K key,
                V value)
Adds a single entry (key-value pair) into this ValuesTable.

Parameters:
key - entry key
value - entry value

add

public void add(int index,
                K key,
                V value)
Adds a single entry (key-value pair) into this ValuesTable at the specified index.

Parameters:
index - entry index
key - entry key
value - entry value

put

public void put(int index,
                K key,
                V value)
Adds a single entry (key-value pair) into this ValuesTable at the specified index.

Parameters:
index - entry index
key - entry key
value - entry value

remove

public void remove(K key)
Removes entry with the specified key.

Parameters:
key - entry key

removeByKey

public void removeByKey(K key)
Removes entry with the specified key.

Parameters:
key - entry key

removeByValue

public void removeByValue(V value)
Removes entry with the specified value.

Parameters:
value - entry value

remove

public void remove(int index)
Removes entry at the specified index.

Parameters:
index - entry index

removeExistingEntry

protected void removeExistingEntry(int index)
Removes entry at the specified index.

Parameters:
index - entry index

get

public V get(int index)
Returns entry value at the specified index.

Parameters:
index - entry index
Returns:
entry value

getValue

public V getValue(int index)
Returns entry value at the specified index.

Parameters:
index - entry index
Returns:
entry value

getKey

public K getKey(int index)
Returns entry key at the specified index.

Parameters:
index - entry index
Returns:
entry key

get

public V get(K key)
Returns value from entry with specified key.

Parameters:
key - entry key
Returns:
entry value

getValue

public V getValue(K key)
Returns value from entry with specified key.

Parameters:
key - entry key
Returns:
entry value

getKey

public K getKey(V value)
Returns key from entry with specified value.

Parameters:
value - entry value
Returns:
entry key

contains

public boolean contains(K key)
Returns whether this ValuesTable contains entry with specified key or not.

Parameters:
key - entry key
Returns:
true if this ValuesTable contains such entry, false otherwise

containsKey

public boolean containsKey(K key)
Returns whether this ValuesTable contains entry with specified key or not.

Parameters:
key - entry key
Returns:
true if this ValuesTable contains such entry, false otherwise

containsValue

public boolean containsValue(V value)
Returns whether this ValuesTable contains entry with specified value or not.

Parameters:
value - entry value
Returns:
true if this ValuesTable contains such entry, false otherwise

indexOf

public int indexOf(K key)
Returns index of entry with the specified key.

Parameters:
key - entry key
Returns:
entry index

indexOfKey

public int indexOfKey(K key)
Returns index of entry with the specified key.

Parameters:
key - entry key
Returns:
entry index

indexOfValue

public int indexOfValue(V value)
Returns index of entry with the specified value.

Parameters:
value - entry value
Returns:
entry index

size

public int size()
Returns ValuesTable size.

Returns:
ValuesTable size

outOfBoundsMsg

protected java.lang.String outOfBoundsMsg(int index)
Returns a message text for IndexOutOfBoundsException exception.

Parameters:
index - index that is out of bounds
Returns:
message text