Add tolerance checking to indexOf methods

bug 22091, from Tim O'Brien


git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137561 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2003-08-03 23:29:19 +00:00
parent 3401226d40
commit d35d966e65
2 changed files with 214 additions and 8 deletions

View File

@ -75,8 +75,9 @@ import org.apache.commons.lang.builder.ToStringStyle;
* @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: ArrayUtils.java,v 1.21 2003/08/01 20:45:17 scolebourne Exp $
* @version $Id: ArrayUtils.java,v 1.22 2003/08/03 23:29:19 scolebourne Exp $
*/
public class ArrayUtils {
@ -1375,6 +1376,23 @@ public class ArrayUtils {
return indexOf(array, valueToFind, 0);
}
/**
* <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>
*
@ -1390,7 +1408,7 @@ public class ArrayUtils {
* <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) {
@ -1404,6 +1422,40 @@ public class ArrayUtils {
return -1;
}
/**
* <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>
*
@ -1418,6 +1470,23 @@ public class ArrayUtils {
return lastIndexOf(array, valueToFind, Integer.MAX_VALUE);
}
/**
* <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>
*
@ -1433,7 +1502,7 @@ public class ArrayUtils {
* <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) {
@ -1449,6 +1518,41 @@ public class ArrayUtils {
return -1;
}
/**
* <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>
*
@ -1462,6 +1566,22 @@ public class ArrayUtils {
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 @@ public class ArrayUtils {
* <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 @@ public class ArrayUtils {
* <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 @@ public class ArrayUtils {
* <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 @@ public class ArrayUtils {
* <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) {

View File

@ -70,7 +70,8 @@ import junit.textui.TestRunner;
* @author Moritz Petersen
* @author Nikolay Metchev
* @author Matthew Hawthorne
* @version $Id: ArrayUtilsTest.java,v 1.11 2003/07/31 22:31:12 scolebourne Exp $
* @author Tim O'Brien
* @version $Id: ArrayUtilsTest.java,v 1.12 2003/08/03 23:29:19 scolebourne Exp $
*/
public class ArrayUtilsTest extends TestCase {
@ -973,6 +974,8 @@ public class ArrayUtilsTest extends TestCase {
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 @@ public class ArrayUtilsTest extends TestCase {
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 @@ public class ArrayUtilsTest extends TestCase {
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 @@ public class ArrayUtilsTest extends TestCase {
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 @@ public class ArrayUtilsTest extends TestCase {
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 @@ public class ArrayUtilsTest extends TestCase {
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 class ArrayUtilsTest extends TestCase {
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 class ArrayUtilsTest extends TestCase {
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 class ArrayUtilsTest extends TestCase {
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 class ArrayUtilsTest extends TestCase {
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 class ArrayUtilsTest extends TestCase {
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 class ArrayUtilsTest extends TestCase {
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));
@ -1126,6 +1210,8 @@ public class ArrayUtilsTest extends TestCase {
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));
assertEquals(0, ArrayUtils.lastIndexOf(array, true, 1));