svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.impl / src / main / java / org / gvsig / fmap / dal / feature / impl / featureset / FeatureProviderComparator.java @ 40435
History | View | Annotate | Download (2.99 KB)
1 | 40435 | jjdelcerro | package org.gvsig.fmap.dal.feature.impl.featureset; |
---|---|---|---|
2 | |||
3 | import java.util.Comparator; |
||
4 | import java.util.Iterator; |
||
5 | |||
6 | import org.gvsig.compat.CompatLocator; |
||
7 | import org.gvsig.fmap.dal.exception.DataEvaluatorRuntimeException; |
||
8 | import org.gvsig.fmap.dal.feature.FeatureQueryOrder; |
||
9 | import org.gvsig.fmap.dal.feature.FeatureQueryOrder.FeatureQueryOrderMember; |
||
10 | import org.gvsig.fmap.dal.feature.FeatureType; |
||
11 | import org.gvsig.fmap.dal.feature.impl.DefaultFeature; |
||
12 | import org.gvsig.fmap.dal.feature.impl.DefaultFeatureStore; |
||
13 | import org.gvsig.fmap.dal.feature.spi.FeatureProvider; |
||
14 | import org.gvsig.tools.evaluator.Evaluator; |
||
15 | import org.gvsig.tools.evaluator.EvaluatorException; |
||
16 | |||
17 | /*
|
||
18 | *
|
||
19 | * Compara dos Feature o FeatureProvider.
|
||
20 | */
|
||
21 | public class FeatureProviderComparator implements Comparator { |
||
22 | |||
23 | private FeatureQueryOrder order;
|
||
24 | private DefaultFeature f1;
|
||
25 | private DefaultFeature f2;
|
||
26 | private DefaultFeatureStore store;
|
||
27 | private OrderElement[] elements = null; |
||
28 | |||
29 | private class OrderElement { |
||
30 | FeatureQueryOrderMember order = null;
|
||
31 | int fieldIndex = -1; |
||
32 | } |
||
33 | |||
34 | public FeatureProviderComparator(DefaultFeatureStore store, FeatureQueryOrder order) {
|
||
35 | this.order = order;
|
||
36 | this.store = store;
|
||
37 | this.f1 = new DefaultFeature(this.store); |
||
38 | this.f2 = new DefaultFeature(this.store); |
||
39 | } |
||
40 | |||
41 | private void initElements(FeatureType type) { |
||
42 | elements = new OrderElement[this.order.size()]; |
||
43 | OrderElement cur; |
||
44 | Iterator iter = order.iterator();
|
||
45 | FeatureQueryOrderMember orderItem; |
||
46 | int i = 0; |
||
47 | while (iter.hasNext()) {
|
||
48 | cur = new OrderElement();
|
||
49 | cur.order = (FeatureQueryOrderMember) iter.next(); |
||
50 | if (!cur.order.hasEvaluator()) {
|
||
51 | cur.fieldIndex = type.getIndex(cur.order.getAttributeName()); |
||
52 | } |
||
53 | elements[i] = cur; |
||
54 | i++; |
||
55 | } |
||
56 | |||
57 | } |
||
58 | |||
59 | public int compare(Object arg0, Object arg1) { |
||
60 | FeatureQueryOrderMember order; |
||
61 | Comparable v1;
|
||
62 | Comparable v2;
|
||
63 | Object o1;
|
||
64 | Object o2;
|
||
65 | int v;
|
||
66 | int fieldIndex;
|
||
67 | |||
68 | f1.setData((FeatureProvider) arg0); |
||
69 | f2.setData((FeatureProvider) arg1); |
||
70 | |||
71 | if (elements == null){ |
||
72 | initElements(f1.getType()); |
||
73 | } |
||
74 | |||
75 | OrderElement element; |
||
76 | |||
77 | for (int i = 0; i < elements.length; i++) { |
||
78 | element = elements[i]; |
||
79 | |||
80 | if (element.order.hasEvaluator()) {
|
||
81 | Evaluator evaluator = element.order.getEvaluator(); |
||
82 | try {
|
||
83 | o1 = evaluator.evaluate(f1); |
||
84 | o2 = evaluator.evaluate(f2); |
||
85 | } catch (EvaluatorException e) {
|
||
86 | throw new DataEvaluatorRuntimeException(e); |
||
87 | } |
||
88 | } else {
|
||
89 | o1 = f1.get(element.fieldIndex); |
||
90 | o2 = f2.get(element.fieldIndex); |
||
91 | } |
||
92 | if (o1 == null) { |
||
93 | if (o2 == null) { |
||
94 | return 0; |
||
95 | } else {
|
||
96 | v = 1;
|
||
97 | } |
||
98 | } else {
|
||
99 | if (o1 instanceof String && o2 instanceof String) { |
||
100 | v = CompatLocator.getStringUtils().compare((String) o1,
|
||
101 | (String) o2, true); |
||
102 | } |
||
103 | else if (o1 instanceof Comparable && o2 instanceof Comparable) { |
||
104 | v1 = (Comparable) o1;
|
||
105 | v2 = (Comparable) o2;
|
||
106 | v = v1.compareTo(v2); |
||
107 | } else {
|
||
108 | v = -1;
|
||
109 | } |
||
110 | } |
||
111 | |||
112 | if (v != 0) { |
||
113 | if (element.order.getAscending()) {
|
||
114 | return v;
|
||
115 | } else {
|
||
116 | return -v;
|
||
117 | } |
||
118 | } |
||
119 | } |
||
120 | return 0; |
||
121 | } |
||
122 | |||
123 | } |