scolebourne 2002/11/24 08:50:38
Modified: lang/src/java/org/apache/commons/lang ArrayUtils.java
lang/src/test/org/apache/commons/lang ArrayUtilsTest.java
Log:
Add reverse to ArrayUtils
Revision Changes Path
1.6 +210 -1
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.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- ArrayUtils.java 16 Nov 2002 12:56:43 -0000 1.5
+++ ArrayUtils.java 24 Nov 2002 16:50:38 -0000 1.6
@@ -679,4 +679,213 @@
return array1.getClass().getName().equals(array2.getClass().getName());
}
+ /**
+ * Reverses the order of the given array.
+ * <p>
+ * There is no special handling for multi-dimensional arrays.
+ * <p>
+ * The method does nothing if <code>null</code> is passed in.
+ *
+ * @param array the array to reverse
+ */
+ public static void reverse(Object[] array) {
+ if (array == null) {
+ return;
+ }
+ int i = 0;
+ int j = array.length - 1;
+ Object tmp;
+ while (j > i) {
+ tmp = array[j];
+ array[j] = array[i];
+ array[i] = tmp;
+ j--;
+ i++;
+ }
+ }
+
+ /**
+ * Reverses the order of the given array.
+ * <p>
+ * The method does nothing if <code>null</code> is passed in.
+ *
+ * @param array the array to reverse
+ */
+ public static void reverse(long[] array) {
+ if (array == null) {
+ return;
+ }
+ int i = 0;
+ int j = array.length - 1;
+ long tmp;
+ while (j > i) {
+ tmp = array[j];
+ array[j] = array[i];
+ array[i] = tmp;
+ j--;
+ i++;
+ }
+ }
+
+ /**
+ * Reverses the order of the given array.
+ * <p>
+ * The method does nothing if <code>null</code> is passed in.
+ *
+ * @param array the array to reverse
+ */
+ public static void reverse(int[] array) {
+ if (array == null) {
+ return;
+ }
+ int i = 0;
+ int j = array.length - 1;
+ int tmp;
+ while (j > i) {
+ tmp = array[j];
+ array[j] = array[i];
+ array[i] = tmp;
+ j--;
+ i++;
+ }
+ }
+
+ /**
+ * Reverses the order of the given array.
+ * <p>
+ * There is no special handling for multi-dimensional arrays.
+ *
+ * @param array the array to reverse
+ */
+ public static void reverse(short[] array) {
+ if (array == null) {
+ return;
+ }
+ int i = 0;
+ int j = array.length - 1;
+ short tmp;
+ while (j > i) {
+ tmp = array[j];
+ array[j] = array[i];
+ array[i] = tmp;
+ j--;
+ i++;
+ }
+ }
+
+ /**
+ * Reverses the order of the given array.
+ * <p>
+ * The method does nothing if <code>null</code> is passed in.
+ *
+ * @param array the array to reverse
+ */
+ public static void reverse(char[] array) {
+ if (array == null) {
+ return;
+ }
+ int i = 0;
+ int j = array.length - 1;
+ char tmp;
+ while (j > i) {
+ tmp = array[j];
+ array[j] = array[i];
+ array[i] = tmp;
+ j--;
+ i++;
+ }
+ }
+
+ /**
+ * Reverses the order of the given array.
+ * <p>
+ * The method does nothing if <code>null</code> is passed in.
+ *
+ * @param array the array to reverse
+ */
+ public static void reverse(byte[] array) {
+ if (array == null) {
+ return;
+ }
+ int i = 0;
+ int j = array.length - 1;
+ byte tmp;
+ while (j > i) {
+ tmp = array[j];
+ array[j] = array[i];
+ array[i] = tmp;
+ j--;
+ i++;
+ }
+ }
+
+ /**
+ * Reverses the order of the given array.
+ * <p>
+ * The method does nothing if <code>null</code> is passed in.
+ *
+ * @param array the array to reverse
+ */
+ public static void reverse(double[] array) {
+ if (array == null) {
+ return;
+ }
+ int i = 0;
+ int j = array.length - 1;
+ double tmp;
+ while (j > i) {
+ tmp = array[j];
+ array[j] = array[i];
+ array[i] = tmp;
+ j--;
+ i++;
+ }
+ }
+
+ /**
+ * Reverses the order of the given array.
+ * <p>
+ * The method does nothing if <code>null</code> is passed in.
+ *
+ * @param array the array to reverse
+ */
+ public static void reverse(float[] array) {
+ if (array == null) {
+ return;
+ }
+ int i = 0;
+ int j = array.length - 1;
+ float tmp;
+ while (j > i) {
+ tmp = array[j];
+ array[j] = array[i];
+ array[i] = tmp;
+ j--;
+ i++;
+ }
+ }
+
+ /**
+ * Reverses the order of the given array.
+ * <p>
+ * The method does nothing if <code>null</code> is passed in.
+ *
+ * @param array the array to reverse
+ */
+ public static void reverse(boolean[] array) {
+ if (array == null) {
+ return;
+ }
+ int i = 0;
+ int j = array.length - 1;
+ boolean tmp;
+ while (j > i) {
+ tmp = array[j];
+ array[j] = array[i];
+ array[i] = tmp;
+ j--;
+ i++;
+ }
+ }
+
}
1.3 +122 -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.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- ArrayUtilsTest.java 16 Nov 2002 12:56:44 -0000 1.2
+++ ArrayUtilsTest.java 24 Nov 2002 16:50:38 -0000 1.3
@@ -517,4 +517,125 @@
assertEquals(false, ArrayUtils.isSameType(new String[0][0], new String[0]));
}
+ //-----------------------------------------------------------------------
+ public void testReverse() {
+ StringBuffer str1 = new StringBuffer("pick");
+ String str2 = "a";
+ String[] str3 = new String[] {"stick"};
+ String str4 = "up";
+
+ Object[] array = new Object[] {str1, str2, str3};
+ ArrayUtils.reverse(array);
+ assertEquals(array[0], str3);
+ assertEquals(array[1], str2);
+ assertEquals(array[2], str1);
+
+ array = new Object[] {str1, str2, str3, str4};
+ ArrayUtils.reverse(array);
+ assertEquals(array[0], str4);
+ assertEquals(array[1], str3);
+ assertEquals(array[2], str2);
+ assertEquals(array[3], str1);
+
+ array = null;
+ ArrayUtils.reverse(array);
+ assertEquals(null, array);
+ }
+
+ public void testReverseLong() {
+ long[] array = new long[] {1L, 2L, 3L};
+ ArrayUtils.reverse(array);
+ assertEquals(array[0], 3L);
+ assertEquals(array[1], 2L);
+ assertEquals(array[2], 1L);
+
+ array = null;
+ ArrayUtils.reverse(array);
+ assertEquals(null, array);
+ }
+
+ public void testReverseInt() {
+ int[] array = new int[] {1, 2, 3};
+ ArrayUtils.reverse(array);
+ assertEquals(array[0], 3);
+ assertEquals(array[1], 2);
+ assertEquals(array[2], 1);
+
+ array = null;
+ ArrayUtils.reverse(array);
+ assertEquals(null, array);
+ }
+
+ public void testReverseShort() {
+ short[] array = new short[] {1, 2, 3};
+ ArrayUtils.reverse(array);
+ assertEquals(array[0], 3);
+ assertEquals(array[1], 2);
+ assertEquals(array[2], 1);
+
+ array = null;
+ ArrayUtils.reverse(array);
+ assertEquals(null, array);
+ }
+
+ public void testReverseChar() {
+ char[] array = new char[] {'a', 'f', 'C'};
+ ArrayUtils.reverse(array);
+ assertEquals(array[0], 'C');
+ assertEquals(array[1], 'f');
+ assertEquals(array[2], 'a');
+
+ array = null;
+ ArrayUtils.reverse(array);
+ assertEquals(null, array);
+ }
+
+ public void testReverseByte() {
+ byte[] array = new byte[] {2, 3, 4};
+ ArrayUtils.reverse(array);
+ assertEquals(array[0], 4);
+ assertEquals(array[1], 3);
+ assertEquals(array[2], 2);
+
+ array = null;
+ ArrayUtils.reverse(array);
+ assertEquals(null, array);
+ }
+
+ public void testReverseDouble() {
+ double[] array = new double[] {0.3d, 0.4d, 0.5d};
+ ArrayUtils.reverse(array);
+ assertEquals(array[0], 0.5d, 0.0d);
+ assertEquals(array[1], 0.4d, 0.0d);
+ assertEquals(array[2], 0.3d, 0.0d);
+
+ array = null;
+ ArrayUtils.reverse(array);
+ assertEquals(null, array);
+ }
+
+ public void testReverseFloat() {
+ float[] array = new float[] {0.3f, 0.4f, 0.5f};
+ ArrayUtils.reverse(array);
+ assertEquals(array[0], 0.5f, 0.0f);
+ assertEquals(array[1], 0.4f, 0.0f);
+ assertEquals(array[2], 0.3f, 0.0f);
+
+ array = null;
+ ArrayUtils.reverse(array);
+ assertEquals(null, array);
+ }
+
+ public void testReverseBoolean() {
+ boolean[] array = new boolean[] {false, false, true};
+ ArrayUtils.reverse(array);
+ assertEquals(array[0], true);
+ assertEquals(array[1], false);
+ assertEquals(array[2], false);
+
+ array = null;
+ ArrayUtils.reverse(array);
+ assertEquals(null, array);
+ }
+
}
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>