1 | /* |
2 | @license.text@ |
3 | */ |
4 | package com.hammurapi.eventbus.tests.familyties.model; |
5 | |
6 | |
7 | public class Relative implements Comparable<Relative> { |
8 | |
9 | private Person subject; |
10 | private Person object; |
11 | |
12 | public Relative(Person subject, Person object) { |
13 | this.subject = subject; |
14 | this.object = object; |
15 | } |
16 | |
17 | public String toString() { |
18 | StringBuffer ret = new StringBuffer(getSubject().toString()); |
19 | ret.append(" is a "); |
20 | |
21 | int idx = getClass().getName().lastIndexOf('.'); |
22 | ret.append(idx == -1 ? getClass().getName() : getClass().getName().substring(idx + 1)); |
23 | ret.append(" of "); |
24 | ret.append(getObject().toString()); |
25 | ret.append(" ("); |
26 | // ret.append(Integer.toString(System.identityHashCode(this), Character.MAX_RADIX)); |
27 | ret.append(")"); |
28 | // ret.append(" ("); |
29 | // ret.append(getDepth()); |
30 | // ret.append("/"); |
31 | // ret.append(getCardinality()); |
32 | // ret.append(")"); |
33 | return ret.toString(); |
34 | } |
35 | |
36 | public Person getSubject() { |
37 | return subject; |
38 | } |
39 | |
40 | public Person getObject() { |
41 | return object; |
42 | } |
43 | |
44 | public int compareTo(Relative o) { |
45 | if (this==o || this.equals(o)) { |
46 | return 0; |
47 | } |
48 | |
49 | int subjectCompare = getSubject().compareTo(o.getSubject()); |
50 | if (subjectCompare!=0) { |
51 | return subjectCompare; |
52 | } |
53 | |
54 | int objectCompare = getObject().compareTo(o.getObject()); |
55 | if (objectCompare!=0) { |
56 | return objectCompare; |
57 | } |
58 | |
59 | int typeCompare = getClass().getName().compareTo(o.getClass().getName()); |
60 | if (typeCompare!=0) { |
61 | return typeCompare; |
62 | } |
63 | |
64 | return hashCode() - o.hashCode(); |
65 | } |
66 | |
67 | @Override |
68 | public int hashCode() { |
69 | final int prime = 31; |
70 | int result = getClass().hashCode(); |
71 | result = prime * result + ((object == null) ? 0 : object.hashCode()); |
72 | result = prime * result + ((subject == null) ? 0 : subject.hashCode()); |
73 | return result; |
74 | } |
75 | |
76 | @Override |
77 | public boolean equals(Object obj) { |
78 | if (this == obj) { |
79 | return true; |
80 | } |
81 | if (obj == null) { |
82 | return false; |
83 | } |
84 | if (getClass() != obj.getClass()) { |
85 | return false; |
86 | } |
87 | Relative other = (Relative) obj; |
88 | if (object == null) { |
89 | if (other.object != null) { |
90 | return false; |
91 | } |
92 | } else if (!object.equals(other.object)) { |
93 | return false; |
94 | } |
95 | if (subject == null) { |
96 | if (other.subject != null) { |
97 | return false; |
98 | } |
99 | } else if (!subject.equals(other.subject)) { |
100 | return false; |
101 | } |
102 | return true; |
103 | } |
104 | |
105 | } |