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 @ 47248
History | View | Annotate | Download (4.24 KB)
1 | 40559 | jjdelcerro | /**
|
---|---|---|---|
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 | 40435 | jjdelcerro | package org.gvsig.fmap.dal.feature.impl.featureset; |
25 | |||
26 | 47037 | fdiaz | import java.util.ArrayList; |
27 | 40435 | jjdelcerro | import java.util.Comparator; |
28 | import java.util.Iterator; |
||
29 | 47037 | fdiaz | import java.util.List; |
30 | 40435 | jjdelcerro | |
31 | import org.gvsig.compat.CompatLocator; |
||
32 | import org.gvsig.fmap.dal.exception.DataEvaluatorRuntimeException; |
||
33 | import org.gvsig.fmap.dal.feature.FeatureQueryOrder; |
||
34 | 43026 | jjdelcerro | import org.gvsig.fmap.dal.feature.FeatureQueryOrder.FeatureQueryOrderMember; |
35 | 40435 | jjdelcerro | import org.gvsig.fmap.dal.feature.FeatureType; |
36 | import org.gvsig.fmap.dal.feature.impl.DefaultFeature; |
||
37 | import org.gvsig.fmap.dal.feature.impl.DefaultFeatureStore; |
||
38 | import org.gvsig.fmap.dal.feature.spi.FeatureProvider; |
||
39 | import org.gvsig.tools.evaluator.Evaluator; |
||
40 | import org.gvsig.tools.evaluator.EvaluatorException; |
||
41 | |||
42 | /*
|
||
43 | *
|
||
44 | * Compara dos Feature o FeatureProvider.
|
||
45 | */
|
||
46 | public class FeatureProviderComparator implements Comparator { |
||
47 | |||
48 | private FeatureQueryOrder order;
|
||
49 | private DefaultFeature f1;
|
||
50 | private DefaultFeature f2;
|
||
51 | private DefaultFeatureStore store;
|
||
52 | 47037 | fdiaz | private List<OrderElement> elements = null; |
53 | 40435 | jjdelcerro | |
54 | private class OrderElement { |
||
55 | FeatureQueryOrderMember order = null;
|
||
56 | int fieldIndex = -1; |
||
57 | } |
||
58 | |||
59 | public FeatureProviderComparator(DefaultFeatureStore store, FeatureQueryOrder order) {
|
||
60 | this.order = order;
|
||
61 | this.store = store;
|
||
62 | this.f1 = new DefaultFeature(this.store); |
||
63 | this.f2 = new DefaultFeature(this.store); |
||
64 | } |
||
65 | |||
66 | private void initElements(FeatureType type) { |
||
67 | 47037 | fdiaz | elements = new ArrayList<>(); |
68 | 40435 | jjdelcerro | OrderElement cur; |
69 | Iterator iter = order.iterator();
|
||
70 | while (iter.hasNext()) {
|
||
71 | 47037 | fdiaz | |
72 | FeatureQueryOrderMember orderMember = (FeatureQueryOrderMember) iter.next(); |
||
73 | int index = type.getIndex(orderMember.getAttributeName());
|
||
74 | if(index >= 0) { |
||
75 | 40435 | jjdelcerro | cur = new OrderElement();
|
76 | 47037 | fdiaz | cur.order = orderMember; |
77 | cur.fieldIndex = index; |
||
78 | elements.add(cur); |
||
79 | } else if(orderMember.hasEvaluator()){ |
||
80 | cur = new OrderElement();
|
||
81 | cur.order = orderMember; |
||
82 | cur.fieldIndex = index; |
||
83 | elements.add(cur); |
||
84 | } |
||
85 | 40435 | jjdelcerro | } |
86 | |||
87 | } |
||
88 | |||
89 | public int compare(Object arg0, Object arg1) { |
||
90 | FeatureQueryOrderMember order; |
||
91 | Comparable v1;
|
||
92 | Comparable v2;
|
||
93 | Object o1;
|
||
94 | Object o2;
|
||
95 | int v;
|
||
96 | int fieldIndex;
|
||
97 | |||
98 | f1.setData((FeatureProvider) arg0); |
||
99 | f2.setData((FeatureProvider) arg1); |
||
100 | |||
101 | if (elements == null){ |
||
102 | initElements(f1.getType()); |
||
103 | } |
||
104 | |||
105 | 47037 | fdiaz | for (OrderElement element : elements) {
|
106 | 40435 | jjdelcerro | |
107 | if (element.order.hasEvaluator()) {
|
||
108 | Evaluator evaluator = element.order.getEvaluator(); |
||
109 | try {
|
||
110 | o1 = evaluator.evaluate(f1); |
||
111 | o2 = evaluator.evaluate(f2); |
||
112 | } catch (EvaluatorException e) {
|
||
113 | throw new DataEvaluatorRuntimeException(e); |
||
114 | } |
||
115 | } else {
|
||
116 | o1 = f1.get(element.fieldIndex); |
||
117 | o2 = f2.get(element.fieldIndex); |
||
118 | } |
||
119 | if (o1 == null) { |
||
120 | if (o2 == null) { |
||
121 | return 0; |
||
122 | } else {
|
||
123 | v = 1;
|
||
124 | } |
||
125 | } else {
|
||
126 | if (o1 instanceof String && o2 instanceof String) { |
||
127 | v = CompatLocator.getStringUtils().compare((String) o1,
|
||
128 | (String) o2, true); |
||
129 | } |
||
130 | else if (o1 instanceof Comparable && o2 instanceof Comparable) { |
||
131 | v1 = (Comparable) o1;
|
||
132 | v2 = (Comparable) o2;
|
||
133 | v = v1.compareTo(v2); |
||
134 | } else {
|
||
135 | v = -1;
|
||
136 | } |
||
137 | } |
||
138 | |||
139 | if (v != 0) { |
||
140 | if (element.order.getAscending()) {
|
||
141 | return v;
|
||
142 | } else {
|
||
143 | return -v;
|
||
144 | } |
||
145 | } |
||
146 | } |
||
147 | return 0; |
||
148 | } |
||
149 | |||
150 | } |