[cp-patches] JList.getNextMatch() fix

David Gilbert david.gilbert at object-refinery.com
Mon Jun 26 17:16:17 UTC 2006


This patch (committed) fixes the getNextMatch() method to perform a circular (and 
non-case-sensitive) search for the next matching item, to correspond with the 
reference implementation.  This fixes a failing test in Intel's test suite, and some 
additional checks I added to Mauve:

2006-06-26  David Gilbert  <david.gilbert at object-refinery.com>

	* javax/swing/JList.java
	(getNextMatch): Reimplemented to perform a circular search for the
	matching item.

Regards,

Dave
-------------- next part --------------
Index: javax/swing/JList.java
===================================================================
RCS file: /sources/classpath/classpath/javax/swing/JList.java,v
retrieving revision 1.58
diff -u -r1.58 JList.java
--- javax/swing/JList.java	26 Jun 2006 14:50:48 -0000	1.58
+++ javax/swing/JList.java	26 Jun 2006 17:07:04 -0000
@@ -2289,14 +2289,16 @@
   }
 
   /**
-   * Returns the next list element (beginning from <code>startIndex</code>
-   * that starts with <code>prefix</code>. Searching is done in the direction
-   * specified by <code>bias</code>.
+   * Returns the index of the next list element (beginning at 
+   * <code>startIndex</code> and moving in the specified direction through the
+   * list, looping around if necessary) that starts with <code>prefix</code>
+   * (ignoring case).
    *
    * @param prefix the prefix to search for in the cell values
    * @param startIndex the index where to start searching from
-   * @param bias the search direction, either {@link Position.Bias#Forward}
-   *     or {@link Position.Bias#Backward}
+   * @param direction the search direction, either {@link Position.Bias#Forward}
+   *     or {@link Position.Bias#Backward} (<code>null</code> is interpreted
+   *     as {@link Position.Bias#Backward}.
    *
    * @return the index of the found element or -1 if no such element has
    *     been found
@@ -2306,7 +2308,8 @@
    *
    * @since 1.4
    */
-  public int getNextMatch(String prefix, int startIndex, Position.Bias bias)
+  public int getNextMatch(String prefix, int startIndex, 
+                          Position.Bias direction)
   {
     if (prefix == null)
       throw new IllegalArgumentException("The argument 'prefix' must not be"
@@ -2316,37 +2319,33 @@
                                          + " be less than zero.");
 
     int size = model.getSize();
-    if (startIndex > model.getSize())
+    if (startIndex >= model.getSize())
       throw new IllegalArgumentException("The argument 'startIndex' must not"
                                          + " be greater than the number of"
                                          + " elements in the ListModel.");
 
-    int index = -1;
-    if (bias == Position.Bias.Forward)
+    int result = -1;
+    int current = startIndex;
+    int delta = -1;
+    int itemCount = model.getSize();
+    boolean finished = false;
+    prefix = prefix.toUpperCase();
+    
+    if (direction == Position.Bias.Forward)
+      delta = 1;
+    while (!finished)
       {
-        for (int i = startIndex; i < size; i++)
-          {
-            String item = model.getElementAt(i).toString();
-            if (item.startsWith(prefix))
-              {
-                index = i;
-                break;
-              }
-          }
+        String itemStr = model.getElementAt(current).toString().toUpperCase();
+        if (itemStr.startsWith(prefix))
+          return current;
+        current = (current + delta);
+        if (current == -1)
+          current += itemCount;
+        else
+          current = current % itemCount; 
+        finished = current == startIndex;
       }
-    else
-      {
-        for (int i = startIndex; i >= 0; i--)
-          {
-            String item = model.getElementAt(i).toString();
-            if (item.startsWith(prefix))
-              {
-                index = i;
-                break;
-              }
-          }
-      }
-    return index;
+    return result;
   }
   
   /**


More information about the Classpath-patches mailing list