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 @ 43020
History | View | Annotate | Download (3.91 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 3
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.fmap.dal.feature.impl.featureset; |
25 |
|
26 |
import java.util.Comparator; |
27 |
import java.util.Iterator; |
28 |
|
29 |
import org.gvsig.compat.CompatLocator; |
30 |
import org.gvsig.fmap.dal.exception.DataEvaluatorRuntimeException; |
31 |
import org.gvsig.fmap.dal.feature.FeatureQueryOrder; |
32 |
import org.gvsig.fmap.dal.feature.FeatureQueryOrderMember; |
33 |
import org.gvsig.fmap.dal.feature.FeatureType; |
34 |
import org.gvsig.fmap.dal.feature.impl.DefaultFeature; |
35 |
import org.gvsig.fmap.dal.feature.impl.DefaultFeatureStore; |
36 |
import org.gvsig.fmap.dal.feature.spi.FeatureProvider; |
37 |
import org.gvsig.tools.evaluator.Evaluator; |
38 |
import org.gvsig.tools.evaluator.EvaluatorException; |
39 |
|
40 |
/*
|
41 |
*
|
42 |
* Compara dos Feature o FeatureProvider.
|
43 |
*/
|
44 |
public class FeatureProviderComparator implements Comparator { |
45 |
|
46 |
private FeatureQueryOrder order;
|
47 |
private DefaultFeature f1;
|
48 |
private DefaultFeature f2;
|
49 |
private DefaultFeatureStore store;
|
50 |
private OrderElement[] elements = null; |
51 |
|
52 |
private class OrderElement { |
53 |
FeatureQueryOrderMember order = null;
|
54 |
int fieldIndex = -1; |
55 |
} |
56 |
|
57 |
public FeatureProviderComparator(DefaultFeatureStore store, FeatureQueryOrder order) {
|
58 |
this.order = order;
|
59 |
this.store = store;
|
60 |
this.f1 = new DefaultFeature(this.store); |
61 |
this.f2 = new DefaultFeature(this.store); |
62 |
} |
63 |
|
64 |
private void initElements(FeatureType type) { |
65 |
elements = new OrderElement[this.order.size()]; |
66 |
OrderElement cur; |
67 |
Iterator iter = order.iterator();
|
68 |
FeatureQueryOrderMember orderItem; |
69 |
int i = 0; |
70 |
while (iter.hasNext()) {
|
71 |
cur = new OrderElement();
|
72 |
cur.order = (FeatureQueryOrderMember) iter.next(); |
73 |
if (!cur.order.hasEvaluator()) {
|
74 |
cur.fieldIndex = type.getIndex(cur.order.getAttributeName()); |
75 |
} |
76 |
elements[i] = cur; |
77 |
i++; |
78 |
} |
79 |
|
80 |
} |
81 |
|
82 |
public int compare(Object arg0, Object arg1) { |
83 |
FeatureQueryOrderMember order; |
84 |
Comparable v1;
|
85 |
Comparable v2;
|
86 |
Object o1;
|
87 |
Object o2;
|
88 |
int v;
|
89 |
int fieldIndex;
|
90 |
|
91 |
f1.setData((FeatureProvider) arg0); |
92 |
f2.setData((FeatureProvider) arg1); |
93 |
|
94 |
if (elements == null){ |
95 |
initElements(f1.getType()); |
96 |
} |
97 |
|
98 |
OrderElement element; |
99 |
|
100 |
for (int i = 0; i < elements.length; i++) { |
101 |
element = elements[i]; |
102 |
|
103 |
if (element.order.hasEvaluator()) {
|
104 |
Evaluator evaluator = element.order.getEvaluator(); |
105 |
try {
|
106 |
o1 = evaluator.evaluate(f1); |
107 |
o2 = evaluator.evaluate(f2); |
108 |
} catch (EvaluatorException e) {
|
109 |
throw new DataEvaluatorRuntimeException(e); |
110 |
} |
111 |
} else {
|
112 |
o1 = f1.get(element.fieldIndex); |
113 |
o2 = f2.get(element.fieldIndex); |
114 |
} |
115 |
if (o1 == null) { |
116 |
if (o2 == null) { |
117 |
return 0; |
118 |
} else {
|
119 |
v = 1;
|
120 |
} |
121 |
} else {
|
122 |
if (o1 instanceof String && o2 instanceof String) { |
123 |
v = CompatLocator.getStringUtils().compare((String) o1,
|
124 |
(String) o2, true); |
125 |
} |
126 |
else if (o1 instanceof Comparable && o2 instanceof Comparable) { |
127 |
v1 = (Comparable) o1;
|
128 |
v2 = (Comparable) o2;
|
129 |
v = v1.compareTo(v2); |
130 |
} else {
|
131 |
v = -1;
|
132 |
} |
133 |
} |
134 |
|
135 |
if (v != 0) { |
136 |
if (element.order.getAscending()) {
|
137 |
return v;
|
138 |
} else {
|
139 |
return -v;
|
140 |
} |
141 |
} |
142 |
} |
143 |
return 0; |
144 |
} |
145 |
|
146 |
} |