1
2
3
4
5
6
7
8
9
10
11
12
13 package org.abstracthorizon.aequo;
14
15
16
17
18
19
20
21
22 public class DefaultCompareEntry<T> implements CompareEntry<T> {
23
24
25 protected T[] data;
26
27
28 protected byte[] status;
29
30
31
32
33
34
35 public DefaultCompareEntry(T[] data) {
36 this.data = data;
37 status = new byte[data.length];
38 }
39
40
41
42
43
44 public T[] getData() {
45 return data;
46 }
47
48
49
50
51
52 public void setData(T[] data) {
53 this.data = data;
54 updateEntryStatus();
55 }
56
57
58
59
60
61
62 public T getData(int i) {
63 return data[i];
64 }
65
66
67
68
69
70
71 public void setData(int i, T data) {
72 this.data[i] = data;
73 updateEntryStatus();
74 }
75
76
77
78
79
80
81
82
83
84
85
86 public byte getStatus(int i) {
87 return status[i];
88 }
89
90
91
92
93
94
95
96
97
98
99
100 public void setStatus(int i, byte status) {
101 this.status[i] = status;
102 }
103
104
105
106
107
108
109
110 @SuppressWarnings("unchecked")
111 public boolean equals(Object o) {
112 if (o instanceof CompareEntry) {
113 CompareEntry<T> entry = (CompareEntry)o;
114 T[] otherData = entry.getData();
115 if (otherData.length != data.length) {
116 return false;
117 }
118 for (int i = 0; i < data.length; i++) {
119 if (otherData[i] != data[i]) {
120 return false;
121 }
122 }
123 return true;
124 }
125 return false;
126 }
127
128
129
130
131
132 public int hashCode() {
133 if (data != null) {
134 return data.hashCode();
135 } else {
136 return 0;
137 }
138 }
139
140
141
142
143
144 @SuppressWarnings("unchecked")
145 public void updateEntryStatus() {
146 if (data.length == 2) {
147 byte res = compare(data[0], data[1]);
148
149
150
151
152
153
154
155
156
157
158
159
160 if ((res == EQUAL)
161
162 || (res == DIFFERENT)) {
163 status[0] = res;
164 status[1] = res;
165 } else if (res == GREATER) {
166 status[0] = res;
167 status[1] = LESS;
168 } else if (res == LESS) {
169 status[0] = res;
170 status[1] = GREATER;
171 } else {
172 throw new IllegalStateException("EQUAL, DIFFERENT, LESS or GREATER are only allowed results");
173 }
174
175 } else {
176 throw new IllegalStateException("Not implemented: Only two arguments can be compared at the moment");
177 }
178 }
179
180
181
182
183
184
185
186
187 @SuppressWarnings("unchecked")
188 public byte compare(T o1, T o2) {
189 if (o1 == o2) {
190 return EQUAL;
191 }
192 if (o1 != null) {
193 if (o1.equals(o2)) {
194 return EQUAL;
195 }
196 if (o2 != null) {
197 if (o1 instanceof Comparable<?>) {
198 Comparable<T> comp = (Comparable<T>)o1;
199 int res = comp.compareTo(o2);
200 if (res == 0) {
201 return EQUAL;
202 } else if (res < 0) {
203 return LESS;
204 } else {
205 return GREATER;
206 }
207 } else {
208 return DIFFERENT;
209 }
210 } else {
211 return GREATER;
212 }
213 } else {
214 return LESS;
215 }
216 }
217 }