1/*
2 * Copyright (c) 2007 Mockito contributors
3 * This program is made available under the terms of the MIT License.
4 */
5
6package org.mockito.internal.matchers;
7
8import java.lang.reflect.Array;
9import java.util.Arrays;
10
11import org.hamcrest.Description;
12
13public class ArrayEquals extends Equals {
14
15    private static final long serialVersionUID = -7167812844261087583L;
16
17    public ArrayEquals(Object wanted) {
18        super(wanted);
19    }
20
21    public boolean matches(Object actual) {
22        Object wanted = getWanted();
23        if (wanted == null || actual == null) {
24            return super.matches(actual);
25        } else if (wanted instanceof boolean[] && actual instanceof boolean[]) {
26            return Arrays.equals((boolean[]) wanted, (boolean[]) actual);
27        } else if (wanted instanceof byte[] && actual instanceof byte[]) {
28            return Arrays.equals((byte[]) wanted, (byte[]) actual);
29        } else if (wanted instanceof char[] && actual instanceof char[]) {
30            return Arrays.equals((char[]) wanted, (char[]) actual);
31        } else if (wanted instanceof double[] && actual instanceof double[]) {
32            return Arrays.equals((double[]) wanted, (double[]) actual);
33        } else if (wanted instanceof float[] && actual instanceof float[]) {
34            return Arrays.equals((float[]) wanted, (float[]) actual);
35        } else if (wanted instanceof int[] && actual instanceof int[]) {
36            return Arrays.equals((int[]) wanted, (int[]) actual);
37        } else if (wanted instanceof long[] && actual instanceof long[]) {
38            return Arrays.equals((long[]) wanted, (long[]) actual);
39        } else if (wanted instanceof short[] && actual instanceof short[]) {
40            return Arrays.equals((short[]) wanted, (short[]) actual);
41        } else if (wanted instanceof Object[] && actual instanceof Object[]) {
42            return Arrays.equals((Object[]) wanted, (Object[]) actual);
43        }
44        return false;
45    }
46
47    public void describeTo(Description description) {
48        if (getWanted() != null && getWanted().getClass().isArray()) {
49            appendArray(createObjectArray(getWanted()), description);
50        } else {
51            super.describeTo(description);
52        }
53    }
54
55    private void appendArray(Object[] array, Description description) {
56        description.appendText("[");
57        for (int i = 0; i < array.length; i++) {
58            new Equals(array[i]).describeTo(description);
59            if (i != array.length - 1) {
60                description.appendText(", ");
61            }
62        }
63        description.appendText("]");
64    }
65
66    public static Object[] createObjectArray(Object array) {
67        if (array instanceof Object[]) {
68            return (Object[]) array;
69        }
70        Object[] result = new Object[Array.getLength(array)];
71        for (int i = 0; i < Array.getLength(array); i++) {
72            result[i] = Array.get(array, i);
73        }
74        return result;
75    }
76}
77