diff options
| author | Benjamin Culkin <scorpress@gmail.com> | 2024-07-01 17:27:48 -0400 |
|---|---|---|
| committer | Benjamin Culkin <scorpress@gmail.com> | 2024-07-01 17:27:48 -0400 |
| commit | 02bc52037e9ccccca672d6156d9c325c74fe28b3 (patch) | |
| tree | db022b83c90562f461f4a27412caa9936a8dfd04 /projects/net.wotonomy.foundation/src | |
| parent | c75d7dbd613a47b217499f7a856c469a8bc59e2a (diff) | |
Update a whole bunch of things
Yeah... not a great commit message. t.b.h, I could maybe've split the
commit into more parts; but that would be quite a lot off effort and
would have a pretty decent chance of at least one of the commits leaving
the repository in a non-working state.
For the future, will want to try and commit more often so there aren't
these mega-commits where it's just "a whole bunch of stuff changed"
Diffstat (limited to 'projects/net.wotonomy.foundation/src')
30 files changed, 513 insertions, 357 deletions
diff --git a/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSArray.java b/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSArray.java index 175c3a1..3b75868 100644 --- a/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSArray.java +++ b/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSArray.java @@ -41,13 +41,13 @@ import java.util.ListIterator; * @author $Author: cgruber $ * @version $Revision: 929 $ */ -public class NSArray implements List<Object>, Serializable { +public class NSArray<T> implements List<T>, Serializable { private static final long serialVersionUID = 3640615326084287961L; /** * Actual list that backs this instance. */ - List<Object> list; + List<T> list; /** * Return value when array index is not found. @@ -57,15 +57,15 @@ public class NSArray implements List<Object>, Serializable { /** * A constant representing an empty array. */ - public static final NSArray EmptyArray = new NSArray(); + public static final NSArray<?> EmptyArray = new NSArray<>(); /** * Returns an NSArray backed by the specified List. This is useful to "protect" * an internal representation that is returned by a method of return type * NSArray. */ - public static NSArray arrayBackedByList(List<Object> aList) { - return new NSArray(aList, null); + public static <T> NSArray<T> arrayBackedByList(List<T> aList) { + return new NSArray<>(aList, null); } /** @@ -82,7 +82,7 @@ public class NSArray implements List<Object>, Serializable { * @param ignored This parameter is entirely ignored, and is only there to * distinguish the API. */ - NSArray(List<Object> aList, Object ignored) // differentiates + NSArray(List<T> aList, Object ignored) // differentiates { list = aList; } @@ -104,7 +104,7 @@ public class NSArray implements List<Object>, Serializable { /** * Produces an array containing only the specified object. */ - public NSArray(Object anObject) { + public NSArray(T anObject) { this(); list.add(anObject); } @@ -112,7 +112,7 @@ public class NSArray implements List<Object>, Serializable { /** * Produces an array containing the specified objects. */ - public NSArray(Object[] anArray) { + public NSArray(T[] anArray) { this(); for (int i = 0; i < anArray.length; i++) { list.add(anArray[i]); @@ -122,9 +122,9 @@ public class NSArray implements List<Object>, Serializable { /** * Produces an array containing the objects in the specified collection. */ - public NSArray(Collection<Object> aCollection) { + public NSArray(Collection<T> aCollection) { this(); - Iterator i = aCollection.iterator(); + Iterator<T> i = aCollection.iterator(); while (i.hasNext()) list.add(i.next()); } @@ -140,8 +140,8 @@ public class NSArray implements List<Object>, Serializable { * Returns an array containing all objects in this array plus the specified * object. */ - public NSArray arrayByAddingObject(Object anObject) { - NSArray result = new NSArray(this); + public NSArray<T> arrayByAddingObject(T anObject) { + NSArray<T> result = new NSArray<>(this); result.protectedAdd(anObject); return result; } @@ -150,8 +150,8 @@ public class NSArray implements List<Object>, Serializable { * Returns an array containing all objects in this array plus all objects in the * specified list. */ - public NSArray arrayByAddingObjectsFromArray(Collection aCollection) { - NSArray result = new NSArray(this); + public NSArray<T> arrayByAddingObjectsFromArray(Collection<T> aCollection) { + NSArray<T> result = new NSArray<>(this); result.protectedAddAll(aCollection); return result; } @@ -163,7 +163,7 @@ public class NSArray implements List<Object>, Serializable { */ public String componentsJoinedByString(String separator) { StringBuffer buf = new StringBuffer(); - Iterator it = list.iterator(); + Iterator<T> it = list.iterator(); if (it.hasNext()) { buf.append(it.next().toString()); } @@ -177,7 +177,7 @@ public class NSArray implements List<Object>, Serializable { /** * Returns whether an equivalent object is contained in this array. */ - public boolean containsObject(Object anObject) { + public boolean containsObject(T anObject) { return list.contains(anObject); } @@ -185,12 +185,12 @@ public class NSArray implements List<Object>, Serializable { * Returns the first object in this array that is equivalent to an object in the * specified list, or null if no objects are in common. */ - public Object firstObjectCommonWithArray(Collection aCollection) { + public T firstObjectCommonWithArray(Collection<T> aCollection) { if (aCollection == null) return null; - Object o; - Iterator it = list.iterator(); + T o; + Iterator<T> it = list.iterator(); while (it.hasNext()) { o = it.next(); if (aCollection.contains(o)) @@ -203,14 +203,14 @@ public class NSArray implements List<Object>, Serializable { * Returns whether the specified list contains elements equivalent to those in * this array in the same order. */ - public boolean isEqualToArray(List aList) { + public boolean isEqualToArray(List<T> aList) { return list.equals(aList); } /** * Returns the last object in this array, or null if the array is empty. */ - public Object lastObject() { + public T lastObject() { int i; if ((i = list.size()) == 0) return null; @@ -228,9 +228,9 @@ public class NSArray implements List<Object>, Serializable { * Returns an array comprised of only those elements whose indices fall within * the specified range. */ - public NSArray subarrayWithRange(NSRange aRange) { + public NSArray<T> subarrayWithRange(NSRange aRange) { // TODO: Test this logic. - NSArray result = new NSArray(); + NSArray<T> result = new NSArray<>(); if (aRange == null) return result; @@ -246,16 +246,18 @@ public class NSArray implements List<Object>, Serializable { /** * Returns an enumeration over the the elements of the array. */ - public Enumeration objectEnumerator() { + public Enumeration<T> objectEnumerator() { // TODO: Test this logic. - return new Enumeration() { - Iterator it = NSArray.this.iterator(); + return new Enumeration<>() { + Iterator<T> it = NSArray.this.iterator(); + @Override public boolean hasMoreElements() { return it.hasNext(); } - public Object nextElement() { + @Override + public T nextElement() { return it.next(); } }; @@ -264,11 +266,11 @@ public class NSArray implements List<Object>, Serializable { /** * Returns an enumeration over the elements of the array in reverse order. */ - public java.util.Enumeration reverseObjectEnumerator() { - return new java.util.Enumeration() { - ListIterator it = null; + public java.util.Enumeration<T> reverseObjectEnumerator() { + class ReverseArrayEnumerator implements Enumeration<T> { + ListIterator<T> it = null; - public ListIterator getIterator() { + ListIterator<T> getIterator() { if (it == null) { it = NSArray.this.listIterator(); // zoom to end @@ -278,21 +280,24 @@ public class NSArray implements List<Object>, Serializable { return it; } + @Override public boolean hasMoreElements() { return getIterator().hasPrevious(); } - public Object nextElement() { + @Override + public T nextElement() { return getIterator().previous(); } - }; + } + return new ReverseArrayEnumerator(); } /** * Copies the elements of this array into the specified object array as the * array's capacity permits. */ - public void getObjects(Object[] anArray) { + public void getObjects(T[] anArray) { getObjects(anArray, null); } @@ -310,7 +315,7 @@ public class NSArray implements List<Object>, Serializable { * @param range An NSRange object representing the range of data in the * NSArray to be copied. */ - public void getObjects(Object[] array, NSRange range) { + public void getObjects(T[] array, NSRange range) { if (array == null) return; if (range == null) @@ -326,7 +331,7 @@ public class NSArray implements List<Object>, Serializable { * Returns the index of the first object in the array equivalent to the * specified object. Returns NotFound if the item is not found. */ - public int indexOfObject(Object anObject) { + public int indexOfObject(T anObject) { int result = list.indexOf(anObject); if (result == -1) return NotFound; // in case this changes @@ -338,7 +343,7 @@ public class NSArray implements List<Object>, Serializable { * equivalent to the specified object. Returns NotFound if the item is not * found. */ - public int indexOfObject(Object anObject, NSRange aRange) { + public int indexOfObject(T anObject, NSRange aRange) { if ((anObject == null) || (aRange == null)) return NotFound; @@ -356,7 +361,7 @@ public class NSArray implements List<Object>, Serializable { * Returns the index of the specified object if it exists in the array, * comparing by reference. Returns NotFound if the item is not found. */ - public int indexOfIdenticalObject(Object anObject) { + public int indexOfIdenticalObject(T anObject) { int size = list.size(); for (int i = 0; i < size; i++) { if (anObject == list.get(i)) { @@ -370,7 +375,7 @@ public class NSArray implements List<Object>, Serializable { * Returns the index of the first object in the array within the specified range * equivalent to the specified object. */ - public int indexOfIdenticalObject(Object anObject, NSRange aRange) { + public int indexOfIdenticalObject(T anObject, NSRange aRange) { if (aRange == null) return NotFound; @@ -388,7 +393,7 @@ public class NSArray implements List<Object>, Serializable { * Returns the object at the specified index. Throws an IndexOutOfRange * exception if the index is out of range. */ - public Object objectAtIndex(int anIndex) { + public T objectAtIndex(int anIndex) { return list.get(anIndex); } @@ -396,12 +401,12 @@ public class NSArray implements List<Object>, Serializable { * Returns an array consisting of strings within the specified string as * delimited by the specified separator characters. */ - public static NSArray componentsSeparatedByString(String aString, String aSeparator) { - NSArray result = new NSArray(); + public static NSArray<String> componentsSeparatedByString(String aString, String aSeparator) { + NSArray<String> result = new NSArray<>(); if (aString == null) return result; if (aSeparator == null) - return new NSArray(aString); + return new NSArray<>(aString); // FIXME: The spec probably considers the whole // string as a separator, unlike string tokenizer. @@ -413,18 +418,20 @@ public class NSArray implements List<Object>, Serializable { return result; } + @Override public Object clone() { - return new NSArray(list); + return new NSArray<>(list); } - public NSArray immutableClone() { + public NSArray<T> immutableClone() { return this; } - public NSMutableArray mutableClone() { - return new NSMutableArray(this); + public NSMutableArray<T> mutableClone() { + return new NSMutableArray<>(this); } + @Override public String toString() { StringBuffer buf = new StringBuffer(); buf.append(NSPropertyListSerialization.TOKEN_BEGIN[NSPropertyListSerialization.PLIST_ARRAY]); @@ -440,22 +447,27 @@ public class NSArray implements List<Object>, Serializable { // interface List: accessors + @Override public boolean contains(Object o) { return list.contains(o); } - public boolean containsAll(Collection c) { + @Override + public boolean containsAll(Collection<?> c) { return list.containsAll(c); } + @Override public boolean equals(Object o) { return list.equals(o); } - public Object get(int index) { + @Override + public T get(int index) { return list.get(index); } + @Override public int hashCode() { int code = 19; code *= getClass().hashCode(); @@ -463,151 +475,184 @@ public class NSArray implements List<Object>, Serializable { return code; } + @Override public int indexOf(Object o) { return list.indexOf(o); } + @Override public boolean isEmpty() { return list.isEmpty(); } + @Override public int lastIndexOf(Object o) { return list.lastIndexOf(o); } + @Override public int size() { return list.size(); } + @Override public Object[] toArray() { return list.toArray(); } - public Object[] toArray(Object[] a) { - return list.toArray(a); + @SuppressWarnings("unchecked") + @Override + public T[] toArray(Object[] a) { + return (T[]) list.toArray(a); } // interface List: mutators - public void add(int index, Object element) { + @Override + public void add(int index, T element) { this.list.add(index, element); } - public boolean add(Object o) { + @Override + public boolean add(T o) { return this.list.add(o); } - public boolean addAll(Collection coll) { + @Override + public boolean addAll(Collection<? extends T> coll) { return this.list.addAll(coll); } - public boolean addAll(int index, Collection c) { + @Override + public boolean addAll(int index, Collection<? extends T> c) { return this.list.addAll(index, c); } + @Override public void clear() { this.list.clear(); } - public Iterator iterator() { + @Override + public Iterator<T> iterator() { // make a copy to avoid ConcurrentModificationExceptions - final Iterator i = new LinkedList(list).iterator(); - return new Iterator() { + final Iterator<T> i = new LinkedList<>(list).iterator(); + return new Iterator<>() { + @Override public boolean hasNext() { return i.hasNext(); } - public Object next() { + @Override + public T next() { return i.next(); } + @Override public void remove() { throw new UnsupportedOperationException(); } }; } - public ListIterator listIterator() { + @Override + public ListIterator<T> listIterator() { return listIterator(0); } - public ListIterator listIterator(final int index) { + @Override + public ListIterator<T> listIterator(final int index) { // make a copy to avoid ConcurrentModificationExceptions - final ListIterator i = new LinkedList(list).listIterator(index); - return new ListIterator() { + final ListIterator<T> i = new LinkedList<>(list).listIterator(index); + return new ListIterator<>() { + @Override public boolean hasNext() { return i.hasNext(); } - public Object next() { + @Override + public T next() { return i.next(); } + @Override public boolean hasPrevious() { return i.hasPrevious(); } - public Object previous() { + @Override + public T previous() { return i.previous(); } + @Override public int nextIndex() { return i.nextIndex(); } + @Override public int previousIndex() { return i.previousIndex(); } + @Override public void remove() { throw new UnsupportedOperationException(); } - public void set(Object o) { + @Override + public void set(T o) { throw new UnsupportedOperationException(); } - public void add(Object o) { + @Override + public void add(T o) { throw new UnsupportedOperationException(); } }; } - public Object remove(int index) { + @Override + public T remove(int index) { return this.list.remove(index); } + @Override public boolean remove(Object o) { return this.list.remove(o); } - public boolean removeAll(Collection coll) { + @Override + public boolean removeAll(Collection<?> coll) { return this.list.removeAll(coll); } - public boolean retainAll(Collection coll) { + @Override + public boolean retainAll(Collection<?> coll) { return this.list.retainAll(coll); } - public Object set(int index, Object element) { + @Override + public T set(int index, T element) { return this.list.set(index, element); } - public List subList(int fromIndex, int toIndex) { + @Override + public List<T> subList(int fromIndex, int toIndex) { return Collections.unmodifiableList(list.subList(fromIndex, toIndex)); } /** * Provided for the use of subclasses like ArrayFault. */ - protected boolean protectedAdd(Object o) { + protected boolean protectedAdd(T o) { return list.add(o); } /** * Provided for the use of subclasses like ArrayFault. */ - protected boolean protectedAddAll(Collection coll) { + protected boolean protectedAddAll(Collection<T> coll) { return list.addAll(coll); } } diff --git a/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSBundle.java b/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSBundle.java index 49dda88..97fd9b9 100644 --- a/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSBundle.java +++ b/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSBundle.java @@ -64,7 +64,7 @@ public class NSBundle { private static final NSMutableArray _allFrameworks = new NSMutableArray(); - private static NSMutableDictionary _languageCodes = new NSMutableDictionary(); + private static NSMutableDictionary<String, String> _languageCodes = new NSMutableDictionary<>(); private static NSBundle _mainBundle = null; diff --git a/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSCoder.java b/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSCoder.java index 8a1721a..b925bbf 100644 --- a/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSCoder.java +++ b/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSCoder.java @@ -58,7 +58,7 @@ public abstract class NSCoder { public abstract void encodeObject(Object obj); - public abstract void encodeClass(Class class1); + public abstract void encodeClass(Class<?> class1); public abstract void encodeObjects(Object aobj[]); @@ -82,7 +82,7 @@ public abstract class NSCoder { public abstract Object decodeObject(); - public abstract Class decodeClass(); + public abstract Class<?> decodeClass(); public abstract Object[] decodeObjects(); diff --git a/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSComparator.java b/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSComparator.java index 6c88d19..2ce3646 100644 --- a/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSComparator.java +++ b/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSComparator.java @@ -32,10 +32,11 @@ import java.util.Comparator; * @version $Revision: 913 $ */ -public abstract class NSComparator implements Comparator { - protected static class _NSSelectorComparator extends NSComparator { +public abstract class NSComparator<T> implements Comparator<T> { + protected static class _NSSelectorComparator extends NSComparator<NSSelector> { - public int compare(Object obj, Object obj1) throws ComparisonException { + @Override + public int compare(NSSelector obj, NSSelector obj1) throws ComparisonException { throw new UnsupportedOperationException("Not Yet Implemented"); } @@ -44,33 +45,36 @@ public abstract class NSComparator implements Comparator { } } - private static class StandInComparator extends NSComparator { + private static class StandInComparator<T> extends NSComparator<T> { public StandInComparator() { throw new UnsupportedOperationException("Not Yet Implemented"); } - public int compare(Object obj, Object obj1) throws ComparisonException { + @Override + public int compare(T obj, T obj1) throws ComparisonException { throw new UnsupportedOperationException("Not Yet Implemented"); } } public static class ComparisonException extends ClassCastException { + private static final long serialVersionUID = 8417668044508338674L; public ComparisonException(String s) { super(s); } } - public static final NSComparator AscendingStringComparator = new StandInComparator(); - public static final NSComparator DescendingStringComparator = new StandInComparator(); - public static final NSComparator AscendingCaseInsensitiveStringComparator = new StandInComparator(); - public static final NSComparator DescendingCaseInsensitiveStringComparator = new StandInComparator(); - public static final NSComparator AscendingNumberComparator = new StandInComparator(); - public static final NSComparator DescendingNumberComparator = new StandInComparator(); - public static final NSComparator AscendingTimestampComparator = new StandInComparator(); - public static final NSComparator DescendingTimestampComparator = new StandInComparator(); + public static final NSComparator<String> AscendingStringComparator = new StandInComparator<>(); + public static final NSComparator<String> DescendingStringComparator = new StandInComparator<>(); + public static final NSComparator<String> AscendingCaseInsensitiveStringComparator = new StandInComparator<>(); + public static final NSComparator<String> DescendingCaseInsensitiveStringComparator = new StandInComparator<>(); + public static final NSComparator<String> AscendingNumberComparator = new StandInComparator<>(); + public static final NSComparator<String> DescendingNumberComparator = new StandInComparator<>(); + public static final NSComparator<String> AscendingTimestampComparator = new StandInComparator<>(); + public static final NSComparator<String> DescendingTimestampComparator = new StandInComparator<>(); + public static final int OrderedAscending = -1; public static final int OrderedSame = 0; public static final int OrderedDescending = 1; @@ -78,9 +82,10 @@ public abstract class NSComparator implements Comparator { public NSComparator() { } - public abstract int compare(Object obj, Object obj1) throws ClassCastException; + @Override + public abstract int compare(T obj, T obj1) throws ClassCastException; - public static int _compareObjects(Comparable comparable, Comparable comparable1) { + public static <T> int _compareObjects(Comparable<T> comparable, Comparable<T> comparable1) { throw new UnsupportedOperationException("Not Yet Implemented"); } diff --git a/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSData.java b/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSData.java index 67f1d59..3120ad3 100644 --- a/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSData.java +++ b/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSData.java @@ -20,6 +20,7 @@ package net.wotonomy.foundation; import java.io.ByteArrayOutputStream; import java.io.File; +import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; @@ -75,8 +76,8 @@ public class NSData { public NSData(File aFile) { int len = (int) aFile.length(); byte[] data = new byte[len]; - try { - new java.io.FileInputStream(aFile).read(data); + try (FileInputStream fis = new java.io.FileInputStream(aFile)) { + fis.read(data); } catch (Exception exc) { // produce an empty or partially blank array } @@ -189,6 +190,7 @@ public class NSData { return bytes(0, length()); } + @Override public String toString() { String hex = "0123456789ABCDEF"; StringBuffer buf = new StringBuffer(); diff --git a/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSDate.java b/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSDate.java index a2121fd..e3ea753 100644 --- a/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSDate.java +++ b/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSDate.java @@ -31,8 +31,9 @@ import java.util.TimeZone; * @version $Revision: 892 $ */ public class NSDate extends Date { + private static final long serialVersionUID = -8755214209734414214L; + // NSComparisonResult compatibility - public static final int NSOrderedAscending = -1; public static final int NSOrderedSame = 0; public static final int NSOrderedDescending = 1; 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("; "); } diff --git a/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSForwardException.java b/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSForwardException.java index 10f54af..2fd8501 100644 --- a/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSForwardException.java +++ b/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSForwardException.java @@ -32,6 +32,7 @@ import java.io.StringWriter; */ public class NSForwardException extends RuntimeException { + private static final long serialVersionUID = -6960129453213771785L; protected String message; protected Throwable wrappedThrowable; @@ -78,6 +79,7 @@ public class NSForwardException extends RuntimeException { return wrappedThrowable; } + @Override public void printStackTrace(PrintWriter s) { if (message != null) { s.println(toString()); @@ -89,6 +91,7 @@ public class NSForwardException extends RuntimeException { super.printStackTrace(s); } + @Override public void printStackTrace(PrintStream s) { if (message != null) { s.println(toString()); @@ -100,6 +103,7 @@ public class NSForwardException extends RuntimeException { super.printStackTrace(s); } + @Override public void printStackTrace() { if (message != null) { System.err.println(toString()); @@ -124,6 +128,7 @@ public class NSForwardException extends RuntimeException { return writer.toString(); } + @Override public String toString() { String result = message; if (result == null) diff --git a/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSKeyValueCoding.java b/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSKeyValueCoding.java index 9eece7e..6f7da5f 100644 --- a/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSKeyValueCoding.java +++ b/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSKeyValueCoding.java @@ -36,7 +36,7 @@ import net.wotonomy.foundation.internal.WotonomyException; * * NSKeyValueCodingSupport implements the default behaviors for each of these * methods, so classes implementing this interface can call those methods to - * acheive the same behavior. <br> + * achieve the same behavior. <br> * <br> * * valueForKey and takeValueForKey are called in response to user actions, like @@ -318,6 +318,7 @@ public interface NSKeyValueCoding { super(); } + @Override public String toString() { return "<null>"; } diff --git a/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSKeyValueCodingAdditions.java b/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSKeyValueCodingAdditions.java index 39428ad..72c9fc8 100644 --- a/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSKeyValueCodingAdditions.java +++ b/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSKeyValueCodingAdditions.java @@ -49,13 +49,13 @@ public interface NSKeyValueCodingAdditions extends NSKeyValueCoding { * Returns a Map of the specified keys to their values, each of which might be * obtained by calling valueForKey. */ - NSDictionary valuesForKeys(List aKeyList); + NSDictionary<String, Object> valuesForKeys(List<String> aKeyList); /** * Takes the keys from the specified map as properties and applies the * corresponding values, each of which might be set by calling takeValueForKey. */ - void takeValuesFromDictionary(Map aMap); + void takeValuesFromDictionary(Map<String, Object> aMap); /** * Static utility methods that call the appropriate method if the object @@ -68,7 +68,7 @@ public interface NSKeyValueCodingAdditions extends NSKeyValueCoding { * NSKeyValueCodingAdditions, otherwise calls the method on * DefaultImplementation. */ - public static void takeValuesFromDictionary(Object object, Map dictionary) { + public static void takeValuesFromDictionary(Object object, Map<String, Object> dictionary) { if (object instanceof NSKeyValueCodingAdditions) { ((NSKeyValueCodingAdditions) object).takeValuesFromDictionary(dictionary); } else { @@ -94,7 +94,7 @@ public interface NSKeyValueCodingAdditions extends NSKeyValueCoding { * NSKeyValueCodingAdditions, otherwise calls the method on * DefaultImplementation. */ - public static NSDictionary valuesForKeys(Object object, List keys) { + public static NSDictionary<String, Object> valuesForKeys(Object object, List<String> keys) { if (object instanceof NSKeyValueCodingAdditions) { return ((NSKeyValueCodingAdditions) object).valuesForKeys(keys); } else { @@ -125,7 +125,7 @@ public interface NSKeyValueCodingAdditions extends NSKeyValueCoding { * Provides a reflection-based implementation for classes that don't implement * NSKeyValueCodingAdditions. */ - public static void takeValuesFromDictionary(Object object, Map dictionary) { + public static void takeValuesFromDictionary(Object object, Map<String, Object> dictionary) { throw new RuntimeException("Not implemented yet."); } @@ -142,7 +142,7 @@ public interface NSKeyValueCodingAdditions extends NSKeyValueCoding { * Provides a reflection-based implementation for classes that don't implement * NSKeyValueCodingAdditions. */ - public static NSDictionary valuesForKeys(Object object, List keys) { + public static NSDictionary<String, Object> valuesForKeys(Object object, List<String> keys) { throw new RuntimeException("Not implemented yet."); } diff --git a/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSLock.java b/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSLock.java index 45afdde..c9287eb 100644 --- a/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSLock.java +++ b/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSLock.java @@ -41,6 +41,7 @@ public class NSLock extends Mutex implements NSLocking { public NSLock() { } + @Override public synchronized void lock() { try { acquire(); @@ -49,6 +50,7 @@ public class NSLock extends Mutex implements NSLocking { } } + @Override public synchronized void unlock() { release(); } @@ -70,6 +72,7 @@ public class NSLock extends Mutex implements NSLocking { return tryLock(nstimestamp.getTime() - System.currentTimeMillis()); } + @Override public String toString() { return getClass().getName() + " <" + (inuse_ ? "Locked" : "Unlocked") + ">"; } diff --git a/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSLog.java b/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSLog.java index b447bd3..eba1383 100644 --- a/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSLog.java +++ b/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSLog.java @@ -222,14 +222,14 @@ public class NSLog { * Convenience to append a Boolean. */ public void appendln(boolean aValue) { - appendln(new Boolean(aValue)); + appendln(Boolean.valueOf(aValue)); } /** * Convenience to append a Byte. */ public void appendln(byte aValue) { - appendln(new Byte(aValue)); + appendln(Byte.valueOf(aValue)); } /** @@ -244,7 +244,7 @@ public class NSLog { * Convenience to append a Character. */ public void appendln(char aValue) { - appendln(new Character(aValue)); + appendln(Character.valueOf(aValue)); } /** @@ -258,35 +258,35 @@ public class NSLog { * Convenience to append a Double. */ public void appendln(double aValue) { - appendln(new Double(aValue)); + appendln(Double.valueOf(aValue)); } /** * Convenience to append a Float. */ public void appendln(float aValue) { - appendln(new Float(aValue)); + appendln(Float.valueOf(aValue)); } /** * Convenience to append a Integer. */ public void appendln(int aValue) { - appendln(new Integer(aValue)); + appendln(Integer.valueOf(aValue)); } /** * Convenience to append a Long. */ public void appendln(long aValue) { - appendln(new Long(aValue)); + appendln(Long.valueOf(aValue)); } /** * Convenience to append a Short */ public void appendln(short aValue) { - appendln(new Short(aValue)); + appendln(Short.valueOf(aValue)); } /** @@ -362,6 +362,7 @@ public class NSLog { /** * Sends a newline to the print stream. */ + @Override public void appendln() { if (isEnabled()) { thePrintStream.println(); @@ -371,6 +372,7 @@ public class NSLog { /** * Writes the throwable to the print stream. */ + @Override public void appendln(Throwable aValue) { appendln(NSLog.throwableAsString(aValue)); } @@ -378,6 +380,7 @@ public class NSLog { /** * Writes aValue.toString to the print stream. */ + @Override public void appendln(Object aValue) { if (isEnabled()) { if (isVerbose()) { @@ -392,6 +395,7 @@ public class NSLog { /** * Flushes the print stream. */ + @Override public void flush() { thePrintStream.flush(); } diff --git a/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSMultiReaderLock.java b/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSMultiReaderLock.java index 3492141..fe296db 100644 --- a/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSMultiReaderLock.java +++ b/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSMultiReaderLock.java @@ -39,7 +39,7 @@ import EDU.oswego.cs.dl.util.concurrent.ReentrantWriterPreferenceReadWriteLock; */ public class NSMultiReaderLock extends ReentrantWriterPreferenceReadWriteLock implements NSLocking { - NSMutableDictionary _readerSuspended = new NSMutableDictionary(); + NSMutableDictionary<Thread, Integer> _readerSuspended = new NSMutableDictionary<>(); public NSMultiReaderLock() { } @@ -58,6 +58,7 @@ public class NSMultiReaderLock extends ReentrantWriterPreferenceReadWriteLock im readerLock_.release(); } + @Override public void lock() { lockForWriting(); } @@ -72,6 +73,7 @@ public class NSMultiReaderLock extends ReentrantWriterPreferenceReadWriteLock im } } + @Override public void unlock() { unlockForWriting(); } @@ -122,6 +124,7 @@ public class NSMultiReaderLock extends ReentrantWriterPreferenceReadWriteLock im } } + @Override public String toString() { throw new UnsupportedOperationException("Not Yet Implemented"); } diff --git a/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSMutableArray.java b/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSMutableArray.java index c75de01..39f9fb1 100644 --- a/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSMutableArray.java +++ b/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSMutableArray.java @@ -30,17 +30,19 @@ import java.util.ListIterator; * @author $Author: cgruber $ * @version $Revision: 893 $ */ -public class NSMutableArray extends NSArray { +public class NSMutableArray<T> extends NSArray<T> { + private static final long serialVersionUID = 2414783500843649556L; + /** * Returns an NSArray backed by the specified List. This is useful to "protect" * an internal representation that is returned by a method of return type * NSArray. */ - public static NSMutableArray mutableArrayBackedByList(List aList) { - return new NSMutableArray(aList, null); + public static <T> NSMutableArray<T> mutableArrayBackedByList(List<T> aList) { + return new NSMutableArray<>(aList, null); } - NSMutableArray(List aList, Object ignored) // differentiates + NSMutableArray(List<T> aList, Object ignored) // differentiates { super(aList, ignored); } @@ -64,7 +66,7 @@ public class NSMutableArray extends NSArray { /** * Produces an array containing only the specified object. */ - public NSMutableArray(Object anObject) { + public NSMutableArray(T anObject) { super(anObject); //System.out.println( "NSMutableArray: " + net.wotonomy.ui.swing.util.StackTraceInspector.getMyCaller() ); } @@ -72,7 +74,7 @@ public class NSMutableArray extends NSArray { /** * Produces an array containing the specified objects. */ - public NSMutableArray(Object[] anArray) { + public NSMutableArray(T[] anArray) { super(anArray); //System.out.println( "NSMutableArray: " + net.wotonomy.ui.swing.util.StackTraceInspector.getMyCaller() ); } @@ -80,7 +82,7 @@ public class NSMutableArray extends NSArray { /** * Produces an array containing the objects in the specified collection. */ - public NSMutableArray(Collection aCollection) { + public NSMutableArray(Collection<T> aCollection) { super(aCollection); //System.out.println( "NSMutableArray: " + net.wotonomy.ui.swing.util.StackTraceInspector.getMyCaller() ); } @@ -102,7 +104,7 @@ public class NSMutableArray extends NSArray { /** * Adds all objects in the specified collection. */ - public void addObjectsFromArray(Collection aCollection) { + public void addObjectsFromArray(Collection<T> aCollection) { list.addAll(aCollection); } @@ -117,7 +119,7 @@ public class NSMutableArray extends NSArray { * Removes all objects equivalent to the specified object within the range of * specified indices. */ - public void removeObject(Object anObject, NSRange aRange) { + public void removeObject(T anObject, NSRange aRange) { if ((anObject == null) || (aRange == null)) return; @@ -136,7 +138,7 @@ public class NSMutableArray extends NSArray { * Removes all instances of the specified object within the range of specified * indices, comparing by reference. */ - public void removeIdenticalObject(Object anObject, NSRange aRange) { + public void removeIdenticalObject(T anObject, NSRange aRange) { if ((anObject == null) || (aRange == null)) return; @@ -154,7 +156,7 @@ public class NSMutableArray extends NSArray { /** * Removes all objects in the specified collection from the array. */ - public void removeObjectsInArray(Collection aCollection) { + public void removeObjectsInArray(Collection<T> aCollection) { list.removeAll(aCollection); } @@ -176,7 +178,7 @@ public class NSMutableArray extends NSArray { * objects are removed. If otherRange is larger than currentRange, the extra * objects are added. */ - public void replaceObjectsInRange(NSRange currentRange, List otherArray, NSRange otherRange) { + public void replaceObjectsInRange(NSRange currentRange, List<T> otherArray, NSRange otherRange) { if ((currentRange == null) || (otherArray == null) || (otherRange == null)) return; @@ -187,8 +189,7 @@ public class NSMutableArray extends NSArray { otherRange = new NSRange(loc, otherArray.size() - loc); } - Object o; - List subList = list.subList(currentRange.location(), currentRange.maxRange()); + List<T> subList = list.subList(currentRange.location(), currentRange.maxRange()); int otherIndex = otherRange.location(); // TODO: Test this logic. for (int i = 0; i < subList.size(); i++) { @@ -210,7 +211,7 @@ public class NSMutableArray extends NSArray { * Clears the current array and then populates it with the contents of the * specified collection. */ - public void setArray(Collection aCollection) { + public void setArray(Collection<T> aCollection) { list.clear(); list.addAll(aCollection); } @@ -233,8 +234,8 @@ public class NSMutableArray extends NSArray { /** * Removes all occurences of the specified object, comparing by reference. */ - public void removeIdenticalObject(Object anObject) { - Iterator it = list.iterator(); + public void removeIdenticalObject(T anObject) { + Iterator<T> it = list.iterator(); while (it.hasNext()) { if (it.next() == anObject) { it.remove(); @@ -245,91 +246,108 @@ public class NSMutableArray extends NSArray { /** * Inserts the specified object into this array at the specified index. */ - public void insertObjectAtIndex(Object anObject, int anIndex) { + public void insertObjectAtIndex(T anObject, int anIndex) { list.add(anIndex, anObject); } /** * Replaces the object at the specified index with the specified object. */ - public void replaceObjectAtIndex(int anIndex, Object anObject) { + public void replaceObjectAtIndex(int anIndex, T anObject) { list.set(anIndex, anObject); } /** * Adds the specified object to the end of this array. */ - public void addObject(Object anObject) { + public void addObject(T anObject) { list.add(anObject); } + @Override public Object clone() { - return new NSMutableArray(list); + return new NSMutableArray<>(list); } - public NSArray immutableClone() { - return new NSArray(this); + @Override + public NSArray<T> immutableClone() { + return new NSArray<>(this); } - public NSMutableArray mutableClone() { - return new NSMutableArray(this); + @Override + public NSMutableArray<T> mutableClone() { + return new NSMutableArray<>(this); } // interface List: mutators - public void add(int index, Object element) { + @Override + public void add(int index, T element) { list.add(index, element); } - public boolean add(Object o) { + @Override + public boolean add(T o) { return list.add(o); } - public boolean addAll(Collection coll) { + @Override + public boolean addAll(Collection<? extends T> coll) { return list.addAll(coll); } - public boolean addAll(int index, Collection c) { + @Override + public boolean addAll(int index, Collection<? extends T> c) { return list.addAll(index, c); } + @Override public void clear() { list.clear(); } - public Iterator iterator() { + @Override + public Iterator<T> iterator() { return list.iterator(); } - public ListIterator listIterator() { + @Override + public ListIterator<T> listIterator() { return list.listIterator(); } - public ListIterator listIterator(int index) { - return list.listIterator(); + @Override + public ListIterator<T> listIterator(int index) { + return list.listIterator(index); } - public Object remove(int index) { + @Override + public T remove(int index) { return list.remove(index); } + @Override public boolean remove(Object o) { return list.remove(o); } - public boolean removeAll(Collection coll) { + @Override + public boolean removeAll(Collection<?> coll) { return list.removeAll(coll); } - public boolean retainAll(Collection coll) { + @Override + public boolean retainAll(Collection<?> coll) { return list.retainAll(coll); } - public Object set(int index, Object element) { + @Override + public T set(int index, T element) { return list.set(index, element); } - public List subList(int fromIndex, int toIndex) { + @Override + public List<T> subList(int fromIndex, int toIndex) { return list.subList(fromIndex, toIndex); } diff --git a/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSMutableDictionary.java b/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSMutableDictionary.java index ba58189..99c5230 100644 --- a/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSMutableDictionary.java +++ b/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSMutableDictionary.java @@ -29,7 +29,9 @@ import java.util.Map; * @author $Author: cgruber $ * @version $Revision: 893 $ */ -public class NSMutableDictionary extends NSDictionary { +public class NSMutableDictionary<K, V> extends NSDictionary<K, V> { + private static final long serialVersionUID = -150787274911739812L; + /** * Default constructor produces an empty dictionary. */ @@ -47,7 +49,7 @@ public class NSMutableDictionary extends NSDictionary { /** * Produces a dictionary that contains one key referencing one value. */ - public NSMutableDictionary(Object key, Object value) { + public NSMutableDictionary(K key, V value) { super(key, value); } @@ -55,21 +57,21 @@ public class NSMutableDictionary extends NSDictionary { * Produces a dictionary containing the specified keys and values. An * IllegalArgumentException is thrown if the arrays are not of the same length. */ - public NSMutableDictionary(Object[] keys, Object[] values) { - super(keys, values); + public NSMutableDictionary(K[] keys, V[] values) { + super(values, keys); } /** * Produces a dictionary that is a copy of the specified map (or dictionary). */ - public NSMutableDictionary(Map aMap) { + public NSMutableDictionary(Map<K, V> aMap) { super(aMap); } /** * Removes the key-value pair for the specified key. */ - public void removeObjectForKey(Object aKey) { + public void removeObjectForKey(K aKey) { remove(aKey); } @@ -77,7 +79,7 @@ public class NSMutableDictionary extends NSDictionary { * Copies all mappings from the specified dictionary to this dictionary, * replacing any mappings this map had for any keys in the specified map. */ - public void addEntriesFromDictionary(Map aMap) { + public void addEntriesFromDictionary(Map<K, V> aMap) { putAll(aMap); } @@ -91,8 +93,8 @@ public class NSMutableDictionary extends NSDictionary { /** * Removes all keys in the specified array from this dictionary. */ - public void removeObjectsForKeys(NSArray anArray) { - Enumeration enumeration = anArray.objectEnumerator(); + public void removeObjectsForKeys(NSArray<K> anArray) { + Enumeration<K> enumeration = anArray.objectEnumerator(); while (enumeration.hasMoreElements()) { removeObjectForKey(enumeration.nextElement()); } @@ -102,7 +104,7 @@ public class NSMutableDictionary extends NSDictionary { * Clears all mappings in this dictionary and then adds all entries in the * specified dictionary. */ - public void setDictionary(Map aMap) { + public void setDictionary(Map<K, V> aMap) { removeAllObjects(); addEntriesFromDictionary(aMap); } @@ -112,7 +114,7 @@ public class NSMutableDictionary extends NSDictionary { * dictionary, the old value is replaced with the specified value. An * IllegalArgumentException is thrown if either the key or value is null. */ - public void setObjectForKey(Object aValue, Object aKey) { + public void setObjectForKey(V aValue, K aKey) { if ((aKey == null) || (aValue == null)) { throw new IllegalArgumentException("Cannot use null objects with an NSMutableDictionary."); } diff --git a/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSNotification.java b/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSNotification.java index 6b091e2..4fbd8af 100644 --- a/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSNotification.java +++ b/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSNotification.java @@ -87,7 +87,7 @@ public class NSNotification { public NSDictionary userInfo() { if (userInfo == null) return null; - return new NSDictionary(userInfo); + return new NSDictionary<>(userInfo); } /** diff --git a/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSNotificationCenter.java b/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSNotificationCenter.java index cf0af53..862d454 100644 --- a/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSNotificationCenter.java +++ b/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSNotificationCenter.java @@ -60,13 +60,13 @@ public class NSNotificationCenter { /** * A Map of (name,object) pairs to a List of (observer,selector) pairs. */ - private Hashtable observers; // thread-safe + private Hashtable<CompoundKey, List<CompoundValue>> observers; // thread-safe /** * Default constructor creates a new notification center. */ public NSNotificationCenter() { - observers = new Hashtable(); + observers = new Hashtable<>(); } /** @@ -81,7 +81,7 @@ public class NSNotificationCenter { } /** - * Addes the specified observer to the notification queue for notifications with + * Adds the specified observer to the notification queue for notifications with * the specified name or the specified object or both. * * @param anObserver The observer that wishes to be notified. @@ -105,12 +105,12 @@ public class NSNotificationCenter { if (anObject == null) { anObject = NullMarker; } - Object key = new CompoundKey(name, anObject); - Object value = new CompoundValue(anObserver, aSelector); - List list = (List) observers.get(key); + CompoundKey key = new CompoundKey(name, anObject); + CompoundValue value = new CompoundValue(anObserver, aSelector); + List<CompoundValue> list = observers.get(key); if (list == null) { // create new list with value and put it in map - list = new Vector(); // thread-safe + list = new Vector<>(); // thread-safe list.add(value); observers.put(new CompoundKey(name, anObject, keyQueue), list); } else { @@ -129,8 +129,9 @@ public class NSNotificationCenter { * selector. */ public void postNotification(NSNotification aNotification) { - List mergedList = new LinkedList(); - Object key, observerList; + List<CompoundValue> mergedList = new LinkedList<>(); + CompoundKey key; + List<CompoundValue> observerList; Object name = aNotification.name(); Object object = aNotification.object(); @@ -139,39 +140,39 @@ public class NSNotificationCenter { if (object != null) { // both are specified observerList = observers.get(new CompoundKey(name, object)); if (observerList != null) { - mergedList.addAll((List) observerList); + mergedList.addAll(observerList); } observerList = observers.get(new CompoundKey(name, NullMarker)); if (observerList != null) { - mergedList.addAll((List) observerList); + mergedList.addAll(observerList); } observerList = observers.get(new CompoundKey(NullMarker, object)); if (observerList != null) { - mergedList.addAll((List) observerList); + mergedList.addAll(observerList); } } else { // object is null observerList = observers.get(new CompoundKey(name, NullMarker)); if (observerList != null) { - mergedList.addAll((List) observerList); + mergedList.addAll(observerList); } } } else if (object != null) { // name is null observerList = observers.get(new CompoundKey(NullMarker, object)); if (observerList != null) { - mergedList.addAll((List) observerList); + mergedList.addAll(observerList); } } key = new CompoundKey(NullMarker, NullMarker); observerList = observers.get(key); if (observerList != null) { - mergedList.addAll((List) observerList); + mergedList.addAll(observerList); } CompoundValue value; - Iterator it = mergedList.iterator(); + Iterator<CompoundValue> it = mergedList.iterator(); while (it.hasNext()) { - value = (CompoundValue) it.next(); + value = it.next(); if (value.get() == null) { it.remove(); } else { @@ -182,7 +183,7 @@ public class NSNotificationCenter { "Error notifying object: " + value.get() + " : " + aNotification, exc); // throw w; w.printStackTrace(); - postNotification("Error notifying object", this, new NSDictionary("exception", w)); + postNotification("Error notifying object", this, new NSDictionary<>("exception", w)); } } } @@ -212,7 +213,7 @@ public class NSNotificationCenter { * originator of the notification and that may be of * interest to a knowledgable observer. */ - public void postNotification(String notificationName, Object anObject, Map userInfo) { + public void postNotification(String notificationName, Object anObject, Map<String, Object> userInfo) { postNotification(new NSNotification(notificationName, anObject, userInfo)); } @@ -226,7 +227,7 @@ public class NSNotificationCenter { // remove freed objects processKeyQueue(); - Iterator it = new LinkedList(observers.keySet()).iterator(); + Iterator<CompoundKey> it = new LinkedList<>(observers.keySet()).iterator(); while (it.hasNext()) { removeObserver(anObserver, it.next()); } @@ -253,10 +254,10 @@ public class NSNotificationCenter { processKeyQueue(); // get key matches - List keys = matchingKeys(notificationName, anObject); + List<CompoundKey> keys = matchingKeys(notificationName, anObject); // remove specified observer from each matching key - Iterator it = keys.iterator(); + Iterator<CompoundKey> it = keys.iterator(); while (it.hasNext()) { removeObserver(anObserver, it.next()); } @@ -267,12 +268,12 @@ public class NSNotificationCenter { * null parameters are considered wildcards. Pass NullMarkers if you want to * explicitly match nulls. */ - private List matchingKeys(String name, Object object) { - List result = new LinkedList(); + private List<CompoundKey> matchingKeys(String name, Object object) { + List<CompoundKey> result = new LinkedList<>(); boolean willAdd; CompoundKey key; - Iterator it = observers.keySet().iterator(); + Iterator<CompoundKey> it = observers.keySet().iterator(); while (it.hasNext()) { key = (CompoundKey) it.next(); willAdd = false; @@ -299,15 +300,15 @@ public class NSNotificationCenter { return; } - List list = (List) observers.get(key); + List<CompoundValue> list = observers.get(key); if (list == null) return; // remove specified observer from list Object observer; - Iterator it = list.iterator(); + Iterator<CompoundValue> it = list.iterator(); while (it.hasNext()) { - observer = ((CompoundValue) it.next()).get(); + observer = it.next().get(); if ((observer == null) || (anObserver == observer)) { // remove if match or freed object it.remove(); @@ -321,7 +322,7 @@ public class NSNotificationCenter { } /* Reference queues for cleared WeakKeys */ - private ReferenceQueue keyQueue = new ReferenceQueue(); + private ReferenceQueue keyQueue = new ReferenceQueue<>(); /** * Removes any keys whose object has been garbage collected. (Garbage collected @@ -339,7 +340,7 @@ public class NSNotificationCenter { * Key combining a name with an object. The object is weakly referenced, and * keys are deallocated by reference queue. equals() compares by reference. */ - private static class CompoundKey extends WeakReference { + private static class CompoundKey extends WeakReference<Object> { private Object name; private int hashCode; @@ -357,6 +358,7 @@ public class NSNotificationCenter { * Creates compound key with queue. Neither name nor object may be null. Use * NullMarker to represent null in either name or object. */ + @SuppressWarnings({ "rawtypes", "unchecked" }) public CompoundKey(Object aName, Object anObject, ReferenceQueue aQueue) { super(anObject, aQueue); name = aName; @@ -367,10 +369,12 @@ public class NSNotificationCenter { return name; } + @Override public int hashCode() { return hashCode; } + @Override public boolean equals(Object anObject) { if (this == anObject) return true; @@ -388,6 +392,7 @@ public class NSNotificationCenter { return false; } + @Override public String toString() { return "[CompoundKey:" + name() + ":" + get() + "]"; } @@ -397,7 +402,7 @@ public class NSNotificationCenter { * Value combining an object with a selector. The object is weakly referenced, * and null values are not allowed. */ - private static class CompoundValue extends WeakReference { + private static class CompoundValue extends WeakReference<Object> { private NSSelector selector; private int hashCode; @@ -411,10 +416,12 @@ public class NSNotificationCenter { return selector; } + @Override public int hashCode() { return hashCode; } + @Override public boolean equals(Object anObject) { if (this == anObject) return true; @@ -431,6 +438,7 @@ public class NSNotificationCenter { return false; } + @Override public String toString() { return "[CompoundValue:" + get() + ":" + selector().name() + "]"; } diff --git a/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSNull.java b/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSNull.java index 861f576..5d08fb2 100644 --- a/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSNull.java +++ b/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSNull.java @@ -36,6 +36,7 @@ import java.io.Serializable; * @version $Revision: 892 $ */ public class NSNull implements Serializable { + private static final long serialVersionUID = 6516669267906970248L; private static final NSNull instance = new NSNull(); /** @@ -56,13 +57,15 @@ public class NSNull implements Serializable { /** * Returns a human-readable string representation. */ + @Override public String toString() { return "[null]"; } /** - * Hashcode of all instances is zero. + * Hash code of all instances is zero. */ + @Override public int hashCode() { return 0; } @@ -70,6 +73,7 @@ public class NSNull implements Serializable { /** * Implemented to return true for any instance of NSNull. */ + @Override public boolean equals(Object anObject) { return (anObject instanceof NSNull); } diff --git a/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSRange.java b/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSRange.java index 13dca2f..5c54a08 100644 --- a/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSRange.java +++ b/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSRange.java @@ -110,6 +110,7 @@ public class NSRange implements Cloneable { /** * Returns whether the specified object is equal to this range. */ + @Override public boolean equals(Object anObject) { if (anObject instanceof NSRange) return isEqualToRange((NSRange) anObject); @@ -119,6 +120,7 @@ public class NSRange implements Cloneable { /** * Returns a hashCode. */ + @Override public int hashCode() { // TODO: Test this logic. return (location() << 2) & length(); // bitwise ops never my forte @@ -127,6 +129,7 @@ public class NSRange implements Cloneable { /** * Returns a string representation of this range. */ + @Override public String toString() { return "[NSRange: location = " + location() + "; length = " + length() + "]"; } @@ -264,6 +267,7 @@ public class NSRange implements Cloneable { /** * Returns a copy of this range. */ + @Override public Object clone() { return new NSRange(location(), length()); } diff --git a/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSRunLoop.java b/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSRunLoop.java index c72cd23..2889aef 100644 --- a/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSRunLoop.java +++ b/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSRunLoop.java @@ -40,7 +40,7 @@ import java.util.ListIterator; * not get their own run loop as in OpenStep.<br> * <br> * - * That said, this event queue is servicable as a replacement for the default + * That said, this event queue is serviceable as a replacement for the default * event queue and will provide prioritized execution of selectors before and * after normal AWT events. <br> * <br> diff --git a/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSSet.java b/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSSet.java index 38a7ec6..ece9e1b 100644 --- a/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSSet.java +++ b/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSSet.java @@ -33,7 +33,9 @@ import java.util.Vector; * @author $Author: cgruber $ * @version $Revision: 893 $ */ -public class NSSet extends HashSet { +public class NSSet<T> extends HashSet<T> { + private static final long serialVersionUID = 4211571026717957124L; + /** * Default constructor. */ @@ -44,14 +46,14 @@ public class NSSet extends HashSet { /** * Constructs a NSSet containing the objects in the specified collection. */ - public NSSet(Collection aCollection) { + public NSSet(Collection<T> aCollection) { super(aCollection); } /** * Constructs a NSSet containing only the specified object. */ - public NSSet(Object anObject) { + public NSSet(T anObject) { super(); add(anObject); } @@ -59,7 +61,7 @@ public class NSSet extends HashSet { /** * Constructs a NSSet containing the objects in the specified array. */ - public NSSet(Object[] anObjectArray) { + public NSSet(T[] anObjectArray) { super(); for (int i = 0; i < anObjectArray.length; i++) { add(anObjectArray[i]); @@ -69,14 +71,16 @@ public class NSSet extends HashSet { /** * Returns an NSArray containing all objects in the set. */ - public NSArray allObjects() { - return new NSArray(this); + public NSArray<T> allObjects() { + return new NSArray<>(this); } /** + * Retrieve an arbitrary object from the set. * + * @return An arbitrary object from the set */ - public Object anyObject() { + public T anyObject() { throw new RuntimeException("Not implemented yet."); } @@ -98,8 +102,8 @@ public class NSSet extends HashSet { * Returns whether this set has one or more elements in common with the * specified set. */ - public boolean intersectsSet(Set aSet) { - Iterator it = aSet.iterator(); + public boolean intersectsSet(Set<T> aSet) { + Iterator<T> it = aSet.iterator(); while (it.hasNext()) { if (this.containsObject(it.next())) { return true; @@ -112,14 +116,14 @@ public class NSSet extends HashSet { /** * Returns whether this set contains the same object as the specified set. */ - public boolean isEqualToSet(Set aSet) { + public boolean isEqualToSet(Set<T> aSet) { return equals(aSet); } /** * Returns whether this set is a subset of the specified set. */ - public boolean isSubsetOfSet(Set aSet) { + public boolean isSubsetOfSet(Set<T> aSet) { return aSet.containsAll(this); } @@ -133,15 +137,15 @@ public class NSSet extends HashSet { /** * Returns an enumerator over the objects in this set. */ - public Enumeration objectEnumerator() { - return new Vector(this).elements(); + public Enumeration<T> objectEnumerator() { + return new Vector<>(this).elements(); } /** * Returns a set that is the intersection of this set and the specified set. */ - public NSSet setByIntersectingSet(Set aSet) { - NSSet result = new NSSet(this); + public NSSet<T> setByIntersectingSet(Set<T> aSet) { + NSSet<T> result = new NSSet<>(this); result.retainAll(aSet); return result; } @@ -150,8 +154,8 @@ public class NSSet extends HashSet { * Returns a set that contains all elements in this set that are not in the * specified set. */ - public NSSet setBySubtractingSet(Set aSet) { - NSSet result = new NSSet(this); + public NSSet<T> setBySubtractingSet(Set<T> aSet) { + NSSet<T> result = new NSSet<>(this); result.removeAll(aSet); return result; } @@ -159,8 +163,8 @@ public class NSSet extends HashSet { /** * Returns a set that is the union of this set and the specified set. */ - public NSSet setByUnioningSet(Set aSet) { - NSSet result = new NSSet(this); + public NSSet<T> setByUnioningSet(Set<T> aSet) { + NSSet<T> result = new NSSet<>(this); result.addAll(aSet); return result; } diff --git a/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/internal/Duplicator.java b/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/internal/Duplicator.java index 16ef393..8ecb7a3 100644 --- a/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/internal/Duplicator.java +++ b/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/internal/Duplicator.java @@ -19,8 +19,6 @@ License along with this library; if not, see http://www.gnu.org package net.wotonomy.foundation.internal; import net.wotonomy.foundation.*; -import net.wotonomy.foundation.internal.Introspector; -import net.wotonomy.foundation.internal.WotonomyException; import java.io.*; import java.util.*; //collections @@ -49,14 +47,14 @@ public class Duplicator { * Returns a list of properties for the specified class that are both readable * and writable. */ - static public List editablePropertiesForObject(Object anObject) { - List readProperties = new ArrayList(); + static public List<String> editablePropertiesForObject(Object anObject) { + List<String> readProperties = new ArrayList<>(); String[] read = Introspector.getReadPropertiesForObject(anObject); for (int i = 0; i < read.length; i++) { readProperties.add(read[i]); } - List properties = new ArrayList(); + List<String> properties = new ArrayList<>(); String[] write = Introspector.getWritePropertiesForObject(anObject); for (int i = 0; i < write.length; i++) { properties.add(write[i]); @@ -73,12 +71,12 @@ public class Duplicator { * and their values. Any null values for properties will be represented with the * NULL object. */ - static public Map readPropertiesForObject(Object anObject) { - NSMutableDictionary result = new NSMutableDictionary(); + static public Map<String, Object> readPropertiesForObject(Object anObject) { + NSMutableDictionary<String, Object> result = new NSMutableDictionary<>(); String key; Object value; - Iterator it = editablePropertiesForObject(anObject).iterator(); + Iterator<String> it = editablePropertiesForObject(anObject).iterator(); while (it.hasNext()) { key = it.next().toString(); value = Introspector.get(anObject, key); @@ -93,10 +91,12 @@ public class Duplicator { * Returns a Map containing only the mutable properties for the specified object * and deep clones of their values. Nulls are represented by the NULL object. */ - static public Map clonePropertiesForObject(Object anObject) { - Object key, value; - Map result = readPropertiesForObject(anObject); - Iterator it = result.keySet().iterator(); + static public Map<String, Object> clonePropertiesForObject(Object anObject) { + String key; + Object value; + + Map<String, Object> result = readPropertiesForObject(anObject); + Iterator<String> it = result.keySet().iterator(); while (it.hasNext()) { key = it.next(); value = result.get(key); @@ -110,10 +110,10 @@ public class Duplicator { * Applies the map of properties and values to the specified object. Null values * for properties must be represented by the NULL object. */ - static public void writePropertiesForObject(Map aMap, Object anObject) { + static public void writePropertiesForObject(Map<String, Object> aMap, Object anObject) { String key; Object value; - Iterator it = aMap.keySet().iterator(); + Iterator<String> it = aMap.keySet().iterator(); while (it.hasNext()) { key = it.next().toString(); value = aMap.get(key); @@ -141,9 +141,9 @@ public class Duplicator { } } - Class c = aSource.getClass(); + Class<?> c = aSource.getClass(); try { - result = c.newInstance(); + result = c.getDeclaredConstructor().newInstance(); } catch (Exception exc) { throw new WotonomyException(exc); } diff --git a/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/internal/IntrospectorException.java b/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/internal/IntrospectorException.java index 45080a2..a5887ab 100644 --- a/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/internal/IntrospectorException.java +++ b/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/internal/IntrospectorException.java @@ -28,4 +28,5 @@ package net.wotonomy.foundation.internal; */ public class IntrospectorException extends WotonomyException { + private static final long serialVersionUID = -997809394704941577L; } diff --git a/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/internal/MissingPropertyException.java b/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/internal/MissingPropertyException.java index c89742f..c53595c 100644 --- a/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/internal/MissingPropertyException.java +++ b/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/internal/MissingPropertyException.java @@ -28,4 +28,5 @@ package net.wotonomy.foundation.internal; */ public class MissingPropertyException extends IntrospectorException { + private static final long serialVersionUID = -6130027204690491083L; } diff --git a/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/internal/NullPrimitiveException.java b/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/internal/NullPrimitiveException.java index bf1dffd..aa52e69 100644 --- a/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/internal/NullPrimitiveException.java +++ b/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/internal/NullPrimitiveException.java @@ -28,4 +28,5 @@ package net.wotonomy.foundation.internal; */ public class NullPrimitiveException extends IntrospectorException { + private static final long serialVersionUID = -572058252131498585L; } diff --git a/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/internal/PropertyComparator.java b/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/internal/PropertyComparator.java index c804893..f8b1cca 100644 --- a/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/internal/PropertyComparator.java +++ b/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/internal/PropertyComparator.java @@ -29,7 +29,8 @@ import java.util.Comparator; * @author $Author: cgruber $ * @version $Revision: 893 $ */ -public class PropertyComparator implements Comparator, Serializable { +public class PropertyComparator<T> implements Comparator<T>, Serializable { + private static final long serialVersionUID = -3028294144521868334L; private String property; /** @@ -43,13 +44,15 @@ public class PropertyComparator implements Comparator, Serializable { // interface Comparator + @SuppressWarnings("unchecked") + @Override public int compare(Object o1, Object o2) { Object v1 = Introspector.get(o1, property); Object v2 = Introspector.get(o2, property); - if (v1 instanceof Comparable) { - return ((Comparable) v1).compareTo(v2); + if (v1 instanceof Comparable<?>) { + return ((Comparable<Object>) v1).compareTo(v2); } else if (v2 instanceof Comparable) { - return ((Comparable) v2).compareTo(v1); + return ((Comparable<Object>) v2).compareTo(v1); } else { if (v1 == null) { if (v2 == null) { @@ -64,6 +67,7 @@ public class PropertyComparator implements Comparator, Serializable { return v1.toString().compareTo(v2.toString()); } + @Override public boolean equals(Object obj) { return (obj instanceof PropertyComparator); } 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"); diff --git a/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/internal/ValueConverter.java b/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/internal/ValueConverter.java index 7f28e06..3d995e5 100644 --- a/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/internal/ValueConverter.java +++ b/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/internal/ValueConverter.java @@ -37,69 +37,70 @@ public class ValueConverter { * Returns the specified object as converted to an instance of the specified * class, or null if the conversion could not be performed. */ - static public Object convertObjectToClass(Object anObject, Class aClass) { + @SuppressWarnings("unchecked") + static public <C> C convertObjectToClass(Object anObject, Class<C> aClass) { if (aClass == String.class) { - return getString(anObject); + return (C) getString(anObject); } if (aClass == Short.class) { - return getShort(anObject); + return (C) getShort(anObject); } if (aClass == short.class) { - return getShort(anObject); + return (C) getShort(anObject); } if (aClass == Integer.class) { - return getInteger(anObject); + return (C) getInteger(anObject); } if (aClass == int.class) { - return getInteger(anObject); + return (C) getInteger(anObject); } if (aClass == Long.class) { - return getLong(anObject); + return (C) getLong(anObject); } if (aClass == long.class) { - return getLong(anObject); + return (C) getLong(anObject); } if (aClass == Float.class) { - return getFloat(anObject); + return (C) getFloat(anObject); } if (aClass == float.class) { - return getFloat(anObject); + return (C) getFloat(anObject); } if (aClass == Double.class) { - return getDouble(anObject); + return (C) getDouble(anObject); } if (aClass == double.class) { - return getDouble(anObject); + return (C) getDouble(anObject); } if (aClass == java.util.Date.class) { - return getDate(anObject); + return (C) getDate(anObject); } if (aClass == Boolean.class) { - return getBoolean(anObject); + return (C) getBoolean(anObject); } if (aClass == boolean.class) { - return getBoolean(anObject); + return (C) getBoolean(anObject); } if (aClass == Character.class) { - return getCharacter(anObject); + return (C) getCharacter(anObject); } if (aClass == char.class) { - return getCharacter(anObject); + return (C) getCharacter(anObject); } if (aClass == Byte.class) { - return getByte(anObject); + return (C) getByte(anObject); } if (aClass == byte.class) { - return getByte(anObject); + return (C) getByte(anObject); } if (Collection.class.isAssignableFrom(aClass)) { - return getCollection(anObject, aClass); + return (C) getCollection(anObject, aClass); } if (aClass.isArray()) { - return getArray(anObject, aClass); + return (C) getArray(anObject, aClass); } - return convert(anObject, aClass); + return (C) convert(anObject, aClass); } /** @@ -108,10 +109,10 @@ public class ValueConverter { * the best fit to the object. and returns a new instance with that constructor. * Subclasses can override to directly support specific types. */ - static protected Object convert(Object anObject, Class aClass) { - Constructor[] ctors = aClass.getConstructors(); + static protected Object convert(Object anObject, Class<?> aClass) { + Constructor<?>[] ctors = aClass.getConstructors(); - Class[] types; + Class<?>[] types; for (int i = 0; i < ctors.length; i++) { types = ctors[i].getParameterTypes(); if (types.length == 1) { @@ -148,9 +149,9 @@ public class ValueConverter { static protected Object preprocess(Object anObject) { if (anObject instanceof Boolean) { if (((Boolean) anObject).booleanValue()) { - return new Double(1.0); + return Double.valueOf(1.0); } - return new Double(0.0); + return Double.valueOf(0.0); } if (anObject instanceof Character) { @@ -167,9 +168,9 @@ public class ValueConverter { static public Short getShort(Object anObject) { if (anObject == null) - return new Short((short) 0); + return Short.valueOf((short) 0); if ("".equals(anObject)) - return new Short((short) 0); + return Short.valueOf((short) 0); if (anObject instanceof Short) return (Short) anObject; diff --git a/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/internal/WotonomyException.java b/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/internal/WotonomyException.java index 21b47b3..041a216 100644 --- a/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/internal/WotonomyException.java +++ b/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/internal/WotonomyException.java @@ -26,7 +26,7 @@ import java.io.PrintWriter; * Behaves as a normal RuntimeException except that it prints a stack trace of * the wrapped throwable if one is specified. * - * Intended to be used anytime you'd report an exception with + * Intended to be used any time you'd report an exception with * System.out.println: that is, for debugging and non-user-visible exceptions. * * @author michael@mpowers.net @@ -35,6 +35,8 @@ import java.io.PrintWriter; */ public class WotonomyException extends RuntimeException { + private static final long serialVersionUID = -7267633105344041941L; + protected String message; protected Throwable wrappedThrowable; @@ -81,6 +83,7 @@ public class WotonomyException extends RuntimeException { return wrappedThrowable; } + @Override public void printStackTrace(PrintWriter s) { if (message != null) { s.println("Exception: " + message); @@ -92,6 +95,7 @@ public class WotonomyException extends RuntimeException { super.printStackTrace(s); } + @Override public void printStackTrace(PrintStream s) { if (message != null) { s.println("Exception: " + message); @@ -103,6 +107,7 @@ public class WotonomyException extends RuntimeException { super.printStackTrace(s); } + @Override public void printStackTrace() { if (message != null) { System.err.println("Exception: " + message); diff --git a/projects/net.wotonomy.foundation/src/test/java/net/wotonomy/foundation/NSArrayTest.java b/projects/net.wotonomy.foundation/src/test/java/net/wotonomy/foundation/NSArrayTest.java index e8bdbbc..98689dc 100644 --- a/projects/net.wotonomy.foundation/src/test/java/net/wotonomy/foundation/NSArrayTest.java +++ b/projects/net.wotonomy.foundation/src/test/java/net/wotonomy/foundation/NSArrayTest.java @@ -45,7 +45,7 @@ public class NSArrayTest extends TestCase { * Test method for 'net.wotonomy.foundation.NSArray.arrayBackedByList(List)' */ public void testArrayBackedByList() { - List list = new ArrayList(); + List<Object> list = new ArrayList<>(); NSArray array = NSArray.arrayBackedByList(list); assertNotNull(array); assertSame(list, array.list); @@ -63,7 +63,7 @@ public class NSArrayTest extends TestCase { * Test method for 'net.wotonomy.foundation.NSArray.NSArray(List, Object)' */ public void testNSArrayListObject() { - List list = new ArrayList(); + List<Object> list = new ArrayList<>(); NSArray array = new NSArray(list, null); assertNotNull(array); assertSame(list, array.list); @@ -131,7 +131,7 @@ public class NSArrayTest extends TestCase { * Test method for 'net.wotonomy.foundation.NSArray.NSArray(Collection)' */ public void testNSArrayCollection() { - ArrayList list = new ArrayList(); + ArrayList<Object> list = new ArrayList<>(); list.add(o1); list.add(o2); list.add(o3); @@ -164,7 +164,7 @@ public class NSArrayTest extends TestCase { */ public void testArrayByAddingObjectsFromArray() { NSArray array = new NSArray(o1); - List list = new ArrayList(); + List<Object> list = new ArrayList<>(); list.add(o2); list.add(o3); NSArray array2 = array.arrayByAddingObjectsFromArray(list); @@ -198,7 +198,7 @@ public class NSArrayTest extends TestCase { * 'net.wotonomy.foundation.NSArray.firstObjectCommonWithArray(Collection)' */ public void testFirstObjectCommonWithArray() { - ArrayList list = new ArrayList(); + ArrayList<Object> list = new ArrayList<>(); list.add(o2); list.add(o3); list.add(o4); |
