@@ 182,6 182,143 @@ public class ArrayUtils {
return false;
}
+ /** @param offset the region offset in the array
+ * @param length the region length in the array
+ * @param otherOffset the region offset in the other array
+ * @param identity if {@code ==} comparison should be used instead of {@link Object#equals(Object) equals(Object)}
+ * @return whether the specified region in both arrays matches
+ * @since 0.13.5 */
+ public static boolean regionEquals(final Object[] array, final int offset, final int length, final Object[] other, final int otherOffset, final boolean identity) {
+ if (identity) {
+ for (int i = offset; i < offset + length; i++) {
+ if (array[i] != other[otherOffset + i - offset]) return false;
+ }
+ } else {
+ for (int i = offset; i < offset + length; i++) {
+ if (!array[i].equals(other[otherOffset + i - offset])) return false;
+ }
+ }
+ return true;
+ }
+
+ /** @see #regionEquals(Object[], int, int, Object[], int, boolean) */
+ public static boolean regionEquals(final Object[] array, final int offset, final int length, final Object[] other, final boolean identity) {
+ return regionEquals(array, offset, length, other, 0, identity);
+ }
+
+ /** @see #regionEquals(Object[], int, int, Object[], boolean) */
+ public static boolean regionEquals(final Object[] array, final int offset, final Object[] other, final boolean identity) {
+ return regionEquals(array, offset, other.length, other, identity);
+ }
+
+ /** @see #regionEquals(Object[], int, int, Object[], int, boolean) */
+ public static boolean regionEquals(final long[] array, final int offset, final int length, final long[] other, final int otherOffset) {
+ for (int i = offset; i < offset + length; i++) {
+ if (array[i] != other[otherOffset + i - offset]) return false;
+ }
+ return true;
+ }
+
+ /** @see #regionEquals(long[], int, int, long[], int) */
+ public static boolean regionEquals(final long[] array, final int offset, final int length, final long[] other) {
+ return regionEquals(array, offset, length, other, 0);
+ }
+
+ /** @see #regionEquals(long[], int, int, long[]) */
+ public static boolean regionEquals(final long[] array, final int offset, final long[] other) {
+ return regionEquals(array, offset, other.length, other);
+ }
+
+ /** @see #regionEquals(Object[], int, int, Object[], int, boolean) */
+ public static boolean regionEquals(final int[] array, final int offset, final int length, final int[] other, final int otherOffset) {
+ for (int i = offset; i < offset + length; i++) {
+ if (array[i] != other[otherOffset + i - offset]) return false;
+ }
+ return true;
+ }
+
+ /** @see #regionEquals(int[], int, int, int[], int) */
+ public static boolean regionEquals(final int[] array, final int offset, final int length, final int[] other) {
+ return regionEquals(array, offset, length, other, 0);
+ }
+
+ /** @see #regionEquals(int[], int, int, int[]) */
+ public static boolean regionEquals(final int[] array, final int offset, final int[] other) {
+ return regionEquals(array, offset, other.length, other);
+ }
+
+ /** @see #regionEquals(Object[], int, int, Object[], int, boolean) */
+ public static boolean regionEquals(final double[] array, final int offset, final int length, final double[] other, final int otherOffset, final double epsilon) {
+ for (int i = offset; i < offset + length; i++) {
+ if (Math.abs(array[i] - other[otherOffset + i - offset]) > epsilon) return false;
+ }
+ return true;
+ }
+
+ /** @see #regionEquals(double[], int, int, double[], int, double) */
+ public static boolean regionEquals(final double[] array, final int offset, final int length, final double[] other, final double epsilon) {
+ return regionEquals(array, offset, length, other, 0, epsilon);
+ }
+
+ /** @see #regionEquals(double[], int, int, double[], double) */
+ public static boolean regionEquals(final double[] array, final int offset, final double[] other, final double epsilon) {
+ return regionEquals(array, offset, other.length, other, epsilon);
+ }
+
+ /** @see #regionEquals(Object[], int, int, Object[], int, boolean) */
+ public static boolean regionEquals(final float[] array, final int offset, final int length, final float[] other, final int otherOffset, final float epsilon) {
+ for (int i = offset; i < offset + length; i++) {
+ if (Math.abs(array[i] - other[otherOffset + i - offset]) > epsilon) return false;
+ }
+ return true;
+ }
+
+ /** @see #regionEquals(float[], int, int, float[], int, float) */
+ public static boolean regionEquals(final float[] array, final int offset, final int length, final float[] other, final float epsilon) {
+ return regionEquals(array, offset, length, other, 0, epsilon);
+ }
+
+ /** @see #regionEquals(float[], int, int, float[], float) */
+ public static boolean regionEquals(final float[] array, final int offset, final float[] other, final float epsilon) {
+ return regionEquals(array, offset, other.length, other, epsilon);
+ }
+
+ /** @see #regionEquals(Object[], int, int, Object[], int, boolean) */
+ public static boolean regionEquals(final short[] array, final int offset, final int length, final short[] other, final int otherOffset) {
+ for (int i = offset; i < offset + length; i++) {
+ if (array[i] != other[otherOffset + i - offset]) return false;
+ }
+ return true;
+ }
+
+ /** @see #regionEquals(short[], int, int, short[], int) */
+ public static boolean regionEquals(final short[] array, final int offset, final int length, final short[] other) {
+ return regionEquals(array, offset, length, other, 0);
+ }
+
+ /** @see #regionEquals(short[], int, int, short[]) */
+ public static boolean regionEquals(final short[] array, final int offset, final short[] other) {
+ return regionEquals(array, offset, other.length, other);
+ }
+
+ /** @see #regionEquals(Object[], int, int, Object[], int, boolean) */
+ public static boolean regionEquals(final byte[] array, final int offset, final int length, final byte[] other, final int otherOffset) {
+ for (int i = offset; i < offset + length; i++) {
+ if (array[i] != other[otherOffset + i - offset]) return false;
+ }
+ return true;
+ }
+
+ /** @see #regionEquals(byte[], int, int, byte[], int) */
+ public static boolean regionEquals(final byte[] array, final int offset, final int length, final byte[] other) {
+ return regionEquals(array, offset, length, other, 0);
+ }
+
+ /** @see #regionEquals(byte[], int, int, byte[]) */
+ public static boolean regionEquals(final byte[] array, final int offset, final byte[] other) {
+ return regionEquals(array, offset, other.length, other);
+ }
+
/** @param obj the object to compare
* @param array the array which items to compare
* @return if the given object equals any of the items in the given array */
@@ 72,6 72,48 @@ public class ArrayUtilsTest {
}
@Test
+ public void regionEquals() {
+ assertTrue(ArrayUtils.regionEquals(new Object[]{"1", "2", "3"}, 0, new Object[]{"1", "2", new String(new char[]{'3'})}, false));
+ assertFalse(ArrayUtils.regionEquals(new Object[]{"1", "2", "3"}, 0, new Object[]{"1", "2", new String(new char[]{'3'})}, true));
+ assertTrue(ArrayUtils.regionEquals(new Object[]{"1", "2", "3", "4", "5"}, 2, 3, new Object[]{"5", "4", "3", "4", "5", "0"}, 2, false));
+ assertFalse(ArrayUtils.regionEquals(new Object[]{"1", "2", "3", "4", "5"}, 2, 3, new Object[]{"5", "4", "3", "4", "5", "0"}, 1, false));
+
+ assertTrue(ArrayUtils.regionEquals(new long[]{1, 2, 3}, 0, new long[]{1, 2, 3}));
+ assertFalse(ArrayUtils.regionEquals(new long[]{1, 2, 3}, 0, new long[]{0, 2, 3}));
+ assertTrue(ArrayUtils.regionEquals(new long[]{1, 2, 3, 4, 5}, 2, 3, new long[]{2, 3, 4, 5}, 1));
+ assertFalse(ArrayUtils.regionEquals(new long[]{1, 2, 3, 4, 5}, 2, 3, new long[]{2, 3, 4, 5}, 0));
+
+ assertTrue(ArrayUtils.regionEquals(new int[]{1, 2, 3}, 0, new int[]{1, 2, 3}));
+ assertFalse(ArrayUtils.regionEquals(new int[]{1, 2, 3}, 0, new int[]{0, 2, 3}));
+ assertTrue(ArrayUtils.regionEquals(new int[]{1, 2, 3, 4, 5}, 2, 3, new int[]{2, 3, 4, 5}, 1));
+ assertFalse(ArrayUtils.regionEquals(new int[]{1, 2, 3, 4, 5}, 2, 3, new int[]{2, 3, 4, 5}, 0));
+
+ assertTrue(ArrayUtils.regionEquals(new short[]{1, 2, 3}, 0, new short[]{1, 2, 3}));
+ assertFalse(ArrayUtils.regionEquals(new short[]{1, 2, 3}, 0, new short[]{0, 2, 3}));
+ assertTrue(ArrayUtils.regionEquals(new short[]{1, 2, 3, 4, 5}, 2, 3, new short[]{2, 3, 4, 5}, 1));
+ assertFalse(ArrayUtils.regionEquals(new short[]{1, 2, 3, 4, 5}, 2, 3, new short[]{2, 3, 4, 5}, 0));
+
+ assertTrue(ArrayUtils.regionEquals(new byte[]{1, 2, 3}, 0, new byte[]{1, 2, 3}));
+ assertFalse(ArrayUtils.regionEquals(new byte[]{1, 2, 3}, 0, new byte[]{0, 2, 3}));
+ assertTrue(ArrayUtils.regionEquals(new byte[]{1, 2, 3, 4, 5}, 2, 3, new byte[]{2, 3, 4, 5}, 1));
+ assertFalse(ArrayUtils.regionEquals(new byte[]{1, 2, 3, 4, 5}, 2, 3, new byte[]{2, 3, 4, 5}, 0));
+
+ assertTrue(ArrayUtils.regionEquals(new double[]{1, 2, 3}, 0, new double[]{1, 2, 3}, 0));
+ assertFalse(ArrayUtils.regionEquals(new double[]{1, 2, 3}, 0, new double[]{0, 2, 3}, 0));
+ assertTrue(ArrayUtils.regionEquals(new double[]{1, 2, 3, 4, 5}, 2, 3, new double[]{2, 3, 4, 5}, 1, 0));
+ assertFalse(ArrayUtils.regionEquals(new double[]{1, 2, 3, 4, 5}, 2, 3, new double[]{2, 3, 4, 5}, 0, 0));
+ assertTrue(ArrayUtils.regionEquals(new double[]{0, 1.05, 1.1, 2}, 1, 3, new double[]{1, 1.03, 1.11, 2.05}, 1, .05));
+ assertFalse(ArrayUtils.regionEquals(new double[]{0, 1.05, 1.1, 2}, 1, 3, new double[]{1, 1.03, 1.11, 2.05}, 1, .049));
+
+ assertTrue(ArrayUtils.regionEquals(new float[]{1, 2, 3}, 0, new float[]{1, 2, 3}, 0));
+ assertFalse(ArrayUtils.regionEquals(new float[]{1, 2, 3}, 0, new float[]{0, 2, 3}, 0));
+ assertTrue(ArrayUtils.regionEquals(new float[]{1, 2, 3, 4, 5}, 2, 3, new float[]{2, 3, 4, 5}, 1, 0));
+ assertFalse(ArrayUtils.regionEquals(new float[]{1, 2, 3, 4, 5}, 2, 3, new float[]{2, 3, 4, 5}, 0, 0));
+ assertTrue(ArrayUtils.regionEquals(new float[]{0, 1.05f, 1.1f, 2}, 1, 3, new float[]{1, 1.03f, 1.11f, 2.05f}, 1, .05f));
+ assertFalse(ArrayUtils.regionEquals(new float[]{0, 1.05f, 1.1f, 2}, 1, 3, new float[]{1, 1.03f, 1.11f, 2.05f}, 1, .049f));
+ }
+
+ @Test
public void shift() {
String[] array = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
ArrayUtils.shift(array, 0, array.length, -1);