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 org.mockito.ArgumentMatcher;
9import org.mockito.internal.matchers.text.ValuePrinter;
10
11import java.io.Serializable;
12
13public class Equals implements ArgumentMatcher<Object>, ContainsExtraTypeInfo, Serializable {
14
15    private final Object wanted;
16
17    public Equals(Object wanted) {
18        this.wanted = wanted;
19    }
20
21    public boolean matches(Object actual) {
22        return Equality.areEqual(this.wanted, actual);
23    }
24
25    public String toString() {
26        return describe(wanted);
27    }
28
29    private String describe(Object object) {
30        return ValuePrinter.print(object);
31    }
32
33    protected final Object getWanted() {
34        return wanted;
35    }
36
37    @Override
38    public boolean equals(Object o) {
39        if (o == null || !this.getClass().equals(o.getClass())) {
40            return false;
41        }
42        Equals other = (Equals) o;
43        return this.wanted == null && other.wanted == null || this.wanted != null && this.wanted.equals(other.wanted);
44    }
45
46    @Override
47    public int hashCode() {
48        return 1;
49    }
50
51    public String toStringWithType() {
52        return "("+ wanted.getClass().getSimpleName() +") " + describe(wanted);
53    }
54
55    public boolean typeMatches(Object target) {
56        return wanted != null && target != null && target.getClass() == wanted.getClass();
57    }
58}
59