diff options
Diffstat (limited to 'projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSArray.java')
| -rw-r--r-- | projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSArray.java | 193 |
1 files changed, 119 insertions, 74 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); } } |
