1/*
2 * Copyright (c) 2007 Mockito contributors
3 * This program is made available under the terms of the MIT License.
4 */
5package org.mockito.internal.matchers;
6
7import java.lang.reflect.Array;
8
9//stolen from hamcrest because I didn't want to have more dependency than Matcher class
10public class Equality {
11
12    public static boolean areEqual(Object o1, Object o2) {
13        if (o1 == null || o2 == null) {
14            return o1 == null && o2 == null;
15        } else if (isArray(o1)) {
16            return isArray(o2) && areArraysEqual(o1, o2);
17        } else {
18            return o1.equals(o2);
19        }
20    }
21
22    static boolean areArraysEqual(Object o1, Object o2) {
23        return areArrayLengthsEqual(o1, o2)
24                && areArrayElementsEqual(o1, o2);
25    }
26
27    static boolean areArrayLengthsEqual(Object o1, Object o2) {
28        return Array.getLength(o1) == Array.getLength(o2);
29    }
30
31    static boolean areArrayElementsEqual(Object o1, Object o2) {
32        for (int i = 0; i < Array.getLength(o1); i++) {
33            if (!areEqual(Array.get(o1, i), Array.get(o2, i))) return false;
34        }
35        return true;
36    }
37
38    static boolean isArray(Object o) {
39        return o.getClass().isArray();
40    }
41}