01: package biz.hammurapi.web.mda;
02:
03: import java.util.Comparator;
04:
05: /**
06: * Orders parts by name as specified in the array passed to constructor.
07: * Unlisted parts are placed after listed parts and ordered alphabetically.
08: * @author Pavel
09: *
10: */
11: public class SimplePartsComparator implements Comparator {
12:
13: private String[] partsOrder;
14:
15: public SimplePartsComparator(String[] partsOrder) {
16: this .partsOrder = partsOrder;
17: }
18:
19: public int compare(Object o1, Object o2) {
20: String pn1 = String.valueOf(o1);
21: String pn2 = String.valueOf(o2);
22: int idx1 = idx(pn1);
23: int idx2 = idx(pn2);
24: if (idx1 == -1) {
25: return idx2 == -1 ? pn1.compareTo(pn2) : 1;
26: }
27:
28: return idx2 == -1 ? -1 : pn1.compareTo(pn2);
29: }
30:
31: private int idx(String str) {
32: for (int i = 0; i < partsOrder.length; ++i) {
33: if (str.equals(partsOrder[i])) {
34: return i;
35: }
36: }
37: return -1;
38: }
39:
40: }
|