implement Arrays.deepEquals and Objects.deepEquals

This commit is contained in:
Joshua Warner 2014-01-30 17:12:34 -07:00
parent 9f505f8522
commit 02becdb5bf
3 changed files with 189 additions and 13 deletions

View File

@ -305,6 +305,132 @@ public class Arrays {
return true;
}
public static boolean equals(int[] a, int[] b) {
if(a == b) {
return true;
}
if(a == null || b == null) {
return false;
}
if(a.length != b.length) {
return false;
}
for(int i = 0; i < a.length; i++) {
if(a[i] != b[i]) {
return false;
}
}
return true;
}
public static boolean equals(long[] a, long[] b) {
if(a == b) {
return true;
}
if(a == null || b == null) {
return false;
}
if(a.length != b.length) {
return false;
}
for(int i = 0; i < a.length; i++) {
if(a[i] != b[i]) {
return false;
}
}
return true;
}
public static boolean equals(short[] a, short[] b) {
if(a == b) {
return true;
}
if(a == null || b == null) {
return false;
}
if(a.length != b.length) {
return false;
}
for(int i = 0; i < a.length; i++) {
if(a[i] != b[i]) {
return false;
}
}
return true;
}
public static boolean equals(char[] a, char[] b) {
if(a == b) {
return true;
}
if(a == null || b == null) {
return false;
}
if(a.length != b.length) {
return false;
}
for(int i = 0; i < a.length; i++) {
if(a[i] != b[i]) {
return false;
}
}
return true;
}
public static boolean equals(float[] a, float[] b) {
if(a == b) {
return true;
}
if(a == null || b == null) {
return false;
}
if(a.length != b.length) {
return false;
}
for(int i = 0; i < a.length; i++) {
if(a[i] != b[i]) {
return false;
}
}
return true;
}
public static boolean equals(double[] a, double[] b) {
if(a == b) {
return true;
}
if(a == null || b == null) {
return false;
}
if(a.length != b.length) {
return false;
}
for(int i = 0; i < a.length; i++) {
if(a[i] != b[i]) {
return false;
}
}
return true;
}
public static boolean deepEquals(Object[] a, Object[] b) {
if(a == b) {
return true;
}
if(a == null || b == null) {
return false;
}
if(a.length != b.length) {
return false;
}
for(int i = 0; i < a.length; i++) {
if(!Objects.deepEquals(a[i], b[i])) {
return false;
}
}
return true;
}
public static <T> List<T> asList(final T ... array) {
return new AbstractList<T>() {
@Override

View File

@ -17,7 +17,25 @@ public final class Objects {
return true;
if (x == null || y == null)
return false;
throw new UnsupportedOperationException("deepEquals is not implemented yet.");
if (x.getClass().isArray()) {
if(x instanceof Object[] && y instanceof Object[])
return Arrays.deepEquals((Object[])x, (Object[])y);
if(x instanceof byte[] && y instanceof byte[])
return Arrays.equals((byte[]) x, (byte[]) y);
if(x instanceof int[] && y instanceof int[])
return Arrays.equals((int[]) x, (int[]) y);
if(x instanceof long[] && y instanceof long[])
return Arrays.equals((long[]) x, (long[]) y);
if(x instanceof short[] && y instanceof short[])
return Arrays.equals((short[]) x, (short[]) y);
if(x instanceof char[] && y instanceof char[])
return Arrays.equals((char[]) x, (char[]) y);
if(x instanceof float[] && y instanceof float[])
return Arrays.equals((float[]) x, (float[]) y);
if(x instanceof double[] && y instanceof double[])
return Arrays.equals((double[]) x, (double[]) y);
}
return x.equals(y);
}
public static boolean equals(final Object x, final Object y) {

View File

@ -1,4 +1,6 @@
public class Arrays {
import java.util.Arrays;
public class ArraysTest {
private static void expect(boolean v) {
if (! v) throw new RuntimeException();
}
@ -33,7 +35,7 @@ public class Arrays {
int random = 12345;
for (int i = 0; i < 32; ++i) {
random = shuffle(array, random);
java.util.Arrays.sort(array);
Arrays.sort(array);
expectSorted(array);
}
}
@ -116,27 +118,57 @@ public class Arrays {
{ Object[] a = new Object[3];
Object[] b = new Object[3];
expect(java.util.Arrays.equals(a, b));
expect(Arrays.equals(a, b));
a[0] = new Object();
expect(! java.util.Arrays.equals(a, b));
expect(! java.util.Arrays.equals(b, new Object[4]));
expect(! java.util.Arrays.equals(a, null));
expect(! java.util.Arrays.equals(null, b));
expect(java.util.Arrays.equals((Object[])null, (Object[])null));
expect(! Arrays.equals(a, b));
expect(! Arrays.equals(b, new Object[4]));
expect(! Arrays.equals(a, null));
expect(! Arrays.equals(null, b));
expect(Arrays.equals((Object[])null, (Object[])null));
b[0] = a[0];
expect(java.util.Arrays.equals(a, b));
expect(Arrays.equals(a, b));
java.util.Arrays.hashCode(a);
java.util.Arrays.hashCode((Object[])null);
Arrays.hashCode(a);
Arrays.hashCode((Object[])null);
}
{ String[] list = new String[] { "Hello", "World", "!" };
Object[] result = java.util.Arrays.copyOf(list, 2, Object[].class);
Object[] result = Arrays.copyOf(list, 2, Object[].class);
expect(list[1] == result[1]);
expect(result.length == 2);
expect(result.getClass().getComponentType() == Object.class);
}
{ Object[] a = new Object[3];
Object[] b = new Object[3];
expect(Arrays.deepEquals(a, b));
a[0] = new Object();
expect(! Arrays.deepEquals(a, b));
expect(! Arrays.deepEquals(b, new Object[4]));
expect(! Arrays.deepEquals(a, null));
expect(! Arrays.deepEquals(null, b));
expect(Arrays.deepEquals((Object[])null, (Object[])null));
b[0] = a[0];
expect(Arrays.deepEquals(a, b));
a[0] = new Object[] {1};
expect(! Arrays.deepEquals(a, b));
b[0] = new Object[] {1};
expect(Arrays.deepEquals(a, b));
((Object[])a[0])[0] = (Long)1L;
expect(! Arrays.deepEquals(a, b));
a[0] = new Integer[] {1};
expect(Arrays.deepEquals(a, b));
a[0] = new int[] {1};
expect(! Arrays.deepEquals(a, b));
b[0] = new int[] {1};
expect(Arrays.deepEquals(a, b));
}
testSort();
}
}