From 02becdb5bff57f374f3ce3732ea2a8a38523492a Mon Sep 17 00:00:00 2001 From: Joshua Warner Date: Thu, 30 Jan 2014 17:12:34 -0700 Subject: [PATCH] implement Arrays.deepEquals and Objects.deepEquals --- classpath/java/util/Arrays.java | 126 ++++++++++++++++++++++++++ classpath/java/util/Objects.java | 20 +++- test/{Arrays.java => ArraysTest.java} | 56 +++++++++--- 3 files changed, 189 insertions(+), 13 deletions(-) rename test/{Arrays.java => ArraysTest.java} (69%) diff --git a/classpath/java/util/Arrays.java b/classpath/java/util/Arrays.java index 15b6ec20ae..59640ba8a9 100644 --- a/classpath/java/util/Arrays.java +++ b/classpath/java/util/Arrays.java @@ -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 List asList(final T ... array) { return new AbstractList() { @Override diff --git a/classpath/java/util/Objects.java b/classpath/java/util/Objects.java index 586ee916bb..e082b9c31f 100644 --- a/classpath/java/util/Objects.java +++ b/classpath/java/util/Objects.java @@ -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) { diff --git a/test/Arrays.java b/test/ArraysTest.java similarity index 69% rename from test/Arrays.java rename to test/ArraysTest.java index 36df4a5dc5..cd008065b6 100644 --- a/test/Arrays.java +++ b/test/ArraysTest.java @@ -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(); } }