summaryrefslogtreecommitdiff
path: root/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/internal/QueueMap.java
diff options
context:
space:
mode:
Diffstat (limited to 'projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/internal/QueueMap.java')
-rw-r--r--projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/internal/QueueMap.java127
1 files changed, 74 insertions, 53 deletions
diff --git a/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/internal/QueueMap.java b/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/internal/QueueMap.java
index 59104e5..2f6accf 100644
--- a/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/internal/QueueMap.java
+++ b/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/internal/QueueMap.java
@@ -22,9 +22,9 @@ import java.util.*; //collections
/**
* A queue based implementation of the Map interface. This class provides for
- * all the opertions of a map, but keeps the entries in the same sequence as
+ * all the operations of a map, but keeps the entries in the same sequence as
* originally added. The first entry placed in the map will be the first entry
- * retreived during iteration: first-in, first-out (FIFO). <BR>
+ * retrieved during iteration: first-in, first-out (FIFO). <BR>
* <BR>
*
* Keys cannot be duplicated. If an entry is made using a key that already
@@ -33,9 +33,9 @@ import java.util.*; //collections
* <BR>
*
* Some convenience methods are provided for the queue type operations. The
- * first key can be retreived as well as the last key. A key can be used to
- * retreive its corresponding value from the map. A value can also be used to
- * retreive its key from the map, however, since there may be multiple values of
+ * first key can be retrieved as well as the last key. A key can be used to
+ * retrieve its corresponding value from the map. A value can also be used to
+ * retrieve its key from the map, however, since there may be multiple values of
* the same equality, the first key found will be returned. <BR>
* <BR>
*
@@ -44,31 +44,31 @@ import java.util.*; //collections
* @date $Date: 2006-02-18 17:41:36 -0500 (Sat, 18 Feb 2006) $
* @revision $Revision: 899 $
*/
-public class QueueMap implements Map {
- List values;
- List keys;
- Map keyToValue;
+public class QueueMap<K, V> implements Map<K, V> {
+ List<V> values;
+ List<K> keys;
+ Map<K, V> keyToValue;
/**
* Creates an empty QueueMap.
*/
public QueueMap() {
- values = new LinkedList();
- keys = new LinkedList();
- keyToValue = new HashMap();
+ values = new LinkedList<>();
+ keys = new LinkedList<>();
+ keyToValue = new HashMap<>();
}
/**
* Creates a QueueMap and places the entries from the passed in map into this
- * map. The order of the entries is based on however the iterator iteratated
+ * map. The order of the entries is based on however the iterator iterated
* through the map entries.
*
* @param t A map object.
*/
- public QueueMap(Map t) {
- values = new ArrayList();
- keys = new ArrayList();
- keyToValue = new HashMap();
+ public QueueMap(Map<K, V> t) {
+ values = new ArrayList<>();
+ keys = new ArrayList<>();
+ keyToValue = new HashMap<>();
putAll(t);
}
@@ -76,6 +76,7 @@ public class QueueMap implements Map {
/**
* Removes all the entries from this map.
*/
+ @Override
public void clear() {
values.clear();
keys.clear();
@@ -89,6 +90,7 @@ public class QueueMap implements Map {
*
* @return True if the map contains the given key, false otherwise.
*/
+ @Override
public boolean containsKey(Object key) {
return keyToValue.containsKey(key);
}
@@ -102,6 +104,7 @@ public class QueueMap implements Map {
*
* @return True if the map contains the given value, false otherwise.
*/
+ @Override
public boolean containsValue(Object value) {
return keyToValue.containsValue(value);
}
@@ -114,11 +117,12 @@ public class QueueMap implements Map {
*
* @return a set view of the mappings contained in this map.
*/
- public Set entrySet() {
- Set result = new HashSet(keys.size(), 1F);
+ @Override
+ public Set<Map.Entry<K, V>> entrySet() {
+ Set<Map.Entry<K, V>> result = new HashSet<>(keys.size(), 1F);
for (int i = 0; i < keys.size(); i++) {
- result.add(new KeyValuePair(keys.get(i), values.get(i)));
+ result.add(new KeyValuePair<>(keys.get(i), values.get(i)));
}
return result;
@@ -135,6 +139,7 @@ public class QueueMap implements Map {
* @param o object to be compared for equality with this map.
* @return <tt>true</tt> if the specified object is equal to this map.
*/
+ @Override
public boolean equals(Object o) {
return keyToValue.equals(o);
}
@@ -149,7 +154,8 @@ public class QueueMap implements Map {
* @return The value corresponding to the key (can be null), or null if the key
* is not contained in the map.
*/
- public Object get(Object key) {
+ @Override
+ public V get(Object key) {
return keyToValue.get(key);
}
@@ -162,6 +168,7 @@ public class QueueMap implements Map {
*
* @return the hash code value for this map.
*/
+ @Override
public int hashCode() {
return keyToValue.hashCode();
}
@@ -171,6 +178,7 @@ public class QueueMap implements Map {
*
* @return True is this map contains no entries, false otherwise.
*/
+ @Override
public boolean isEmpty() {
return keyToValue.isEmpty();
}
@@ -181,8 +189,9 @@ public class QueueMap implements Map {
*
* @return A Set containing all the keys used in the map.
*/
- public Set keySet() {
- Set result = new HashSet(keys.size(), 1F);
+ @Override
+ public Set<K> keySet() {
+ Set<K> result = new HashSet<>(keys.size(), 1F);
for (int i = 0; i < keys.size(); i++) {
result.add(keys.get(i));
@@ -202,7 +211,8 @@ public class QueueMap implements Map {
* @return Null if the key is new, the replaced value if the key already
* existed. (Note: If the key was null, then null is returned.)
*/
- public Object put(Object key, Object value) {
+ @Override
+ public V put(K key, V value) {
if (key == null)
return null;
@@ -223,15 +233,16 @@ public class QueueMap implements Map {
*
* @param t A map of key/value entries.
*/
- public void putAll(Map t) {
+ @Override
+ public void putAll(Map<? extends K, ? extends V> t) {
if (t == null) {
// Nothing to do!
return;
}
// Place the entries from the passed in map into this map.
- for (Iterator it = t.keySet().iterator(); it.hasNext();) {
- Object aKey = it.next();
+ for (Iterator<? extends K> it = t.keySet().iterator(); it.hasNext();) {
+ K aKey = it.next();
put(aKey, t.get(aKey));
}
}
@@ -245,11 +256,12 @@ public class QueueMap implements Map {
* Null could also be returned if the associated value of the key was
* null.
*/
- public Object remove(Object key) {
+ @Override
+ public V remove(Object key) {
if (key == null)
return null;
- Object value = null;
+ V value = null;
if (containsKey(key)) {
value = keyToValue.remove(key);
@@ -268,6 +280,7 @@ public class QueueMap implements Map {
*
* @return The number of pairs.
*/
+ @Override
public int size() {
return values.size();
}
@@ -280,7 +293,7 @@ public class QueueMap implements Map {
*
* @return An ordered list of keys.
*/
- public List keys() {
+ public List<K> keys() {
return keys;
}
@@ -292,7 +305,8 @@ public class QueueMap implements Map {
*
* @return An ordered list of values.
*/
- public Collection values() {
+ @Override
+ public Collection<V> values() {
return values;
}
@@ -306,7 +320,7 @@ public class QueueMap implements Map {
* @return The value corresponding to the key (can be null), or null if the key
* is not contained in the map.
*/
- public Object getValueForKey(Object key) {
+ public V getValueForKey(K key) {
return keyToValue.get(key);
}
@@ -318,7 +332,7 @@ public class QueueMap implements Map {
* @param value A value that is contained in this map.
* @return A first key that corresponds to this value.
*/
- public Object getKeyForValue(Object value) {
+ public K getKeyForValue(V value) {
int i = values.indexOf(value);
if (i != -1) {
return keys.get(i);
@@ -331,7 +345,7 @@ public class QueueMap implements Map {
*
* @return The first key in the map, null if there are no mappings.
*/
- public Object getFirstKey() {
+ public K getFirstKey() {
if (keys.size() < 1) {
return null;
}
@@ -343,7 +357,7 @@ public class QueueMap implements Map {
*
* @return The last key in the map, null if there are no mappings.
*/
- public Object getLastKey() {
+ public K getLastKey() {
if (keys.size() < 1) {
return null;
}
@@ -354,9 +368,9 @@ public class QueueMap implements Map {
* This class contains a key/value pair. The key must be a valid object, it
* cannot be null. The value can be a valid object or null.
*/
- static public class KeyValuePair implements Map.Entry {
- Object key;
- Object value;
+ static public class KeyValuePair<K, V> implements Map.Entry<K, V> {
+ K key;
+ V value;
/**
* Default constructor. The constructor takes the key and value as parameters.
@@ -366,7 +380,7 @@ public class QueueMap implements Map {
* @param key The key object of this pair. The key cannot be null.
* @param value The value object of this pair. The value can be null.
*/
- public KeyValuePair(Object aKey, Object aValue) {
+ public KeyValuePair(K aKey, V aValue) {
key = aKey;
value = aValue;
}
@@ -376,7 +390,8 @@ public class QueueMap implements Map {
*
* @return The key object.
*/
- public Object getKey() {
+ @Override
+ public K getKey() {
return key;
}
@@ -385,7 +400,8 @@ public class QueueMap implements Map {
*
* @return The value object.
*/
- public Object getValue() {
+ @Override
+ public V getValue() {
return value;
}
@@ -394,15 +410,18 @@ public class QueueMap implements Map {
*
* @param aValue The value object to place into this pair.
*/
- public Object setValue(Object aValue) {
- Object result = value;
+ @Override
+ public V setValue(V aValue) {
+ V result = value;
value = aValue;
return result;
}
+ @Override
public boolean equals(Object o) {
if (o instanceof KeyValuePair) {
- KeyValuePair p = (KeyValuePair) o;
+ @SuppressWarnings("unchecked")
+ KeyValuePair<K, V> p = (KeyValuePair<K, V>) o;
if ((key.equals(p.getKey())) && (value.equals(p.getValue()))) {
return true;
}
@@ -412,9 +431,10 @@ public class QueueMap implements Map {
}
/**
- * Returns a string reprsentation of this class. The contents of the map are
+ * Returns a string representation of this class. The contents of the map are
* placed in the string in its proper order.
*/
+ @Override
public String toString() {
int max = size() - 1;
StringBuffer buf = new StringBuffer();
@@ -432,10 +452,11 @@ public class QueueMap implements Map {
}
// unit test
+ // TODO convert to JUnit test
public static void main(String[] argv) {
- QueueMap qMap;
+ QueueMap<String, String> qMap;
- qMap = new QueueMap();
+ qMap = new QueueMap<>();
for (int i = 0; i < 5; i++) {
qMap.put(Integer.toString(i), Integer.toString(i));
}
@@ -443,7 +464,7 @@ public class QueueMap implements Map {
System.out.println("Keys = " + qMap.keys());
System.out.println("Values = " + qMap.values());
- qMap = new QueueMap();
+ qMap = new QueueMap<>();
for (int i = 0; i < 5; i++) {
qMap.put(Integer.toString(i), "A");
}
@@ -451,7 +472,7 @@ public class QueueMap implements Map {
System.out.println("Keys = " + qMap.keys());
System.out.println("Values = " + qMap.values());
- qMap = new QueueMap();
+ qMap = new QueueMap<>();
for (int i = 0; i < 5; i++) {
qMap.put(Integer.toString(i), null);
}
@@ -459,17 +480,17 @@ public class QueueMap implements Map {
System.out.println("Keys = " + qMap.keys());
System.out.println("Values = " + qMap.values());
- Map aMap = new HashMap();
+ Map<String, String> aMap = new HashMap<>();
for (int i = 0; i < 5; i++) {
aMap.put(Integer.toString(i), Integer.toString(i));
}
- qMap = new QueueMap(aMap);
+ qMap = new QueueMap<>(aMap);
System.out.println("\nHashMap = " + aMap);
System.out.println("Map = " + qMap);
System.out.println("Keys = " + qMap.keys());
System.out.println("Values = " + qMap.values());
- qMap = new QueueMap();
+ qMap = new QueueMap<>();
qMap.put("Test1", "String1");
qMap.put("Test2", "String2");
qMap.put("Test3", "String3");
@@ -498,7 +519,7 @@ public class QueueMap implements Map {
qMap.put("Test12", "String12");
System.out.println("Clear Test, Map = " + qMap);
- aMap = new HashMap();
+ aMap = new HashMap<>();
aMap.put("Test10", "String10");
aMap.put("Test11", "String11");
aMap.put("Test12", "String12");