/
BeanList.java
326 lines (272 loc) · 10.4 KB
/
BeanList.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
* Copyright 2010 Visural.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* under the License.
*/
package com.visural.common.datastruct;
import com.visural.common.BeanUtil;
import com.visural.common.collection.readonly.ReadOnlyList;
import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
/**
* Wrapper for a List of JavaBeans, which allows sorting on different properties.
* @author Visural
*/
public class BeanList<T> implements List<T>, Serializable {
private static final String[] StringArray = new String[]{};
private final List<T> list;
private int numberOfSorts = 1;
private List<String> sortProperties = new ArrayList<String>();
private Nulls nullHandling = Nulls.HandledByComparators;
public BeanList(List<T> real) {
this.list = real;
}
public BeanList(List<T> real, int numberOfSorts) {
this.list = real;
this.numberOfSorts = numberOfSorts;
}
public BeanList(List<T> real, int numberOfSorts, String... initialSort) {
this(real, numberOfSorts);
sortByProperties(initialSort);
}
public BeanList(List<T> real, int numberOfSorts, Nulls nullHandling, String... initialSort) {
this(real, numberOfSorts);
setNullHandling(nullHandling);
sortByProperties(initialSort);
}
public void sortByProperties(String... properties) {
for (int n = properties.length-1; n >= 0; n--) {
popIfRequiredThenAdd(properties[n]);
}
sort(properties);
}
public BeanList<T> resortByProperty(String property) {
popIfRequiredThenAdd(property);
sort(sortProperties.toArray(StringArray));
return this;
}
private void popIfRequiredThenAdd(String property) {
if (sortProperties.size() > 0 && sortProperties.size() >= numberOfSorts) {
String propAdj = property.startsWith("-") ? property.substring(1) : property;
if (sortProperties.contains(propAdj)) {
// remove old sort on this property
sortProperties.remove(propAdj);
} else if (sortProperties.contains("-"+propAdj)) {
sortProperties.remove("-"+propAdj);
} else {
// sort pops oldest sort from end
sortProperties.remove(sortProperties.size()-1);
}
}
sortProperties.add(0, property);
}
public BeanList<T> resort() {
sort(sortProperties.toArray(StringArray));
return this;
}
private BeanList<T> sort(final String... properties) {
Collections.sort(list, new Comparator<T>() {
public int compare(T o1, T o2) {
try {
for (int n = 0; n < properties.length; n++) {
boolean ascending = true;
String property = properties[n];
if (property.startsWith("-")) {
ascending = false;
property = property.substring(1);
}
Class type = BeanUtil.getPropertyType(o1, property);
Method getter1 = BeanUtil.findGetter(o1.getClass(), property);
if (getter1 == null) {
throw new IllegalStateException(String.format("Property '%s' on class '%s' can not be resolved.", property, o1.getClass().getName()));
}
Method getter2 = BeanUtil.findGetter(o2.getClass(), property);
if (getter2 == null) {
throw new IllegalStateException(String.format("Property '%s' on class '%s' can not be resolved.", property, o2.getClass().getName()));
}
Object val1 = getter1.invoke(o1);
Object val2 = getter2.invoke(o2);
int result = 0;
switch (nullHandling) {
case AreLess:
if (val1 == null && val2 == null) {
continue;
} else if (val1 == null && val2 != null) {
return ascending ? -1 : 1;
} else if (val1 != null && val2 == null) {
return ascending ? 1 : -1;
}
break;
case AreMore:
if (val1 == null && val2 == null) {
continue;
} else if(val1 == null && val2 != null) {
return ascending ? 1 : -1;
} else if (val1 != null && val2 == null) {
return ascending ? -1 : 1;
}
break;
}
Comparator comparator = getComparator(property, type);
if (comparator != null) {
result = comparator.compare(val1, val2);
} else if (Comparable.class.isAssignableFrom(type)) {
if (val1 != null) {
result = ((Comparable)val1).compareTo(val2);
} else if (val2 != null) {
result = ((Comparable)val2).compareTo(val1);
} else {
result = 0;
}
} else {
throw new IllegalStateException("Don't know how to compare type '"+type.getName()+"'");
}
if (result < 0 || result > 0) {
return ascending ? result : -result;
}
// if result == 0 we continue for next property comparison
}
// if all properties return 0, we return 0 too
return 0;
} catch (Exception e) {
throw new IllegalStateException("Error invoking methods.", e);
}
}
});
return this;
}
private Comparator getComparator(String property, Class type) {
Comparator c = propertyComparators.get(property);
if (c == null) {
c = typeComparators.get(type);
}
return c;
}
private final Map<Class, Comparator> typeComparators = new HashMap<Class, Comparator>();
public BeanList<T> registerTypeComparator(Class type, Comparator comparator) {
typeComparators.put(type, comparator);
return this;
}
private final Map<String, Comparator> propertyComparators = new HashMap<String, Comparator>();
public BeanList<T> registerPropertyComparator(String property, Comparator comparator) {
propertyComparators.put(property, comparator);
return this;
}
public int getNumberOfSorts() {
return numberOfSorts;
}
public void setNumberOfSorts(int numberOfSorts) {
this.numberOfSorts = numberOfSorts;
while (numberOfSorts < sortProperties.size()) {
sortProperties.remove(sortProperties.size()-1);
}
}
public ReadOnlyList<String> getSortProperties() {
return new ReadOnlyList(sortProperties);
}
public Nulls getNullHandling() {
return nullHandling;
}
public void setNullHandling(Nulls nullHandling) {
this.nullHandling = nullHandling;
}
/* wrapped list methods follow */
public int size() {
return list.size();
}
public boolean isEmpty() {
return list.isEmpty();
}
public boolean contains(Object o) {
return list.contains(o);
}
public Iterator<T> iterator() {
return list.iterator();
}
public Object[] toArray() {
return list.toArray();
}
public <T> T[] toArray(T[] a) {
return list.toArray(a);
}
public boolean add(T e) {
return list.add(e);
}
public boolean remove(Object o) {
return list.remove(o);
}
public boolean containsAll(Collection<?> c) {
return list.containsAll(c);
}
public boolean addAll(Collection<? extends T> c) {
return list.addAll(c);
}
public boolean addAll(int index, Collection<? extends T> c) {
return list.addAll(index, c);
}
public boolean removeAll(Collection<?> c) {
return list.removeAll(c);
}
public boolean retainAll(Collection<?> c) {
return list.retainAll(c);
}
public void clear() {
list.clear();
}
public T get(int index) {
return list.get(index);
}
public T set(int index, T element) {
return list.set(index, element);
}
public void add(int index, T element) {
list.add(index, element);
}
public T remove(int index) {
return list.remove(index);
}
public int indexOf(Object o) {
return list.indexOf(o);
}
public int lastIndexOf(Object o) {
return list.lastIndexOf(o);
}
public ListIterator<T> listIterator() {
return list.listIterator();
}
public ListIterator<T> listIterator(int index) {
return list.listIterator();
}
public List<T> subList(int fromIndex, int toIndex) {
return list.subList(fromIndex, toIndex);
}
@Override
public String toString() {
return "Sort"+sortProperties+"-"+list.toString();
}
public enum Nulls {
AreLess,
AreMore,
HandledByComparators
}
}