diff options
Diffstat (limited to 'projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSDictionary.java')
| -rw-r--r-- | projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSDictionary.java | 85 |
1 files changed, 49 insertions, 36 deletions
diff --git a/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSDictionary.java b/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSDictionary.java index a4e9cd5..f72d717 100644 --- a/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSDictionary.java +++ b/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSDictionary.java @@ -32,8 +32,10 @@ import java.util.Map; * @author $Author: cgruber $ * @version $Revision: 893 $ */ -public class NSDictionary extends HashMap implements NSKeyValueCoding { - public static final NSDictionary EmptyDictionary = new NSDictionary(); +public class NSDictionary<K, V> extends HashMap<K, V> implements NSKeyValueCoding { + private static final long serialVersionUID = -5043903788052328633L; + + public static final NSDictionary<?, ?> EmptyDictionary = new NSDictionary<>(); /** * Default constructor produces an empty dictionary. @@ -52,7 +54,7 @@ public class NSDictionary extends HashMap implements NSKeyValueCoding { /** * Produces a dictionary that contains one key referencing one value. */ - public NSDictionary(Object key, Object value) { + public NSDictionary(K key, V value) { super(); put(key, value); } @@ -61,7 +63,7 @@ public class NSDictionary extends HashMap implements NSKeyValueCoding { * Produces a dictionary containing the specified keys and values. An * IllegalArgumentException is thrown if the arrays are not of the same length. */ - public NSDictionary(Object[] objects, Object[] keys) { + public NSDictionary(V[] objects, K[] keys) { super(); if (keys.length != objects.length) { throw new IllegalArgumentException("Array lengths do not match."); @@ -75,7 +77,7 @@ public class NSDictionary extends HashMap implements NSKeyValueCoding { /** * Produces a dictionary that is a copy of the specified map (or dictionary). */ - public NSDictionary(Map aMap) { + public NSDictionary(Map<K, V> aMap) { super(aMap); } @@ -89,21 +91,21 @@ public class NSDictionary extends HashMap implements NSKeyValueCoding { /** * Returns an NSArray containing all keys in this dictionary. */ - public NSArray allKeys() { - return new NSArray(keySet()); + public NSArray<K> allKeys() { + return new NSArray<>(keySet()); } /** * Returns an NSArray containing all keys that reference the specified value. */ - public NSArray allKeysForObject(Object value) { - NSMutableArray result = new NSMutableArray(); - Map.Entry entry; - Iterator it = entrySet().iterator(); + public NSArray<K> allKeysForObject(V value) { + NSMutableArray<K> result = new NSMutableArray<>(); + Map.Entry<K, V> entry; + Iterator<Map.Entry<K, V>> it = entrySet().iterator(); while (it.hasNext()) { - entry = (Map.Entry) it.next(); + entry = (Map.Entry<K, V>) it.next(); // handle null values if ((value == null) && (entry.getValue() == null) || (value.equals(entry.getValue()))) { @@ -118,15 +120,15 @@ public class NSDictionary extends HashMap implements NSKeyValueCoding { /** * Returns an NSArray containing all values in this dictionary. */ - public NSArray allValues() { - return new NSArray(values()); + public NSArray<V> allValues() { + return new NSArray<>(values()); } /** * Returns whether the specified dictionary has the same or equivalent key-value * pairs as this dictionary. */ - public boolean isEqualToDictionary(NSDictionary aDictionary) { + public boolean isEqualToDictionary(NSDictionary<K, V> aDictionary) { return equals(aDictionary); } @@ -135,13 +137,13 @@ public class NSDictionary extends HashMap implements NSKeyValueCoding { * found, the marker parameter will be placed in the corresponding index(es) in * the returned array. */ - public NSArray objectsForKeys(NSArray anArray, Object aMarker) { - NSMutableArray result = new NSMutableArray(); + public NSArray<V> objectsForKeys(NSArray<K> anArray, V aMarker) { + NSMutableArray<V> result = new NSMutableArray<>(); if (anArray == null) return result; - Object value; - Enumeration enumeration = anArray.objectEnumerator(); + V value; + Enumeration<K> enumeration = anArray.objectEnumerator(); while (enumeration.hasMoreElements()) { value = objectForKey(enumeration.nextElement()); if (value == null) { @@ -155,15 +157,15 @@ public class NSDictionary extends HashMap implements NSKeyValueCoding { /** * Returns an enumeration over the keys in this dictionary. */ - public java.util.Enumeration keyEnumerator() { - return new java.util.Enumeration() { - Iterator it = NSDictionary.this.keySet().iterator(); + public java.util.Enumeration<K> keyEnumerator() { + return new java.util.Enumeration<>() { + Iterator<K> it = NSDictionary.this.keySet().iterator(); public boolean hasMoreElements() { return it.hasNext(); } - public Object nextElement() { + public K nextElement() { return it.next(); } }; @@ -172,15 +174,15 @@ public class NSDictionary extends HashMap implements NSKeyValueCoding { /** * Returns an enumeration over the values in this dictionary. */ - public java.util.Enumeration objectEnumerator() { - return new java.util.Enumeration() { - Iterator it = NSDictionary.this.values().iterator(); + public java.util.Enumeration<V> objectEnumerator() { + return new java.util.Enumeration<>() { + Iterator<V> it = NSDictionary.this.values().iterator(); public boolean hasMoreElements() { return it.hasNext(); } - public Object nextElement() { + public V nextElement() { return it.next(); } }; @@ -189,43 +191,53 @@ public class NSDictionary extends HashMap implements NSKeyValueCoding { /** * Returns the value for the specified key, or null if the key is not found. */ - public Object objectForKey(Object aKey) { + public V objectForKey(K aKey) { return get(aKey); } // interface NSKeyValueCoding - + @SuppressWarnings("unchecked") + @Override public Object valueForKey(String aKey) { // System.out.println( "valueForKey: " + aKey + "->" + this ); - Object result = objectForKey(aKey); + Object result = objectForKey((K) aKey); if (result == null) result = NSKeyValueCodingSupport.valueForKey(this, aKey); return result; } + @SuppressWarnings("unchecked") + @Override public void takeValueForKey(Object aValue, String aKey) { // System.out.println( "takeValueForKey: " + aKey + " : " // + aValue + "->" + this ); - put(aKey, aValue); // FIXME: technically cheating since this is a read-only class + put((K)aKey, (V)aValue); // FIXME: technically cheating since this is a read-only class } + @SuppressWarnings("unchecked") + @Override public Object storedValueForKey(String aKey) { - Object result = objectForKey(aKey); + Object result = objectForKey((K) aKey); if (result == null) result = NSKeyValueCodingSupport.storedValueForKey(this, aKey); return result; } + @SuppressWarnings("unchecked") + @Override public void takeStoredValueForKey(Object aValue, String aKey) { - put(aKey, aValue); // FIXME: technically cheating since this is a read-only class + put((K)aKey, (V)aValue); // FIXME: technically cheating since this is a read-only class } + @Override public Object handleQueryWithUnboundKey(String aKey) { return NSKeyValueCodingSupport.handleQueryWithUnboundKey(this, aKey); } + @Override public void handleTakeValueForUnboundKey(Object aValue, String aKey) { NSKeyValueCodingSupport.handleTakeValueForUnboundKey(this, aValue, aKey); } + @Override public void unableToSetNullForKey(String aKey) { NSKeyValueCodingSupport.unableToSetNullForKey(this, aKey); } @@ -234,10 +246,11 @@ public class NSDictionary extends HashMap implements NSKeyValueCoding { throw new RuntimeException("Not implemented yet."); } + @SuppressWarnings("unchecked") + @Override public String toString() { StringBuffer buf = new StringBuffer(); - Enumeration enumeration = keyEnumerator(); - boolean quote = false; + Enumeration<K> enumeration = keyEnumerator(); buf.append(NSPropertyListSerialization.TOKEN_BEGIN[NSPropertyListSerialization.PLIST_DICTIONARY]); while (enumeration.hasMoreElements()) { if (buf.length() == 1) @@ -245,7 +258,7 @@ public class NSDictionary extends HashMap implements NSKeyValueCoding { Object k = enumeration.nextElement(); buf.append(NSPropertyListSerialization.stringForPropertyList(k)); buf.append(" = "); - k = objectForKey(k); + k = objectForKey((K) k); buf.append(NSPropertyListSerialization.stringForPropertyList(k)); buf.append("; "); } |
