1 /*
2 * Copyright (c) 2007 Creative Sphere Limited.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 *
10 * Creative Sphere - initial API and implementation
11 *
12 */
13 package org.abstracthorizon.aequo;
14
15 /**
16 * Implementation of {@link CompareEntry}
17 *
18 * @param <T> type
19 *
20 * @author Daniel Sendula
21 */
22 public class DefaultCompareEntry<T> implements CompareEntry<T> {
23
24 /** Data array */
25 protected T[] data;
26
27 /** Status array */
28 protected byte[] status;
29
30 /**
31 * Constructor
32 *
33 * @param data initial data
34 */
35 public DefaultCompareEntry(T[] data) {
36 this.data = data;
37 status = new byte[data.length];
38 }
39
40 /**
41 * Returns data array
42 * @return data array
43 */
44 public T[] getData() {
45 return data;
46 }
47
48 /**
49 * Sets data as an array
50 * @param data sets data as an array
51 */
52 public void setData(T[] data) {
53 this.data = data;
54 updateEntryStatus();
55 }
56
57 /**
58 * Returns data for asked column
59 * @param index column index
60 * @return data for asked column
61 */
62 public T getData(int i) {
63 return data[i];
64 }
65
66 /**
67 * Sets data in given column
68 * @param index index of column
69 * @param data data
70 */
71 public void setData(int i, T data) {
72 this.data[i] = data;
73 updateEntryStatus();
74 }
75
76 /**
77 * Returns status of asked column
78 * @param column column
79 * @return status of asked column
80 *
81 * @see CompareEntry#EQUAL
82 * @see CompareEntry#GREATER
83 * @see CompareEntry#LESS
84 * @see CompareEntry#DIFFERENT
85 */
86 public byte getStatus(int i) {
87 return status[i];
88 }
89
90 /**
91 * Sets status for given column
92 * @param i column
93 * @param status status
94 *
95 * @see CompareEntry#EQUAL
96 * @see CompareEntry#GREATER
97 * @see CompareEntry#LESS
98 * @see CompareEntry#DIFFERENT
99 */
100 public void setStatus(int i, byte status) {
101 this.status[i] = status;
102 }
103
104 /**
105 * Compares two entry.
106 *
107 * @param o other entry
108 * @return <code>true</code> if data lengths are the same and each column data is identical
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 * Returns hash code
130 * @return hash code
131 */
132 public int hashCode() {
133 if (data != null) {
134 return data.hashCode();
135 } else {
136 return 0;
137 }
138 }
139
140 /**
141 * Updates entry status. This method uses comparator to calculate
142 * status for each column.
143 */
144 @SuppressWarnings("unchecked")
145 public void updateEntryStatus() {
146 if (data.length == 2) {
147 byte res = compare(data[0], data[1]);
148 // if (comparator.isEmpty(data[0])) {
149 // if (comparator.isEmpty(data[1])) {
150 // status[0] = EMPTY;
151 // status[1] = EMPTY;
152 // } else {
153 // status[0] = EMPTY;
154 // status[1] = GREATER;
155 // }
156 // } else if (comparator.isEmpty(data[1])) {
157 // status[0] = ComparisonResult.GREATER;
158 // status[1] = ComparisonResult.EMPTY;
159 // } else {
160 if ((res == EQUAL)
161 // || (res == ComparisonResult.EMPTY)
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 * Default result comparator class compares two objects using equality as
182 * defined in {@link Object#equals(Object)} method and if fails checks if
183 * objects implement {@link Comparable} interface and uses that to calculate result.
184 * @param o1 first parameter
185 * @param o2 second parameter
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 }