Revision 43020 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
FeatureQueryOrder.java | ||
---|---|---|
3 | 3 |
* |
4 | 4 |
* Copyright (C) 2007-2013 gvSIG Association. |
5 | 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.
|
|
6 |
* 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 | 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.
|
|
11 |
* 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 | 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. |
|
16 |
* 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. |
|
20 | 19 |
* |
21 |
* For any additional information, do not hesitate to contact us |
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
|
20 |
* For any additional information, do not hesitate to contact us at info AT
|
|
21 |
* gvsig.com, or visit our website www.gvsig.com. |
|
23 | 22 |
*/ |
24 | 23 |
package org.gvsig.fmap.dal.feature; |
25 | 24 |
|
... | ... | |
27 | 26 |
import java.util.Comparator; |
28 | 27 |
import java.util.Iterator; |
29 | 28 |
import java.util.List; |
29 |
import java.util.NoSuchElementException; |
|
30 | 30 |
|
31 | 31 |
import org.gvsig.fmap.dal.exception.DataEvaluatorRuntimeException; |
32 | 32 |
import org.gvsig.tools.evaluator.Evaluator; |
... | ... | |
37 | 37 |
import org.gvsig.tools.persistence.PersistentState; |
38 | 38 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
39 | 39 |
|
40 |
public class FeatureQueryOrder implements Persistent, Cloneable { |
|
40 |
public class FeatureQueryOrder implements Persistent, Cloneable, Iterable<FeatureQueryOrderMember> {
|
|
41 | 41 |
|
42 |
private List members = new ArrayList();
|
|
42 |
private List members = new ArrayList();
|
|
43 | 43 |
|
44 |
public class FeatureQueryOrderMember implements Persistent, Cloneable { |
|
45 |
String attributeName = null; |
|
46 |
Evaluator evaluator = null; |
|
47 |
boolean ascending; |
|
48 |
|
|
49 |
FeatureQueryOrderMember(String attributeName, boolean ascending) { |
|
50 |
this.attributeName = attributeName; |
|
51 |
this.ascending = ascending; |
|
52 |
} |
|
53 |
FeatureQueryOrderMember(Evaluator evaluator, boolean ascending) { |
|
54 |
this.evaluator = evaluator; |
|
55 |
this.ascending = ascending; |
|
56 |
} |
|
57 |
public boolean hasEvaluator() { |
|
58 |
return this.evaluator != null; |
|
59 |
} |
|
60 |
public Evaluator getEvaluator() { |
|
61 |
return this.evaluator; |
|
62 |
} |
|
63 |
public boolean getAscending() { |
|
64 |
return this.ascending; |
|
65 |
} |
|
66 |
public String getAttributeName() { |
|
67 |
return this.attributeName; |
|
68 |
} |
|
69 |
public void loadFromState(PersistentState state) |
|
70 |
throws PersistenceException { |
|
71 |
this.attributeName = state.getString("attributeName"); |
|
72 |
this.ascending = state.getBoolean("ascending"); |
|
73 |
this.evaluator = (Evaluator) state.get("evaluator"); |
|
74 |
} |
|
75 |
|
|
76 |
public void saveToState(PersistentState state) |
|
77 |
throws PersistenceException { |
|
78 |
state.set("attributeName", this.attributeName); |
|
79 |
state.set("ascending", this.ascending); |
|
80 |
if (this.evaluator != null) { |
|
81 |
state.set("evaluator", evaluator); |
|
82 |
} |
|
83 |
|
|
84 |
} |
|
85 |
|
|
86 |
public Object clone() throws CloneNotSupportedException { |
|
87 |
// Nothing more to clone |
|
88 |
return super.clone(); |
|
89 |
} |
|
90 |
} |
|
91 |
|
|
92 |
public Object add(String attributeName, boolean ascending) { |
|
44 |
public Object add(String attributeName, boolean ascending) { |
|
93 | 45 |
FeatureQueryOrderMember member = new FeatureQueryOrderMember( |
94 | 46 |
attributeName, ascending); |
95 |
if( members.add(member) ) {
|
|
96 |
return member;
|
|
97 |
}
|
|
98 |
return null;
|
|
99 |
}
|
|
47 |
if (members.add(member)) {
|
|
48 |
return member;
|
|
49 |
}
|
|
50 |
return null;
|
|
51 |
}
|
|
100 | 52 |
|
101 |
public Object add(Evaluator evaluator, boolean ascending) {
|
|
102 |
FeatureQueryOrderMember member = new FeatureQueryOrderMember(
|
|
103 |
evaluator,
|
|
104 |
ascending);
|
|
105 |
if (members.add(member)) {
|
|
106 |
return member;
|
|
107 |
}
|
|
108 |
return null;
|
|
109 |
}
|
|
53 |
public Object add(Evaluator evaluator, boolean ascending) {
|
|
54 |
FeatureQueryOrderMember member = new FeatureQueryOrderMember(
|
|
55 |
evaluator,
|
|
56 |
ascending);
|
|
57 |
if (members.add(member)) {
|
|
58 |
return member;
|
|
59 |
}
|
|
60 |
return null;
|
|
61 |
}
|
|
110 | 62 |
|
111 |
public Iterator iterator() { |
|
112 |
return members.iterator(); |
|
113 |
} |
|
63 |
@Override |
|
64 |
public Iterator<FeatureQueryOrderMember> iterator() { |
|
65 |
if( members == null ) { |
|
66 |
return new Iterator<FeatureQueryOrderMember>() { |
|
67 |
@Override |
|
68 |
public boolean hasNext() { |
|
69 |
return false; |
|
70 |
} |
|
114 | 71 |
|
115 |
public boolean remove(FeatureQueryOrderMember member) { |
|
116 |
return members.remove(member); |
|
117 |
} |
|
72 |
@Override |
|
73 |
public FeatureQueryOrderMember next() { |
|
74 |
throw new NoSuchElementException(); |
|
75 |
} |
|
118 | 76 |
|
119 |
public void remove(int index) { |
|
120 |
members.remove(index); |
|
121 |
} |
|
77 |
@Override |
|
78 |
public void remove() { |
|
79 |
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. |
|
80 |
} |
|
81 |
}; |
|
82 |
} |
|
83 |
return members.iterator(); |
|
84 |
} |
|
122 | 85 |
|
123 |
public void clear() {
|
|
124 |
members.clear();
|
|
125 |
}
|
|
86 |
public boolean remove(FeatureQueryOrderMember member) {
|
|
87 |
return members.remove(member);
|
|
88 |
}
|
|
126 | 89 |
|
127 |
public int size() {
|
|
128 |
return this.members.size();
|
|
129 |
}
|
|
90 |
public void remove(int index) {
|
|
91 |
members.remove(index);
|
|
92 |
}
|
|
130 | 93 |
|
131 |
public Comparator getFeatureComparator() {
|
|
132 |
return new DefaultFeatureComparator(this);
|
|
133 |
}
|
|
94 |
public void clear() {
|
|
95 |
members.clear();
|
|
96 |
}
|
|
134 | 97 |
|
135 |
public Object clone() throws CloneNotSupportedException { |
|
136 |
FeatureQueryOrder clone = (FeatureQueryOrder) super.clone(); |
|
98 |
public int size() { |
|
99 |
return this.members.size(); |
|
100 |
} |
|
137 | 101 |
|
138 |
if (members != null) { |
|
139 |
clone.members = new ArrayList(members.size()); |
|
140 |
for (int i = 0; i < members.size(); i++) { |
|
141 |
clone.members.add(((Cloneable) members.get(i)).clone()); |
|
142 |
} |
|
143 |
} |
|
102 |
public Comparator getFeatureComparator() { |
|
103 |
return new DefaultFeatureComparator(this); |
|
104 |
} |
|
144 | 105 |
|
145 |
return clone;
|
|
146 |
}
|
|
106 |
public Object clone() throws CloneNotSupportedException {
|
|
107 |
FeatureQueryOrder clone = (FeatureQueryOrder) super.clone();
|
|
147 | 108 |
|
148 |
public FeatureQueryOrder getCopy() { |
|
149 |
FeatureQueryOrder aCopy = new FeatureQueryOrder(); |
|
150 |
Iterator iter = this.members.iterator(); |
|
151 |
FeatureQueryOrderMember member; |
|
152 |
while (iter.hasNext()) { |
|
153 |
member = (FeatureQueryOrderMember) iter.next(); |
|
154 |
if (member.hasEvaluator()) { |
|
155 |
aCopy.add(member.getEvaluator(), member.getAscending()); |
|
156 |
} else { |
|
157 |
aCopy.add(member.getAttributeName(), member.getAscending()); |
|
158 |
} |
|
159 |
} |
|
160 |
return aCopy; |
|
161 |
} |
|
109 |
if (members != null) { |
|
110 |
clone.members = new ArrayList(members.size()); |
|
111 |
for (int i = 0; i < members.size(); i++) { |
|
112 |
clone.members.add(((Cloneable) members.get(i)).clone()); |
|
113 |
} |
|
114 |
} |
|
162 | 115 |
|
163 |
public void loadFromState(PersistentState state) |
|
164 |
throws PersistenceException { |
|
165 |
this.members = (List) state.get("menbers"); |
|
166 |
} |
|
116 |
return clone; |
|
117 |
} |
|
167 | 118 |
|
168 |
public void saveToState(PersistentState state) throws PersistenceException { |
|
169 |
state.set("menbers", members); |
|
170 |
} |
|
171 |
|
|
172 |
private class DefaultFeatureComparator implements Comparator { |
|
119 |
public FeatureQueryOrder getCopy() { |
|
120 |
FeatureQueryOrder aCopy = new FeatureQueryOrder(); |
|
121 |
Iterator iter = this.members.iterator(); |
|
122 |
FeatureQueryOrderMember member; |
|
123 |
while (iter.hasNext()) { |
|
124 |
member = (FeatureQueryOrderMember) iter.next(); |
|
125 |
if (member.hasEvaluator()) { |
|
126 |
aCopy.add(member.getEvaluator(), member.getAscending()); |
|
127 |
} else { |
|
128 |
aCopy.add(member.getAttributeName(), member.getAscending()); |
|
129 |
} |
|
130 |
} |
|
131 |
return aCopy; |
|
132 |
} |
|
173 | 133 |
|
174 |
private FeatureQueryOrder order; |
|
134 |
public void loadFromState(PersistentState state) |
|
135 |
throws PersistenceException { |
|
136 |
this.members = (List) state.get("menbers"); |
|
137 |
} |
|
175 | 138 |
|
139 |
public void saveToState(PersistentState state) throws PersistenceException { |
|
140 |
state.set("menbers", members); |
|
141 |
} |
|
176 | 142 |
|
177 |
public DefaultFeatureComparator(FeatureQueryOrder order) { |
|
178 |
this.order = order; |
|
179 |
// TODO optimizar en un array??? |
|
143 |
private class DefaultFeatureComparator implements Comparator { |
|
180 | 144 |
|
181 |
}
|
|
145 |
private FeatureQueryOrder order;
|
|
182 | 146 |
|
147 |
public DefaultFeatureComparator(FeatureQueryOrder order) { |
|
148 |
this.order = order; |
|
149 |
// TODO optimizar en un array??? |
|
183 | 150 |
|
184 |
private int myCompare(Object arg0, Object arg1) { |
|
185 |
if (arg0 == null){ |
|
186 |
if (arg1 == null){ |
|
187 |
return 0; |
|
188 |
} else{ |
|
189 |
return 1; |
|
190 |
} |
|
191 |
} else if (arg1 == null){ |
|
192 |
if (arg0 == null) { |
|
193 |
return 0; |
|
194 |
} else { |
|
195 |
return 1; |
|
196 |
} |
|
197 |
} |
|
198 |
if (arg0 instanceof Comparable) { |
|
199 |
return ((Comparable) arg0).compareTo(arg1); |
|
200 |
} else if (arg1 instanceof Comparable) { |
|
201 |
return ((Comparable) arg1).compareTo(arg0) * -1; |
|
202 |
} |
|
151 |
} |
|
203 | 152 |
|
204 |
if (arg0.equals(arg1)){ |
|
205 |
return 0; |
|
206 |
} else{ |
|
207 |
return -1; |
|
208 |
} |
|
153 |
private int myCompare(Object arg0, Object arg1) { |
|
154 |
if (arg0 == null) { |
|
155 |
if (arg1 == null) { |
|
156 |
return 0; |
|
157 |
} else { |
|
158 |
return 1; |
|
159 |
} |
|
160 |
} else if (arg1 == null) { |
|
161 |
if (arg0 == null) { |
|
162 |
return 0; |
|
163 |
} else { |
|
164 |
return 1; |
|
165 |
} |
|
166 |
} |
|
167 |
if (arg0 instanceof Comparable) { |
|
168 |
return ((Comparable) arg0).compareTo(arg1); |
|
169 |
} else if (arg1 instanceof Comparable) { |
|
170 |
return ((Comparable) arg1).compareTo(arg0) * -1; |
|
171 |
} |
|
209 | 172 |
|
210 |
} |
|
173 |
if (arg0.equals(arg1)) { |
|
174 |
return 0; |
|
175 |
} else { |
|
176 |
return -1; |
|
177 |
} |
|
211 | 178 |
|
212 |
public int compare(Object arg0, Object arg1) { |
|
213 |
Iterator iter = this.order.iterator(); |
|
214 |
int returnValue = 0; |
|
215 |
Feature f0 = (Feature) arg0; |
|
216 |
Feature f1 = (Feature) arg1; |
|
217 |
Object item; |
|
218 |
String attrName; |
|
219 |
Evaluator evaluator; |
|
220 |
while (returnValue == 0 && iter.hasNext()) { |
|
221 |
item = iter.next(); |
|
222 |
if (item instanceof String) { |
|
223 |
attrName = (String) item; |
|
224 |
returnValue = this |
|
225 |
.myCompare(f0.get(attrName), f1 |
|
226 |
.get(attrName)); |
|
227 |
} else { |
|
228 |
evaluator = (Evaluator) item; |
|
229 |
try { |
|
230 |
returnValue = this.myCompare(evaluator |
|
231 |
.evaluate((EvaluatorData) f0), evaluator |
|
232 |
.evaluate((EvaluatorData) f1)); |
|
233 |
} catch (EvaluatorException e) { |
|
234 |
throw new DataEvaluatorRuntimeException(e); |
|
235 |
} |
|
236 |
} |
|
237 |
} |
|
179 |
} |
|
238 | 180 |
|
239 |
return returnValue; |
|
240 |
} |
|
181 |
public int compare(Object arg0, Object arg1) { |
|
182 |
Iterator iter = this.order.iterator(); |
|
183 |
int returnValue = 0; |
|
184 |
Feature f0 = (Feature) arg0; |
|
185 |
Feature f1 = (Feature) arg1; |
|
186 |
Object item; |
|
187 |
String attrName; |
|
188 |
Evaluator evaluator; |
|
189 |
while (returnValue == 0 && iter.hasNext()) { |
|
190 |
item = iter.next(); |
|
191 |
if (item instanceof String) { |
|
192 |
attrName = (String) item; |
|
193 |
returnValue = this |
|
194 |
.myCompare(f0.get(attrName), f1 |
|
195 |
.get(attrName)); |
|
196 |
} else { |
|
197 |
evaluator = (Evaluator) item; |
|
198 |
try { |
|
199 |
returnValue = this.myCompare(evaluator |
|
200 |
.evaluate((EvaluatorData) f0), evaluator |
|
201 |
.evaluate((EvaluatorData) f1)); |
|
202 |
} catch (EvaluatorException e) { |
|
203 |
throw new DataEvaluatorRuntimeException(e); |
|
204 |
} |
|
205 |
} |
|
206 |
} |
|
241 | 207 |
|
242 |
} |
|
208 |
return returnValue; |
|
209 |
} |
|
243 | 210 |
|
244 |
} |
|
211 |
} |
|
212 |
|
|
213 |
} |
Also available in: Unified diff