svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.api / src / main / java / org / gvsig / fmap / dal / feature / FeatureQueryOrder.java @ 43026
History | View | Annotate | Download (9.25 KB)
1 | 40559 | jjdelcerro | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | *
|
||
4 | * Copyright (C) 2007-2013 gvSIG Association.
|
||
5 | *
|
||
6 | 43020 | jjdelcerro | * This program is free software; you can redistribute it and/or modify it under
|
7 | * the terms of the GNU General Public License as published by the Free Software
|
||
8 | * Foundation; either version 3 of the License, or (at your option) any later
|
||
9 | * version.
|
||
10 | 40559 | jjdelcerro | *
|
11 | 43020 | jjdelcerro | * This program is distributed in the hope that it will be useful, but WITHOUT
|
12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||
13 | * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
||
14 | * details.
|
||
15 | 40559 | jjdelcerro | *
|
16 | 43020 | jjdelcerro | * You should have received a copy of the GNU General Public License along with
|
17 | * this program; if not, write to the Free Software Foundation, Inc., 51
|
||
18 | * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||
19 | 40559 | jjdelcerro | *
|
20 | 43020 | jjdelcerro | * For any additional information, do not hesitate to contact us at info AT
|
21 | * gvsig.com, or visit our website www.gvsig.com.
|
||
22 | 40559 | jjdelcerro | */
|
23 | 40435 | jjdelcerro | package org.gvsig.fmap.dal.feature; |
24 | |||
25 | import java.util.ArrayList; |
||
26 | import java.util.Comparator; |
||
27 | import java.util.Iterator; |
||
28 | import java.util.List; |
||
29 | 43020 | jjdelcerro | import java.util.NoSuchElementException; |
30 | 40435 | jjdelcerro | |
31 | import org.gvsig.fmap.dal.exception.DataEvaluatorRuntimeException; |
||
32 | import org.gvsig.tools.evaluator.Evaluator; |
||
33 | import org.gvsig.tools.evaluator.EvaluatorData; |
||
34 | import org.gvsig.tools.evaluator.EvaluatorException; |
||
35 | import org.gvsig.tools.lang.Cloneable; |
||
36 | import org.gvsig.tools.persistence.Persistent; |
||
37 | import org.gvsig.tools.persistence.PersistentState; |
||
38 | import org.gvsig.tools.persistence.exception.PersistenceException; |
||
39 | |||
40 | 43026 | jjdelcerro | public class FeatureQueryOrder implements Persistent, Cloneable { |
41 | 40435 | jjdelcerro | |
42 | 43026 | jjdelcerro | public class FeatureQueryOrderMember implements Persistent, org.gvsig.tools.lang.Cloneable { |
43 | |||
44 | String attributeName = null; |
||
45 | Evaluator evaluator = null;
|
||
46 | boolean ascending;
|
||
47 | |||
48 | FeatureQueryOrderMember(String attributeName, boolean ascending) { |
||
49 | this.attributeName = attributeName;
|
||
50 | this.ascending = ascending;
|
||
51 | } |
||
52 | |||
53 | FeatureQueryOrderMember(Evaluator evaluator, boolean ascending) {
|
||
54 | this.evaluator = evaluator;
|
||
55 | this.ascending = ascending;
|
||
56 | } |
||
57 | |||
58 | public boolean hasEvaluator() { |
||
59 | return this.evaluator != null; |
||
60 | } |
||
61 | |||
62 | public Evaluator getEvaluator() {
|
||
63 | return this.evaluator; |
||
64 | } |
||
65 | |||
66 | public boolean getAscending() { |
||
67 | return this.ascending; |
||
68 | } |
||
69 | |||
70 | public String getAttributeName() { |
||
71 | return this.attributeName; |
||
72 | } |
||
73 | |||
74 | @Override
|
||
75 | public void loadFromState(PersistentState state) |
||
76 | throws PersistenceException {
|
||
77 | this.attributeName = state.getString("attributeName"); |
||
78 | this.ascending = state.getBoolean("ascending"); |
||
79 | this.evaluator = (Evaluator) state.get("evaluator"); |
||
80 | } |
||
81 | |||
82 | @Override
|
||
83 | public void saveToState(PersistentState state) |
||
84 | throws PersistenceException {
|
||
85 | state.set("attributeName", this.attributeName); |
||
86 | state.set("ascending", this.ascending); |
||
87 | if (this.evaluator != null) { |
||
88 | state.set("evaluator", evaluator);
|
||
89 | } |
||
90 | |||
91 | } |
||
92 | |||
93 | @Override
|
||
94 | public FeatureQueryOrderMember clone() throws CloneNotSupportedException { |
||
95 | // Nothing more to clone
|
||
96 | return (FeatureQueryOrderMember) super.clone(); |
||
97 | } |
||
98 | |||
99 | } |
||
100 | |||
101 | 43020 | jjdelcerro | private List members = new ArrayList(); |
102 | 40435 | jjdelcerro | |
103 | 43026 | jjdelcerro | public FeatureQueryOrder() {
|
104 | } |
||
105 | |||
106 | 43020 | jjdelcerro | public Object add(String attributeName, boolean ascending) { |
107 | 40435 | jjdelcerro | FeatureQueryOrderMember member = new FeatureQueryOrderMember(
|
108 | attributeName, ascending); |
||
109 | 43020 | jjdelcerro | if (members.add(member)) {
|
110 | return member;
|
||
111 | } |
||
112 | return null; |
||
113 | } |
||
114 | 40435 | jjdelcerro | |
115 | 43020 | jjdelcerro | public Object add(Evaluator evaluator, boolean ascending) { |
116 | FeatureQueryOrderMember member = new FeatureQueryOrderMember(
|
||
117 | evaluator, |
||
118 | ascending); |
||
119 | if (members.add(member)) {
|
||
120 | return member;
|
||
121 | } |
||
122 | return null; |
||
123 | } |
||
124 | 40435 | jjdelcerro | |
125 | 43026 | jjdelcerro | public Iterable<FeatureQueryOrderMember> members() { |
126 | // Me hubiese gustado que FeatureQueryOrder fuese Iterable, pero no se deja por
|
||
127 | // que FeatureQueryOrderMember es una clase interna y no esta definida en el
|
||
128 | // momento de poner el :
|
||
129 | // implements Iterable<FeatureQueryOrderMember>
|
||
130 | // Y si saco a una clase independiente FeatureQueryOrderMember se pierde
|
||
131 | // compatibilidad binaria con quien la esta usando.
|
||
132 | // Para rodearlo, he creado el metodo members.
|
||
133 | return new Iterable<FeatureQueryOrderMember> () { |
||
134 | |||
135 | @Override
|
||
136 | public Iterator<FeatureQueryOrderMember> iterator() { |
||
137 | return FeatureQueryOrder.this.iterator();
|
||
138 | } |
||
139 | }; |
||
140 | } |
||
141 | |||
142 | 43020 | jjdelcerro | public Iterator<FeatureQueryOrderMember> iterator() { |
143 | 43026 | jjdelcerro | if (members == null) { |
144 | 43020 | jjdelcerro | return new Iterator<FeatureQueryOrderMember>() { |
145 | @Override
|
||
146 | public boolean hasNext() { |
||
147 | return false; |
||
148 | } |
||
149 | 40435 | jjdelcerro | |
150 | 43020 | jjdelcerro | @Override
|
151 | public FeatureQueryOrderMember next() {
|
||
152 | throw new NoSuchElementException(); |
||
153 | } |
||
154 | 40435 | jjdelcerro | |
155 | 43020 | jjdelcerro | @Override
|
156 | public void remove() { |
||
157 | throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. |
||
158 | } |
||
159 | }; |
||
160 | } |
||
161 | return members.iterator();
|
||
162 | } |
||
163 | 40435 | jjdelcerro | |
164 | 43020 | jjdelcerro | public boolean remove(FeatureQueryOrderMember member) { |
165 | return members.remove(member);
|
||
166 | } |
||
167 | 40435 | jjdelcerro | |
168 | 43020 | jjdelcerro | public void remove(int index) { |
169 | members.remove(index); |
||
170 | } |
||
171 | 40435 | jjdelcerro | |
172 | 43020 | jjdelcerro | public void clear() { |
173 | members.clear(); |
||
174 | } |
||
175 | 40435 | jjdelcerro | |
176 | 43020 | jjdelcerro | public int size() { |
177 | return this.members.size(); |
||
178 | } |
||
179 | 40435 | jjdelcerro | |
180 | 43020 | jjdelcerro | public Comparator getFeatureComparator() { |
181 | return new DefaultFeatureComparator(this); |
||
182 | } |
||
183 | 40435 | jjdelcerro | |
184 | 43020 | jjdelcerro | public Object clone() throws CloneNotSupportedException { |
185 | FeatureQueryOrder clone = (FeatureQueryOrder) super.clone();
|
||
186 | 40435 | jjdelcerro | |
187 | 43020 | jjdelcerro | if (members != null) { |
188 | clone.members = new ArrayList(members.size()); |
||
189 | for (int i = 0; i < members.size(); i++) { |
||
190 | clone.members.add(((Cloneable) members.get(i)).clone());
|
||
191 | } |
||
192 | } |
||
193 | 40435 | jjdelcerro | |
194 | 43020 | jjdelcerro | return clone;
|
195 | } |
||
196 | 40435 | jjdelcerro | |
197 | 43020 | jjdelcerro | public FeatureQueryOrder getCopy() {
|
198 | FeatureQueryOrder aCopy = new FeatureQueryOrder();
|
||
199 | Iterator iter = this.members.iterator(); |
||
200 | FeatureQueryOrderMember member; |
||
201 | while (iter.hasNext()) {
|
||
202 | member = (FeatureQueryOrderMember) iter.next(); |
||
203 | if (member.hasEvaluator()) {
|
||
204 | aCopy.add(member.getEvaluator(), member.getAscending()); |
||
205 | } else {
|
||
206 | aCopy.add(member.getAttributeName(), member.getAscending()); |
||
207 | } |
||
208 | } |
||
209 | return aCopy;
|
||
210 | } |
||
211 | 40435 | jjdelcerro | |
212 | 43020 | jjdelcerro | public void loadFromState(PersistentState state) |
213 | throws PersistenceException {
|
||
214 | this.members = (List) state.get("menbers"); |
||
215 | } |
||
216 | 40435 | jjdelcerro | |
217 | 43020 | jjdelcerro | public void saveToState(PersistentState state) throws PersistenceException { |
218 | state.set("menbers", members);
|
||
219 | } |
||
220 | 40435 | jjdelcerro | |
221 | 43020 | jjdelcerro | private class DefaultFeatureComparator implements Comparator { |
222 | 40435 | jjdelcerro | |
223 | 43020 | jjdelcerro | private FeatureQueryOrder order;
|
224 | 40435 | jjdelcerro | |
225 | 43020 | jjdelcerro | public DefaultFeatureComparator(FeatureQueryOrder order) {
|
226 | this.order = order;
|
||
227 | // TODO optimizar en un array???
|
||
228 | 40435 | jjdelcerro | |
229 | 43020 | jjdelcerro | } |
230 | 40435 | jjdelcerro | |
231 | 43020 | jjdelcerro | private int myCompare(Object arg0, Object arg1) { |
232 | if (arg0 == null) { |
||
233 | if (arg1 == null) { |
||
234 | return 0; |
||
235 | } else {
|
||
236 | return 1; |
||
237 | } |
||
238 | } else if (arg1 == null) { |
||
239 | if (arg0 == null) { |
||
240 | return 0; |
||
241 | } else {
|
||
242 | return 1; |
||
243 | } |
||
244 | } |
||
245 | if (arg0 instanceof Comparable) { |
||
246 | return ((Comparable) arg0).compareTo(arg1); |
||
247 | } else if (arg1 instanceof Comparable) { |
||
248 | return ((Comparable) arg1).compareTo(arg0) * -1; |
||
249 | } |
||
250 | 40435 | jjdelcerro | |
251 | 43020 | jjdelcerro | if (arg0.equals(arg1)) {
|
252 | return 0; |
||
253 | } else {
|
||
254 | return -1; |
||
255 | } |
||
256 | 40435 | jjdelcerro | |
257 | 43020 | jjdelcerro | } |
258 | 40435 | jjdelcerro | |
259 | 43020 | jjdelcerro | public int compare(Object arg0, Object arg1) { |
260 | Iterator iter = this.order.iterator(); |
||
261 | int returnValue = 0; |
||
262 | Feature f0 = (Feature) arg0; |
||
263 | Feature f1 = (Feature) arg1; |
||
264 | Object item;
|
||
265 | String attrName;
|
||
266 | Evaluator evaluator; |
||
267 | while (returnValue == 0 && iter.hasNext()) { |
||
268 | item = iter.next(); |
||
269 | if (item instanceof String) { |
||
270 | attrName = (String) item;
|
||
271 | returnValue = this
|
||
272 | .myCompare(f0.get(attrName), f1 |
||
273 | .get(attrName)); |
||
274 | } else {
|
||
275 | evaluator = (Evaluator) item; |
||
276 | try {
|
||
277 | returnValue = this.myCompare(evaluator
|
||
278 | .evaluate((EvaluatorData) f0), evaluator |
||
279 | .evaluate((EvaluatorData) f1)); |
||
280 | } catch (EvaluatorException e) {
|
||
281 | throw new DataEvaluatorRuntimeException(e); |
||
282 | } |
||
283 | } |
||
284 | } |
||
285 | 40435 | jjdelcerro | |
286 | 43020 | jjdelcerro | return returnValue;
|
287 | } |
||
288 | 40435 | jjdelcerro | |
289 | 43020 | jjdelcerro | } |
290 | |||
291 | } |