Evaluating Software Design Patterns
— the "Gang of Four" patterns implemented in Java 6

dk.rode.thesis.meta.util
Class KeyValue<K,V>

java.lang.Object
  extended by dk.rode.thesis.meta.util.KeyValue<K,V>

public class KeyValue<K,V>
extends Object

A placeholder for a key with an associated value, where a non-null key or value can only be set once.

This class is not thread-safe!

Author:
Gunni Rode / rode.dk

Field Summary
private  K key
          The key.
private  V value
          The value, which can only be non-null if a key is also set.
 
Constructor Summary
KeyValue(K key)
          Constructor.
KeyValue(KeyValue<? extends K,? extends V> keyValue)
          Copy constructor.
KeyValue(K key, V value)
          Constructor.
 
Method Summary
 KeyValue<K,V> copyKey()
          Returns a new (key,value) placeholder with a value of null and a key value matching the key value of this (key,value) placeholder.
 boolean equals(Object o)
          Returns true if o is a (key,value) placeholder having a key that is equivalent to the key of this (key,value) placeholder, regardless of the associated values, false if not.
 K getKey()
          Returns the key of this (key,value) placeholder.
 V getValue()
          Returns the value of this (key,value) placeholder.
 int hashCode()
          Returns the hash code of this (key,value) placeholder.
 KeyValue<K,V> setKey(K key)
          Sets the key of this (key,value) placeholder to key.
 KeyValue<K,V> setValue(V value)
          Sets the value of this (key,value) placeholder to value.
 String toString()
          Returns the string representation of this (key,value) placeholder.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

key

private K key
The key.

Can be null if not set.


value

private V value
The value, which can only be non-null if a key is also set.

Can be null if not set.

Constructor Detail

KeyValue

public KeyValue(K key)
Constructor.

Parameters:
key - The key; can be null.

KeyValue

public KeyValue(KeyValue<? extends K,? extends V> keyValue)
Copy constructor.

Parameters:
keyValue - The (key,value) placeholder to copy; cannot be null.
Throws:
NullPointerException - If keyValue is null.

KeyValue

public KeyValue(K key,
                V value)
Constructor.

Parameters:
key - The key; can be null.
value - The value; can be null, and not allowed to be non-null if key is null.
Throws:
IllegalStateException - If key is null and value is not.
Method Detail

copyKey

public KeyValue<K,V> copyKey()
Returns a new (key,value) placeholder with a value of null and a key value matching the key value of this (key,value) placeholder.

Returns:
A new (key,value) placeholder; never null.

equals

public boolean equals(Object o)
Returns true if o is a (key,value) placeholder having a key that is equivalent to the key of this (key,value) placeholder, regardless of the associated values, false if not.

Overrides:
equals in class Object
Parameters:
o - The object to test; can be null.
Returns:
True if equal, false if not.

getKey

public K getKey()
Returns the key of this (key,value) placeholder.

Returns:
The key; can be null if not set.

getValue

public V getValue()
Returns the value of this (key,value) placeholder.

Returns:
The value; can be null if not set, but never null if non-null a key is set.

hashCode

public int hashCode()
Returns the hash code of this (key,value) placeholder.

Overrides:
hashCode in class Object
Returns:
The hash code.

setKey

public KeyValue<K,V> setKey(K key)
Sets the key of this (key,value) placeholder to key.

Parameters:
key - The key to set; can be null.
Returns:
This (key,value) placeholder; never null.
Throws:
IllegalStateException - If a non-null key has already been set.

setValue

public KeyValue<K,V> setValue(V value)
Sets the value of this (key,value) placeholder to value.

Parameters:
value - The value to set; can be null.
Returns:
This (key,value) placeholder; never null.
Throws:
IllegalStateException - If a non-null key has not been set, or if a non-null value has been set.

toString

public String toString()
Returns the string representation of this (key,value) placeholder.

Overrides:
toString in class Object
Returns:
The string representation; never null.

Gunni Rode / rode.dk

Feel free to use and/or modify the Java 6 source code developed for this thesis AT YOUR OWN RISK, but note that the source code comes WITHOUT ANY — and I do mean WITHOUT ANY — form of warranty WHAT SO EVER!

The original thesis and source code are available at rode.dk/thesis.