You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pivot.apache.org by rw...@apache.org on 2017/05/15 22:18:59 UTC

svn commit: r1795246 - in /pivot/trunk/core/src/org/apache/pivot/util: ListenerList.java Utils.java

Author: rwhitcomb
Date: Mon May 15 22:18:59 2017
New Revision: 1795246

URL: http://svn.apache.org/viewvc?rev=1795246&view=rev
Log:
Use some Utils methods in ListenerList to simplify / regularize some of the
parameter validation checks.

Make a new "checkZeroBasedIndex" method in Utils (and use it in ListenerList)
to simplify the oft-used case of checking an array/list/whatever index against
0..size-1.


Modified:
    pivot/trunk/core/src/org/apache/pivot/util/ListenerList.java
    pivot/trunk/core/src/org/apache/pivot/util/Utils.java

Modified: pivot/trunk/core/src/org/apache/pivot/util/ListenerList.java
URL: http://svn.apache.org/viewvc/pivot/trunk/core/src/org/apache/pivot/util/ListenerList.java?rev=1795246&r1=1795245&r2=1795246&view=diff
==============================================================================
--- pivot/trunk/core/src/org/apache/pivot/util/ListenerList.java (original)
+++ pivot/trunk/core/src/org/apache/pivot/util/ListenerList.java Mon May 15 22:18:59 2017
@@ -108,9 +108,8 @@ public abstract class ListenerList<T> im
     }
 
     private int indexOf(T listener) {
-        if (listener == null) {
-            throw new IllegalArgumentException("listener is null.");
-        }
+        Utils.checkNull(listener, "listener");
+
         for (int i = 0; i < last; i++) {
             if (list[i] == listener) {
                 return i;
@@ -157,9 +156,7 @@ public abstract class ListenerList<T> im
      * @throws IndexOutOfBoundsException if the index is out of range.
      */
     public T get(int index) {
-        if (index < 0 || index >= last) {
-            throw new IndexOutOfBoundsException("index " + index + " out of bounds [0," + last + "].");
-        }
+        Utils.checkZeroBasedIndex(index, last);
         return list[index];
     }
 

Modified: pivot/trunk/core/src/org/apache/pivot/util/Utils.java
URL: http://svn.apache.org/viewvc/pivot/trunk/core/src/org/apache/pivot/util/Utils.java?rev=1795246&r1=1795245&r2=1795246&view=diff
==============================================================================
--- pivot/trunk/core/src/org/apache/pivot/util/Utils.java (original)
+++ pivot/trunk/core/src/org/apache/pivot/util/Utils.java Mon May 15 22:18:59 2017
@@ -155,6 +155,20 @@ public class Utils {
     }
 
     /**
+     * Special case of {@link #checkIndexBounds(int, int, int)} for the case that start is zero and therefore
+     * the end case is usually size - 1.
+     *
+     * @param index   The candidate index into the zero-based range.
+     * @param size    The size of the array/list/etc. (so the proper range is {@code 0 .. size - 1}).
+     * @throws IndexOutOfBoundsException if the {@code index} is &lt; 0 or &gt;= {@code size}.
+     */
+    public static void checkZeroBasedIndex(int index, int size) {
+        if (index < 0 || index >= size) {
+            throw new IndexOutOfBoundsException("index " + index + " out of bounds [0," + (size-1) + "].");
+        }
+    }
+
+    /**
      * Check that the given {@code index} plus {@code count} are between the values of {@code start} and {@code end}.
      *
      * @param index  The candidate index into the range.