You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pivot.apache.org by no...@apache.org on 2011/12/21 09:45:10 UTC

svn commit: r1221645 - /pivot/trunk/wtk/src/org/apache/pivot/wtk/

Author: noelgrandin
Date: Wed Dec 21 08:45:09 2011
New Revision: 1221645

URL: http://svn.apache.org/viewvc?rev=1221645&view=rev
Log:
pull the index bounds checking code out into a utility method, improving the exception messages in the process

Modified:
    pivot/trunk/wtk/src/org/apache/pivot/wtk/Accordion.java
    pivot/trunk/wtk/src/org/apache/pivot/wtk/Alert.java
    pivot/trunk/wtk/src/org/apache/pivot/wtk/CardPane.java
    pivot/trunk/wtk/src/org/apache/pivot/wtk/Component.java
    pivot/trunk/wtk/src/org/apache/pivot/wtk/Container.java
    pivot/trunk/wtk/src/org/apache/pivot/wtk/ListButton.java
    pivot/trunk/wtk/src/org/apache/pivot/wtk/ListView.java
    pivot/trunk/wtk/src/org/apache/pivot/wtk/Prompt.java
    pivot/trunk/wtk/src/org/apache/pivot/wtk/Spinner.java
    pivot/trunk/wtk/src/org/apache/pivot/wtk/SuggestionPopup.java
    pivot/trunk/wtk/src/org/apache/pivot/wtk/TabPane.java
    pivot/trunk/wtk/src/org/apache/pivot/wtk/TableView.java
    pivot/trunk/wtk/src/org/apache/pivot/wtk/TextArea.java
    pivot/trunk/wtk/src/org/apache/pivot/wtk/TextPane.java

Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/Accordion.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/Accordion.java?rev=1221645&r1=1221644&r2=1221645&view=diff
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/Accordion.java (original)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/Accordion.java Wed Dec 21 08:45:09 2011
@@ -240,10 +240,7 @@ public class Accordion extends Container
     }
 
     public void setSelectedIndex(int selectedIndex) {
-        if (selectedIndex < -1
-            || selectedIndex > panels.getLength() - 1) {
-            throw new IndexOutOfBoundsException();
-        }
+        indexBoundsCheck("selectedIndex", selectedIndex, -1, panels.getLength() - 1);
 
         int previousSelectedIndex = this.selectedIndex;
 

Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/Alert.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/Alert.java?rev=1221645&r1=1221644&r2=1221645&view=diff
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/Alert.java (original)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/Alert.java Wed Dec 21 08:45:09 2011
@@ -301,10 +301,7 @@ public class Alert extends Dialog {
     }
 
     public void setSelectedOptionIndex(int selectedOption) {
-        if (selectedOption < -1
-            || selectedOption > options.getLength() - 1) {
-            throw new IndexOutOfBoundsException();
-        }
+        indexBoundsCheck("selectedOption", selectedOption, -1, options.getLength() - 1);
 
         int previousSelectedOption = this.selectedOptionIndex;
 

Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/CardPane.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/CardPane.java?rev=1221645&r1=1221644&r2=1221645&view=diff
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/CardPane.java (original)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/CardPane.java Wed Dec 21 08:45:09 2011
@@ -78,10 +78,7 @@ public class CardPane extends Container 
      * The selected card index, or <tt>-1</tt> for no selection.
      */
     public void setSelectedIndex(int selectedIndex) {
-        if (selectedIndex < -1
-            || selectedIndex > getLength() - 1) {
-            throw new IndexOutOfBoundsException();
-        }
+        indexBoundsCheck("selectedIndex", selectedIndex, -1, getLength() -1);
 
         int previousSelectedIndex = this.selectedIndex;
 

Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/Component.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/Component.java?rev=1221645&r1=1221644&r2=1221645&view=diff
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/Component.java (original)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/Component.java Wed Dec 21 08:45:09 2011
@@ -2920,4 +2920,18 @@ public abstract class Component implemen
     public static ListenerList<ComponentClassListener> getComponentClassListeners() {
         return componentClassListeners;
     }
+
+    /**
+     * Provide a nice exception message for out of range values.
+     *
+     * @throws IndexOutOfBoundsException if index is out of range.
+     */
+    protected static final void indexBoundsCheck(String indexName, int index, int min, int max) throws IndexOutOfBoundsException {
+        if (index < min) {
+            throw new IndexOutOfBoundsException(indexName + index + " < " + min);
+        }
+        if (index > max) {
+            throw new IndexOutOfBoundsException(indexName + index + " > " + max);
+        }
+    }
 }

Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/Container.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/Container.java?rev=1221645&r1=1221644&r2=1221645&view=diff
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/Container.java (original)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/Container.java Wed Dec 21 08:45:09 2011
@@ -221,12 +221,8 @@ public abstract class Container extends 
         if (from != to) {
             int n = components.getLength();
 
-            if (from < 0
-                || from >= n
-                || to < 0
-                || to >= n) {
-                throw new IndexOutOfBoundsException();
-            }
+            indexBoundsCheck("from", from, 0, n - 1);
+            indexBoundsCheck("to", to, 0, n - 1);
 
             Sequence<Component> removed = components.remove(from, 1);
             Component component = removed.get(0);

Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/ListButton.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/ListButton.java?rev=1221645&r1=1221644&r2=1221645&view=diff
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/ListButton.java (original)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/ListButton.java Wed Dec 21 08:45:09 2011
@@ -504,10 +504,7 @@ public class ListButton extends Button {
      * selection.
      */
     public void setSelectedIndex(int selectedIndex) {
-        if (selectedIndex < -1
-            || selectedIndex >= listData.getLength()) {
-            throw new IndexOutOfBoundsException();
-        }
+        indexBoundsCheck("selectedIndex", selectedIndex, -1, listData.getLength() - 1);
 
         int previousSelectedIndex = this.selectedIndex;
 

Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/ListView.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/ListView.java?rev=1221645&r1=1221644&r2=1221645&view=diff
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/ListView.java (original)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/ListView.java Wed Dec 21 08:45:09 2011
@@ -1083,9 +1083,7 @@ public class ListView extends Component 
      * otherwise.
      */
     public boolean isItemSelected(int index) {
-        if (index < 0 || index >= listData.getLength()) {
-            throw new IndexOutOfBoundsException();
-        }
+        indexBoundsCheck("index", index, 0, listData.getLength() - 1);
 
         return (rangeSelection.containsIndex(index));
     }

Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/Prompt.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/Prompt.java?rev=1221645&r1=1221644&r2=1221645&view=diff
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/Prompt.java (original)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/Prompt.java Wed Dec 21 08:45:09 2011
@@ -269,10 +269,7 @@ public class Prompt extends Sheet {
     }
 
     public void setSelectedOptionIndex(int selectedOption) {
-        if (selectedOption < -1
-            || selectedOption > options.getLength() - 1) {
-            throw new IndexOutOfBoundsException();
-        }
+        indexBoundsCheck("selectedOption", selectedOption, -1, options.getLength() - 1);
 
         int previousSelectedOption = this.selectedOptionIndex;
 

Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/Spinner.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/Spinner.java?rev=1221645&r1=1221644&r2=1221645&view=diff
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/Spinner.java (original)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/Spinner.java Wed Dec 21 08:45:09 2011
@@ -496,10 +496,7 @@ public class Spinner extends Container {
      * The index to select, or <tt>-1</tt> to clear the selection.
      */
     public void setSelectedIndex(int selectedIndex) {
-        if (selectedIndex < -1
-            || selectedIndex >= spinnerData.getLength()) {
-            throw new IndexOutOfBoundsException();
-        }
+        indexBoundsCheck("selectedIndex", selectedIndex, -1, spinnerData.getLength() - 1);
 
         int previousSelectedIndex = this.selectedIndex;
 

Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/SuggestionPopup.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/SuggestionPopup.java?rev=1221645&r1=1221644&r2=1221645&view=diff
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/SuggestionPopup.java (original)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/SuggestionPopup.java Wed Dec 21 08:45:09 2011
@@ -335,10 +335,7 @@ public class SuggestionPopup extends Win
      * selection.
      */
     public void setSelectedIndex(int selectedIndex) {
-        if (selectedIndex < -1
-            || selectedIndex >= suggestionData.getLength()) {
-            throw new IndexOutOfBoundsException();
-        }
+        indexBoundsCheck("selectedIndex", selectedIndex, -1, suggestionData.getLength() - 1);
 
         int previousSelectedIndex = this.selectedIndex;
 

Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/TabPane.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/TabPane.java?rev=1221645&r1=1221644&r2=1221645&view=diff
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/TabPane.java (original)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/TabPane.java Wed Dec 21 08:45:09 2011
@@ -317,10 +317,7 @@ public class TabPane extends Container {
     }
 
     public void setSelectedIndex(int selectedIndex) {
-        if (selectedIndex < -1
-            || selectedIndex > tabs.getLength() - 1) {
-            throw new IndexOutOfBoundsException();
-        }
+        indexBoundsCheck("selectedIndex", selectedIndex, -1, tabs.getLength() - 1);
 
         int previousSelectedIndex = this.selectedIndex;
 

Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/TableView.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/TableView.java?rev=1221645&r1=1221644&r2=1221645&view=diff
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/TableView.java (original)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/TableView.java Wed Dec 21 08:45:09 2011
@@ -1723,13 +1723,7 @@ public class TableView extends Component
      * otherwise.
      */
     public boolean isRowSelected(int index) {
-        if (index < 0) {
-            throw new IndexOutOfBoundsException("index < 0, " + index);
-        }
-        if (index >= tableData.getLength()) {
-            throw new IndexOutOfBoundsException("index >= tableData.getLength(), "
-                  + index + " >= " + tableData.getLength());
-        }
+        indexBoundsCheck("index", index, 0, tableData.getLength() - 1);
 
         return rangeSelection.containsIndex(index);
     }

Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/TextArea.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/TextArea.java?rev=1221645&r1=1221644&r2=1221645&view=diff
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/TextArea.java (original)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/TextArea.java Wed Dec 21 08:45:09 2011
@@ -92,10 +92,7 @@ public class TextArea extends Component 
                 throw new IllegalArgumentException();
             }
 
-            if (index < 0
-                || index > characters.length()) {
-                throw new IndexOutOfBoundsException();
-            }
+            indexBoundsCheck("index", index, 0, characters.length());
 
             int count = text.length();
 
@@ -738,10 +735,7 @@ public class TextArea extends Component 
             throw new IllegalArgumentException();
         }
 
-        if (index < 0
-            || index > characterCount) {
-            throw new IndexOutOfBoundsException();
-        }
+        indexBoundsCheck("index", index, 0, characterCount);
 
         if (text.length() > 0) {
             // Insert the text
@@ -847,10 +841,7 @@ public class TextArea extends Component 
      * @param index
      */
     public int getParagraphAt(int index) {
-        if (index < 0
-            || index > characterCount) {
-            throw new IndexOutOfBoundsException();
-        }
+        indexBoundsCheck("index", index, 0, characterCount);
 
         int paragraphIndex = paragraphs.getLength() - 1;
         Paragraph paragraph = paragraphs.get(paragraphIndex);
@@ -868,10 +859,7 @@ public class TextArea extends Component 
      * @param index
      */
     public char getCharacterAt(int index) {
-        if (index < 0
-            || index >= characterCount) {
-            throw new IndexOutOfBoundsException();
-        }
+        indexBoundsCheck("index", index, 0, characterCount - 1);
 
         int paragraphIndex = getParagraphAt(index);
         Paragraph paragraph = paragraphs.get(paragraphIndex);

Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/TextPane.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/TextPane.java?rev=1221645&r1=1221644&r2=1221645&view=diff
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/TextPane.java (original)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/TextPane.java Wed Dec 21 08:45:09 2011
@@ -718,14 +718,7 @@ public class TextPane extends Container 
             throw new IllegalArgumentException("selectionLength is negative, selectionLength=" + selectionLength);
         }
 
-        if (selectionStart < 0) {
-            throw new IndexOutOfBoundsException("selectionStart < 0, selectionStart=" + selectionStart);
-        }
-
-        if (selectionStart >= document.getCharacterCount()) {
-            throw new IndexOutOfBoundsException("selectionStart=" + selectionStart
-                + ", document.characterCount=" + document.getCharacterCount());
-        }
+        indexBoundsCheck("selectionStart", selectionStart, 0, document.getCharacterCount() - 1);
 
         if (selectionStart + selectionLength > document.getCharacterCount()) {
             throw new IndexOutOfBoundsException("selectionStart=" + selectionStart + ", selectionLength=" + selectionLength