You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by sc...@apache.org on 2003/08/04 01:29:19 UTC
cvs commit: jakarta-commons/lang/src/test/org/apache/commons/lang ArrayUtilsTest.java
scolebourne 2003/08/03 16:29:19
Modified: lang/src/java/org/apache/commons/lang ArrayUtils.java
lang/src/test/org/apache/commons/lang ArrayUtilsTest.java
Log:
Add tolerance checking to indexOf methods
bug 22091, from Tim O'Brien
Revision Changes Path
1.22 +127 -7 jakarta-commons/lang/src/java/org/apache/commons/lang/ArrayUtils.java
Index: ArrayUtils.java
===================================================================
RCS file: /home/cvs/jakarta-commons/lang/src/java/org/apache/commons/lang/ArrayUtils.java,v
retrieving revision 1.21
retrieving revision 1.22
diff -u -r1.21 -r1.22
--- ArrayUtils.java 1 Aug 2003 20:45:17 -0000 1.21
+++ ArrayUtils.java 3 Aug 2003 23:29:19 -0000 1.22
@@ -75,6 +75,7 @@
* @author <a href="mailto:fredrik@westermarck.com">Fredrik Westermarck</a>
* @author Nikolay Metchev
* @author Matthew Hawthorne
+ * @author Tim O'Brien
* @since 2.0
* @version $Id$
*/
@@ -1376,6 +1377,23 @@
}
/**
+ * <p>Find the index of the given value within a given tolerance in the array.
+ * This method will return the index of the first value which falls between the region
+ * defined by valueToFind - tolerance and valueToFind + tolerance.</p>
+ *
+ * <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
+ *
+ * @param array the array to search through for the object, may be <code>null</code>
+ * @param valueToFind the value to find
+ * @param tolerance tolerance of the search
+ * @return the index of the value within the array,
+ * <code>-1</code> if not found or <code>null</code> array input
+ */
+ public static int indexOf(final double[] array, final double valueToFind, final double tolerance) {
+ return indexOf(array, valueToFind, 0, tolerance);
+ }
+
+ /**
* <p>Find the index of the given value in the array starting at the given index.</p>
*
* <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
@@ -1390,7 +1408,7 @@
* <code>-1</code> if not found or <code>null</code> array input
*/
public static int indexOf(final double[] array, final double valueToFind, int startIndex) {
- if (array == null) {
+ if (array == null || array.length == 0) {
return -1;
}
if (startIndex < 0) {
@@ -1405,6 +1423,40 @@
}
/**
+ * <p>Find the index of the given value in the array starting at the given index.
+ * This method will return the index of the first value which falls between the region
+ * defined by valueToFind - tolerance and valueToFind + tolerance.</p>
+ *
+ * <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
+ *
+ * <p>A negative startIndex is treated as zero. A startIndex larger than the array
+ * length will return -1.</p>
+ *
+ * @param array the array to search through for the object, may be <code>null</code>
+ * @param valueToFind the value to find
+ * @param startIndex the index to start searching at
+ * @param tolerance tolerance of the search
+ * @return the index of the value within the array,
+ * <code>-1</code> if not found or <code>null</code> array input
+ */
+ public static int indexOf(final double[] array, final double valueToFind, int startIndex, double tolerance) {
+ if (array == null || array.length == 0) {
+ return -1;
+ }
+ if (startIndex < 0) {
+ startIndex = 0;
+ }
+ double min = valueToFind - tolerance;
+ double max = valueToFind + tolerance;
+ for (int i = startIndex; i < array.length; i++) {
+ if (array[i] >= min && array[i] <= max) {
+ return i;
+ }
+ }
+ return -1;
+ }
+
+ /**
* <p>Find the last index of the given value within the array.</p>
*
* <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
@@ -1419,6 +1471,23 @@
}
/**
+ * <p>Find the last index of the given value within a given tolerance in the array.
+ * This method will return the index of the last value which falls between the region
+ * defined by valueToFind - tolerance and valueToFind + tolerance.</p>
+ *
+ * <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
+ *
+ * @param array the array to search through for the object, may be <code>null</code>
+ * @param valueToFind the value to find
+ * @param tolerance tolerance of the search
+ * @return the index of the value within the array,
+ * <code>-1</code> if not found or <code>null</code> array input
+ */
+ public static int lastIndexOf(final double[] array, final double valueToFind, final double tolerance) {
+ return lastIndexOf(array, valueToFind, Integer.MAX_VALUE, tolerance);
+ }
+
+ /**
* <p>Find the last index of the given value in the array starting at the given index.</p>
*
* <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
@@ -1433,7 +1502,7 @@
* <code>-1</code> if not found or <code>null</code> array input
*/
public static int lastIndexOf(final double[] array, final double valueToFind, int startIndex) {
- if (array == null) {
+ if (array == null || array.length == 0) {
return -1;
}
if (startIndex < 0) {
@@ -1450,6 +1519,41 @@
}
/**
+ * <p>Find the last index of the given value in the array starting at the given index.
+ * This method will return the index of the last value which falls between the region
+ * defined by valueToFind - tolerance and valueToFind + tolerance.</p>
+ *
+ * <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
+ *
+ * <p>A negative startIndex will return -1. A startIndex larger than the array
+ * length will search from the end of the array.</p>
+ *
+ * @param array the array to traverse for looking for the object, may be <code>null</code>
+ * @param valueToFind the value to find
+ * @param startIndex the start index to travers backwards from
+ * @return the last index of the value within the array,
+ * <code>-1</code> if not found or <code>null</code> array input
+ */
+ public static int lastIndexOf(final double[] array, final double valueToFind, int startIndex, double tolerance) {
+ if (array == null || array.length == 0) {
+ return -1;
+ }
+ if (startIndex < 0) {
+ return -1;
+ } else if (startIndex >= array.length) {
+ startIndex = array.length - 1;
+ }
+ double min = valueToFind - tolerance;
+ double max = valueToFind + tolerance;
+ for (int i = startIndex; i >= 0; i--) {
+ if (array[i] >= min && array[i] <= max) {
+ return i;
+ }
+ }
+ return -1;
+ }
+
+ /**
* <p>Checks if the value is in the given array.</p>
*
* <p>The method returns <code>false</code> if a <code>null</code> array is passed in.</p>
@@ -1462,6 +1566,22 @@
return (indexOf(array, valueToFind) != -1);
}
+ /**
+ * <p>Checks if a value falling within the given tolerance is in the
+ * given array. If the array contains a value within the inclusive range
+ * defined by (value - tolerance) to (value + tolerance).</p>
+ *
+ * <p>The method returns <code>false</code> if a <code>null</code> array
+ * is passed in.</p>
+ *
+ * @param array the array to search
+ * @param valueToFind the value to find
+ * @param tolerance the array contains the tolerance of the search.
+ */
+ public static boolean contains(final double[] array, final double valueToFind, final double tolerance) {
+ return (indexOf(array, valueToFind, 0, tolerance) != -1);
+ }
+
// float IndexOf
//-----------------------------------------------------------------------
/**
@@ -1493,7 +1613,7 @@
* <code>-1</code> if not found or <code>null</code> array input
*/
public static int indexOf(final float[] array, final float valueToFind, int startIndex) {
- if (array == null) {
+ if (array == null || array.length == 0) {
return -1;
}
if (startIndex < 0) {
@@ -1536,7 +1656,7 @@
* <code>-1</code> if not found or <code>null</code> array input
*/
public static int lastIndexOf(final float[] array, final float valueToFind, int startIndex) {
- if (array == null) {
+ if (array == null || array.length == 0) {
return -1;
}
if (startIndex < 0) {
@@ -1596,7 +1716,7 @@
* <code>-1</code> if not found or <code>null</code> array input
*/
public static int indexOf(final boolean[] array, final boolean valueToFind, int startIndex) {
- if (array == null) {
+ if (array == null || array.length == 0) {
return -1;
}
if (startIndex < 0) {
@@ -1639,7 +1759,7 @@
* <code>-1</code> if not found or <code>null</code> array input
*/
public static int lastIndexOf(final boolean[] array, final boolean valueToFind, int startIndex) {
- if (array == null) {
+ if (array == null || array.length == 0) {
return -1;
}
if (startIndex < 0) {
1.12 +87 -1 jakarta-commons/lang/src/test/org/apache/commons/lang/ArrayUtilsTest.java
Index: ArrayUtilsTest.java
===================================================================
RCS file: /home/cvs/jakarta-commons/lang/src/test/org/apache/commons/lang/ArrayUtilsTest.java,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -r1.11 -r1.12
--- ArrayUtilsTest.java 31 Jul 2003 22:31:12 -0000 1.11
+++ ArrayUtilsTest.java 3 Aug 2003 23:29:19 -0000 1.12
@@ -70,6 +70,7 @@
* @author Moritz Petersen
* @author Nikolay Metchev
* @author Matthew Hawthorne
+ * @author Tim O'Brien
* @version $Id$
*/
public class ArrayUtilsTest extends TestCase {
@@ -973,6 +974,8 @@
public void testIndexOfDouble() {
double[] array = null;
assertEquals(-1, ArrayUtils.indexOf(array, (double) 0));
+ array = new double[0];
+ assertEquals(-1, ArrayUtils.indexOf(array, (double) 0));
array = new double[] { 0, 1, 2, 3, 0 };
assertEquals(0, ArrayUtils.indexOf(array, (double) 0));
assertEquals(1, ArrayUtils.indexOf(array, (double) 1));
@@ -982,9 +985,23 @@
assertEquals(-1, ArrayUtils.indexOf(array, (double) 99));
}
+ public void testIndexOfDoubleTolerance() {
+ double[] array = null;
+ assertEquals(-1, ArrayUtils.indexOf(array, (double) 0, (double) 0));
+ array = new double[0];
+ assertEquals(-1, ArrayUtils.indexOf(array, (double) 0, (double) 0));
+ array = new double[] { 0, 1, 2, 3, 0 };
+ assertEquals(0, ArrayUtils.indexOf(array, (double) 0, (double) 0.3));
+ assertEquals(2, ArrayUtils.indexOf(array, (double) 2.2, (double) 0.35));
+ assertEquals(3, ArrayUtils.indexOf(array, (double) 4.15, (double) 2.0));
+ assertEquals(1, ArrayUtils.indexOf(array, (double) 1.00001324, (double) 0.0001));
+ }
+
public void testIndexOfDoubleWithStartIndex() {
double[] array = null;
assertEquals(-1, ArrayUtils.indexOf(array, (double) 0, 2));
+ array = new double[0];
+ assertEquals(-1, ArrayUtils.indexOf(array, (double) 0, 2));
array = new double[] { 0, 1, 2, 3, 0 };
assertEquals(4, ArrayUtils.indexOf(array, (double) 0, 2));
assertEquals(-1, ArrayUtils.indexOf(array, (double) 1, 2));
@@ -993,10 +1010,26 @@
assertEquals(-1, ArrayUtils.indexOf(array, (double) 99, 0));
assertEquals(-1, ArrayUtils.indexOf(array, (double) 0, 6));
}
+
+ public void testIndexOfDoubleWithStartIndexTolerance() {
+ double[] array = null;
+ assertEquals(-1, ArrayUtils.indexOf(array, (double) 0, 2, (double) 0));
+ array = new double[0];
+ assertEquals(-1, ArrayUtils.indexOf(array, (double) 0, 2, (double) 0));
+ array = new double[] { 0, 1, 2, 3, 0 };
+ assertEquals(-1, ArrayUtils.indexOf(array, (double) 0, 99, (double) 0.3));
+ assertEquals(0, ArrayUtils.indexOf(array, (double) 0, 0, (double) 0.3));
+ assertEquals(4, ArrayUtils.indexOf(array, (double) 0, 3, (double) 0.3));
+ assertEquals(2, ArrayUtils.indexOf(array, (double) 2.2, 0, (double) 0.35));
+ assertEquals(3, ArrayUtils.indexOf(array, (double) 4.15, 0, (double) 2.0));
+ assertEquals(1, ArrayUtils.indexOf(array, (double) 1.00001324, 0, (double) 0.0001));
+ }
public void testLastIndexOfDouble() {
double[] array = null;
assertEquals(-1, ArrayUtils.lastIndexOf(array, (double) 0));
+ array = new double[0];
+ assertEquals(-1, ArrayUtils.lastIndexOf(array, (double) 0));
array = new double[] { 0, 1, 2, 3, 0 };
assertEquals(4, ArrayUtils.lastIndexOf(array, (double) 0));
assertEquals(1, ArrayUtils.lastIndexOf(array, (double) 1));
@@ -1005,9 +1038,23 @@
assertEquals(-1, ArrayUtils.lastIndexOf(array, (double) 99));
}
+ public void testLastIndexOfDoubleTolerance() {
+ double[] array = null;
+ assertEquals(-1, ArrayUtils.lastIndexOf(array, (double) 0, (double) 0));
+ array = new double[0];
+ assertEquals(-1, ArrayUtils.lastIndexOf(array, (double) 0, (double) 0));
+ array = new double[] { 0, 1, 2, 3, 0 };
+ assertEquals(4, ArrayUtils.lastIndexOf(array, (double) 0, (double) 0.3));
+ assertEquals(2, ArrayUtils.lastIndexOf(array, (double) 2.2, (double) 0.35));
+ assertEquals(3, ArrayUtils.lastIndexOf(array, (double) 4.15, (double) 2.0));
+ assertEquals(1, ArrayUtils.lastIndexOf(array, (double) 1.00001324, (double) 0.0001));
+ }
+
public void testLastIndexOfDoubleWithStartIndex() {
double[] array = null;
assertEquals(-1, ArrayUtils.lastIndexOf(array, (double) 0, 2));
+ array = new double[0];
+ assertEquals(-1, ArrayUtils.lastIndexOf(array, (double) 0, 2));
array = new double[] { 0, 1, 2, 3, 0 };
assertEquals(0, ArrayUtils.lastIndexOf(array, (double) 0, 2));
assertEquals(1, ArrayUtils.lastIndexOf(array, (double) 1, 2));
@@ -1018,6 +1065,19 @@
assertEquals(4, ArrayUtils.lastIndexOf(array, (double) 0, 88));
}
+ public void testLastIndexOfDoubleWithStartIndexTolerance() {
+ double[] array = null;
+ assertEquals(-1, ArrayUtils.lastIndexOf(array, (double) 0, 2, (double) 0));
+ array = new double[0];
+ assertEquals(-1, ArrayUtils.lastIndexOf(array, (double) 0, 2, (double) 0));
+ array = new double[] { 0, 1, 2, 3, 0 };
+ assertEquals(4, ArrayUtils.lastIndexOf(array, (double) 0, 99, (double) 0.3));
+ assertEquals(0, ArrayUtils.lastIndexOf(array, (double) 0, 3, (double) 0.3));
+ assertEquals(2, ArrayUtils.lastIndexOf(array, (double) 2.2, 3, (double) 0.35));
+ assertEquals(3, ArrayUtils.lastIndexOf(array, (double) 4.15, array.length, (double) 2.0));
+ assertEquals(1, ArrayUtils.lastIndexOf(array, (double) 1.00001324, array.length, (double) 0.0001));
+ }
+
public void testContainsDouble() {
double[] array = null;
assertEquals(false, ArrayUtils.contains(array, (double) 1));
@@ -1028,11 +1088,23 @@
assertEquals(true, ArrayUtils.contains(array, (double) 3));
assertEquals(false, ArrayUtils.contains(array, (double) 99));
}
+
+ public void testContainsDoubleTolerance() {
+ double[] array = null;
+ assertEquals(false, ArrayUtils.contains(array, (double) 1, (double) 0));
+ array = new double[] { 0, 1, 2, 3, 0 };
+ assertEquals(false, ArrayUtils.contains(array, (double) 4.0, (double) 0.33));
+ assertEquals(false, ArrayUtils.contains(array, (double) 2.5, (double) 0.49));
+ assertEquals(true, ArrayUtils.contains(array, (double) 2.5, (double) 0.50));
+ assertEquals(true, ArrayUtils.contains(array, (double) 2.5, (double) 0.51));
+ }
//-----------------------------------------------------------------------
public void testIndexOfFloat() {
float[] array = null;
assertEquals(-1, ArrayUtils.indexOf(array, (float) 0));
+ array = new float[0];
+ assertEquals(-1, ArrayUtils.indexOf(array, (float) 0));
array = new float[] { 0, 1, 2, 3, 0 };
assertEquals(0, ArrayUtils.indexOf(array, (float) 0));
assertEquals(1, ArrayUtils.indexOf(array, (float) 1));
@@ -1044,6 +1116,8 @@
public void testIndexOfFloatWithStartIndex() {
float[] array = null;
assertEquals(-1, ArrayUtils.indexOf(array, (float) 0, 2));
+ array = new float[0];
+ assertEquals(-1, ArrayUtils.indexOf(array, (float) 0, 2));
array = new float[] { 0, 1, 2, 3, 0 };
assertEquals(4, ArrayUtils.indexOf(array, (float) 0, 2));
assertEquals(-1, ArrayUtils.indexOf(array, (float) 1, 2));
@@ -1057,6 +1131,8 @@
public void testLastIndexOfFloat() {
float[] array = null;
assertEquals(-1, ArrayUtils.lastIndexOf(array, (float) 0));
+ array = new float[0];
+ assertEquals(-1, ArrayUtils.lastIndexOf(array, (float) 0));
array = new float[] { 0, 1, 2, 3, 0 };
assertEquals(4, ArrayUtils.lastIndexOf(array, (float) 0));
assertEquals(1, ArrayUtils.lastIndexOf(array, (float) 1));
@@ -1068,6 +1144,8 @@
public void testLastIndexOfFloatWithStartIndex() {
float[] array = null;
assertEquals(-1, ArrayUtils.lastIndexOf(array, (float) 0, 2));
+ array = new float[0];
+ assertEquals(-1, ArrayUtils.lastIndexOf(array, (float) 0, 2));
array = new float[] { 0, 1, 2, 3, 0 };
assertEquals(0, ArrayUtils.lastIndexOf(array, (float) 0, 2));
assertEquals(1, ArrayUtils.lastIndexOf(array, (float) 1, 2));
@@ -1093,6 +1171,8 @@
public void testIndexOfBoolean() {
boolean[] array = null;
assertEquals(-1, ArrayUtils.indexOf(array, true));
+ array = new boolean[0];
+ assertEquals(-1, ArrayUtils.indexOf(array, true));
array = new boolean[] { true, false, true };
assertEquals(0, ArrayUtils.indexOf(array, true));
assertEquals(1, ArrayUtils.indexOf(array, false));
@@ -1103,6 +1183,8 @@
public void testIndexOfBooleanWithStartIndex() {
boolean[] array = null;
assertEquals(-1, ArrayUtils.indexOf(array, true, 2));
+ array = new boolean[0];
+ assertEquals(-1, ArrayUtils.indexOf(array, true, 2));
array = new boolean[] { true, false, true };
assertEquals(2, ArrayUtils.indexOf(array, true, 1));
assertEquals(-1, ArrayUtils.indexOf(array, false, 2));
@@ -1116,6 +1198,8 @@
public void testLastIndexOfBoolean() {
boolean[] array = null;
assertEquals(-1, ArrayUtils.lastIndexOf(array, true));
+ array = new boolean[0];
+ assertEquals(-1, ArrayUtils.lastIndexOf(array, true));
array = new boolean[] { true, false, true };
assertEquals(2, ArrayUtils.lastIndexOf(array, true));
assertEquals(1, ArrayUtils.lastIndexOf(array, false));
@@ -1125,6 +1209,8 @@
public void testLastIndexOfBooleanWithStartIndex() {
boolean[] array = null;
+ assertEquals(-1, ArrayUtils.lastIndexOf(array, true, 2));
+ array = new boolean[0];
assertEquals(-1, ArrayUtils.lastIndexOf(array, true, 2));
array = new boolean[] { true, false, true };
assertEquals(2, ArrayUtils.lastIndexOf(array, true, 2));