Skip to content

Commit

Permalink
Clean up collection arg checks.
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.apache.org/repos/asf/incubator/pivot/trunk@881058 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
Gregory K. Brown committed Nov 16, 2009
1 parent 0b5391b commit ee9c147
Show file tree
Hide file tree
Showing 12 changed files with 78 additions and 89 deletions.
12 changes: 6 additions & 6 deletions build.xml
Expand Up @@ -149,12 +149,12 @@ limitations under the License.
<sequential>
<mkdir dir="${folder.lib}"/>
<jar destfile="${folder.lib}/@{jarFile}" index="${compiler.indexJars}">
<metainf dir="${basedir}">
<include name="DISCLAIMER"/>
<include name="LICENSE"/>
<include name="NOTICE"/>
<include name="README"/>
</metainf>
<metainf dir="${basedir}">
<include name="DISCLAIMER"/>
<include name="LICENSE"/>
<include name="NOTICE"/>
<include name="README"/>
</metainf>
<manifest>
<attribute name="Sealed" value="true"/>
<attribute name="Implementation-Vendor-Id" value="org.apache"/>
Expand Down
58 changes: 33 additions & 25 deletions core/src/org/apache/pivot/collections/ArrayList.java
Expand Up @@ -141,7 +141,7 @@ public ArrayList(Comparator<T> comparator) {
}

public ArrayList(int capacity) {
ArrayList.validateZeroOrGreater("capacity", capacity);
ArrayList.verifyNonNegative("capacity", capacity);

items = new Object[capacity];
}
Expand All @@ -151,7 +151,7 @@ public ArrayList(T... items) {
}

public ArrayList(T[] items, int index, int count) {
CollectionArgChecks.verifyNotNull("items", items);
verifyNotNull("items", items);
verifyIndexBounds(index, count, 0, items.length);

this.items = new Object[count];
Expand All @@ -165,7 +165,7 @@ public ArrayList(Sequence<T> items) {
}

public ArrayList(Sequence<T> items, int index, int count) {
CollectionArgChecks.verifyNotNull("items", items);
verifyNotNull("items", items);
verifyIndexBounds(index, count, 0, items.getLength());

this.items = new Object[count];
Expand All @@ -182,7 +182,7 @@ public ArrayList(ArrayList<T> arrayList) {
}

public ArrayList(ArrayList<T> arrayList, int index, int count) {
CollectionArgChecks.verifyNotNull("arrayList", arrayList);
verifyNotNull("arrayList", arrayList);
verifyIndexBounds(index, count, 0, arrayList.length);

items = new Object[count];
Expand Down Expand Up @@ -498,20 +498,14 @@ public String toString() {
return sb.toString();
}

private static void validateZeroOrGreater(String fieldName, int field) {
if (field < 0) {
throw new IllegalArgumentException(fieldName + " " + field + " cannot be < 0");
}
}

public static <T> void sort(ArrayList<T> arrayList, Comparator<T> comparator) {
public static <T> void sort(ArrayList<T> arrayList, Comparator<T> comparator) {
sort(arrayList, 0, arrayList.getLength(), comparator);
}

@SuppressWarnings("unchecked")
public static <T> void sort(ArrayList<T> arrayList, int from, int to, Comparator<T> comparator) {
CollectionArgChecks.verifyNotNull("arrayList", arrayList);
CollectionArgChecks.verifyNotNull("comparator", comparator);
verifyNotNull("arrayList", arrayList);
verifyNotNull("comparator", comparator);

Arrays.sort((T[])arrayList.items, from, to, comparator);

Expand All @@ -529,9 +523,9 @@ public int compare(T t1, T t2) {

@SuppressWarnings("unchecked")
public static <T> int binarySearch(ArrayList<T> arrayList, T item, Comparator<T> comparator) {
CollectionArgChecks.verifyNotNull("arrayList", arrayList);
CollectionArgChecks.verifyNotNull("comparator", comparator);
CollectionArgChecks.verifyNotNull("item", item);
verifyNotNull("arrayList", arrayList);
verifyNotNull("comparator", comparator);
verifyNotNull("item", item);

int index = Arrays.binarySearch((T[])arrayList.items, 0, arrayList.length, item, comparator);

Expand All @@ -547,22 +541,36 @@ public int compare(T t1, T t2) {
}
});
}

private static void verifyIndexBounds(int index, int boundStart, int boundEnd) {
if (index < boundStart || index > boundEnd) {
throw new IndexOutOfBoundsException("index " + index + " out of bounds");

private static void verifyNotNull(String argument, Object value) {
if (value == null) {
throw new IllegalArgumentException(argument + " cannot be null.");
}
}

private static void verifyNonNegative(String argument, int value) {
if (value < 0) {
throw new IllegalArgumentException(argument + " cannot be negative.");
}
}

private static void verifyIndexBounds(int index, int start, int end) {
if (index < start || index > end) {
throw new IndexOutOfBoundsException("index " + index + " out of bounds.");
}
}

private static void verifyIndexBounds(int index, int count, int boundStart, int boundEnd) {
private static void verifyIndexBounds(int index, int count, int start, int end) {
if (count < 0) {
throw new IllegalArgumentException();
}
if (index < boundStart) {
throw new IndexOutOfBoundsException("index " + index + " out of bounds");

if (index < start) {
throw new IndexOutOfBoundsException("index " + index + " out of bounds.");
}
if (index + count > boundEnd) {
throw new IndexOutOfBoundsException("index + count " + index + "," + count + " out of range");

if (index + count > end) {
throw new IndexOutOfBoundsException("index + count " + index + "," + count + " out of range.");
}
}
}
31 changes: 0 additions & 31 deletions core/src/org/apache/pivot/collections/CollectionArgChecks.java

This file was deleted.

4 changes: 3 additions & 1 deletion core/src/org/apache/pivot/collections/Dictionary.java
Expand Up @@ -28,7 +28,9 @@ public static final class Pair<K, V> {
public final V value;

public Pair(K key, V value) {
CollectionArgChecks.verifyNotNull("key", key);
if (key == null) {
throw new IllegalArgumentException("key cannot be null.");
}

this.key = key;
this.value = value;
Expand Down
4 changes: 3 additions & 1 deletion core/src/org/apache/pivot/collections/EnumList.java
Expand Up @@ -107,7 +107,9 @@ public E get(int index) {

@Override
public int indexOf(E item) {
CollectionArgChecks.verifyNotNull("item", item);
if (item == null) {
throw new IllegalArgumentException("item cannot be null.");
}

return item.ordinal();
}
Expand Down
12 changes: 9 additions & 3 deletions core/src/org/apache/pivot/collections/EnumMap.java
Expand Up @@ -44,15 +44,15 @@ public EnumMap(Class<E> enumClass) {
@SuppressWarnings("unchecked")
@Override
public V get(E key) {
CollectionArgChecks.verifyNotNull("key", key);
verifyNotNull("key", key);

return (V)values[key.ordinal()];
}

@SuppressWarnings("unchecked")
@Override
public V put(E key, V value) {
CollectionArgChecks.verifyNotNull("key", key);
verifyNotNull("key", key);

int ordinal = key.ordinal();
V previousValue = (V)values[ordinal];
Expand All @@ -71,7 +71,7 @@ public V put(E key, V value) {
@SuppressWarnings("unchecked")
@Override
public V remove(E key) {
CollectionArgChecks.verifyNotNull("key", key);
verifyNotNull("key", key);

V value = null;
if (keySet.contains(key)) {
Expand Down Expand Up @@ -128,4 +128,10 @@ public Iterator<E> iterator() {
public ListenerList<MapListener<E, V>> getMapListeners() {
return mapListeners;
}

private static void verifyNotNull(String argument, Object value) {
if (value == null) {
throw new IllegalArgumentException(argument + " cannot be null.");
}
}
}
18 changes: 9 additions & 9 deletions core/src/org/apache/pivot/collections/LinkedList.java
Expand Up @@ -650,22 +650,22 @@ public String toString() {

return sb.toString();
}
private static void verifyIndexBounds(int index, int boundStart, int boundEnd) {
if (index < boundStart || index > boundEnd) {
throw new IndexOutOfBoundsException("index " + index + " out of bounds");

private static void verifyIndexBounds(int index, int start, int end) {
if (index < start || index > end) {
throw new IndexOutOfBoundsException("index " + index + " out of bounds.");
}
}

private static void verifyIndexBounds(int index, int count, int boundStart, int boundEnd) {
private static void verifyIndexBounds(int index, int count, int start, int end) {
if (count < 0) {
throw new IllegalArgumentException();
}
if (index < boundStart) {
throw new IndexOutOfBoundsException("index " + index + " out of bounds");
if (index < start) {
throw new IndexOutOfBoundsException("index " + index + " out of bounds.");
}
if (index + count > boundEnd) {
throw new IndexOutOfBoundsException("index + count " + index + "," + count + " out of range");
if (index + count > end) {
throw new IndexOutOfBoundsException("index + count " + index + "," + count + " out of range.");
}
}
}
Expand Up @@ -19,14 +19,12 @@
import java.util.Comparator;
import java.util.Iterator;

import org.apache.pivot.collections.CollectionArgChecks;
import org.apache.pivot.collections.List;
import org.apache.pivot.collections.ListListener;
import org.apache.pivot.collections.Sequence;
import org.apache.pivot.util.ImmutableIterator;
import org.apache.pivot.util.ListenerList;


/**
* Synchronized implementation of the {@link List} interface.
*/
Expand Down Expand Up @@ -73,7 +71,9 @@ public synchronized void comparatorChanged(List<T> list, Comparator<T> previousC
private SynchronizedListListenerList<T> listListeners = new SynchronizedListListenerList<T>();

public SynchronizedList(List<T> list) {
CollectionArgChecks.verifyNotNull("list", list);
if (list == null) {
throw new IllegalArgumentException("list cannot be null.");
}

this.list = list;
}
Expand Down
Expand Up @@ -19,7 +19,6 @@
import java.util.Comparator;
import java.util.Iterator;

import org.apache.pivot.collections.CollectionArgChecks;
import org.apache.pivot.collections.Map;
import org.apache.pivot.collections.MapListener;
import org.apache.pivot.util.ImmutableIterator;
Expand Down Expand Up @@ -71,7 +70,9 @@ public synchronized void comparatorChanged(Map<K, V> map, Comparator<K> previous
private SynchronizedMapListenerList<K, V> mapListeners = new SynchronizedMapListenerList<K, V>();

public SynchronizedMap(Map<K, V> map) {
CollectionArgChecks.verifyNotNull("map", map);
if (map == null) {
throw new IllegalArgumentException("map cannot be null.");
}

this.map = map;
}
Expand Down
Expand Up @@ -19,13 +19,11 @@
import java.util.Comparator;
import java.util.Iterator;

import org.apache.pivot.collections.CollectionArgChecks;
import org.apache.pivot.collections.Queue;
import org.apache.pivot.collections.QueueListener;
import org.apache.pivot.util.ImmutableIterator;
import org.apache.pivot.util.ListenerList;


/**
* Synchronized implementation of the {@link Queue} interface.
*/
Expand Down Expand Up @@ -57,7 +55,9 @@ public synchronized void itemDequeued(Queue<T> queue, T item) {
private SynchronizedQueueListenerList<T> queueListeners = new SynchronizedQueueListenerList<T>();

public SynchronizedQueue(Queue<T> queue) {
CollectionArgChecks.verifyNotNull("queue", queue);
if (queue == null) {
throw new IllegalArgumentException("queue cannot be null.");
}

this.queue = queue;
}
Expand Down
Expand Up @@ -19,7 +19,6 @@
import java.util.Comparator;
import java.util.Iterator;

import org.apache.pivot.collections.CollectionArgChecks;
import org.apache.pivot.collections.Set;
import org.apache.pivot.collections.SetListener;
import org.apache.pivot.util.ImmutableIterator;
Expand Down Expand Up @@ -66,7 +65,9 @@ public synchronized void comparatorChanged(Set<E> set, Comparator<E> previousCom
private SynchronizedSetListenerList<E> setListeners = new SynchronizedSetListenerList<E>();

public SynchronizedSet(Set<E> set) {
CollectionArgChecks.verifyNotNull("set", set);
if (set == null) {
throw new IllegalArgumentException("set cannot be null.");
}

this.set = set;
}
Expand Down
Expand Up @@ -19,13 +19,11 @@
import java.util.Comparator;
import java.util.Iterator;

import org.apache.pivot.collections.CollectionArgChecks;
import org.apache.pivot.collections.Stack;
import org.apache.pivot.collections.StackListener;
import org.apache.pivot.util.ImmutableIterator;
import org.apache.pivot.util.ListenerList;


/**
* Synchronized implementation of the {@link Stack} interface.
*/
Expand Down Expand Up @@ -57,7 +55,9 @@ public synchronized void itemPopped(Stack<T> stack, T item) {
private SynchronizedStackListenerList<T> stackListeners = new SynchronizedStackListenerList<T>();

public SynchronizedStack(Stack<T> stack) {
CollectionArgChecks.verifyNotNull("stack", stack);
if (stack == null) {
throw new IllegalArgumentException("stack cannot be null.");
}

this.stack = stack;
}
Expand Down

0 comments on commit ee9c147

Please sign in to comment.