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 @ 43020
History | View | Annotate | Download (6.73 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 | 43020 | jjdelcerro | public class FeatureQueryOrder implements Persistent, Cloneable, Iterable<FeatureQueryOrderMember> { |
41 | 40435 | jjdelcerro | |
42 | 43020 | jjdelcerro | private List members = new ArrayList(); |
43 | 40435 | jjdelcerro | |
44 | 43020 | jjdelcerro | public Object add(String attributeName, boolean ascending) { |
45 | 40435 | jjdelcerro | FeatureQueryOrderMember member = new FeatureQueryOrderMember(
|
46 | attributeName, ascending); |
||
47 | 43020 | jjdelcerro | if (members.add(member)) {
|
48 | return member;
|
||
49 | } |
||
50 | return null; |
||
51 | } |
||
52 | 40435 | jjdelcerro | |
53 | 43020 | jjdelcerro | 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 | } |
||
62 | 40435 | jjdelcerro | |
63 | 43020 | jjdelcerro | @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 | } |
||
71 | 40435 | jjdelcerro | |
72 | 43020 | jjdelcerro | @Override
|
73 | public FeatureQueryOrderMember next() {
|
||
74 | throw new NoSuchElementException(); |
||
75 | } |
||
76 | 40435 | jjdelcerro | |
77 | 43020 | jjdelcerro | @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 | } |
||
85 | 40435 | jjdelcerro | |
86 | 43020 | jjdelcerro | public boolean remove(FeatureQueryOrderMember member) { |
87 | return members.remove(member);
|
||
88 | } |
||
89 | 40435 | jjdelcerro | |
90 | 43020 | jjdelcerro | public void remove(int index) { |
91 | members.remove(index); |
||
92 | } |
||
93 | 40435 | jjdelcerro | |
94 | 43020 | jjdelcerro | public void clear() { |
95 | members.clear(); |
||
96 | } |
||
97 | 40435 | jjdelcerro | |
98 | 43020 | jjdelcerro | public int size() { |
99 | return this.members.size(); |
||
100 | } |
||
101 | 40435 | jjdelcerro | |
102 | 43020 | jjdelcerro | public Comparator getFeatureComparator() { |
103 | return new DefaultFeatureComparator(this); |
||
104 | } |
||
105 | 40435 | jjdelcerro | |
106 | 43020 | jjdelcerro | public Object clone() throws CloneNotSupportedException { |
107 | FeatureQueryOrder clone = (FeatureQueryOrder) super.clone();
|
||
108 | 40435 | jjdelcerro | |
109 | 43020 | jjdelcerro | 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 | } |
||
115 | 40435 | jjdelcerro | |
116 | 43020 | jjdelcerro | return clone;
|
117 | } |
||
118 | 40435 | jjdelcerro | |
119 | 43020 | jjdelcerro | 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 | } |
||
133 | 40435 | jjdelcerro | |
134 | 43020 | jjdelcerro | public void loadFromState(PersistentState state) |
135 | throws PersistenceException {
|
||
136 | this.members = (List) state.get("menbers"); |
||
137 | } |
||
138 | 40435 | jjdelcerro | |
139 | 43020 | jjdelcerro | public void saveToState(PersistentState state) throws PersistenceException { |
140 | state.set("menbers", members);
|
||
141 | } |
||
142 | 40435 | jjdelcerro | |
143 | 43020 | jjdelcerro | private class DefaultFeatureComparator implements Comparator { |
144 | 40435 | jjdelcerro | |
145 | 43020 | jjdelcerro | private FeatureQueryOrder order;
|
146 | 40435 | jjdelcerro | |
147 | 43020 | jjdelcerro | public DefaultFeatureComparator(FeatureQueryOrder order) {
|
148 | this.order = order;
|
||
149 | // TODO optimizar en un array???
|
||
150 | 40435 | jjdelcerro | |
151 | 43020 | jjdelcerro | } |
152 | 40435 | jjdelcerro | |
153 | 43020 | jjdelcerro | 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 | } |
||
172 | 40435 | jjdelcerro | |
173 | 43020 | jjdelcerro | if (arg0.equals(arg1)) {
|
174 | return 0; |
||
175 | } else {
|
||
176 | return -1; |
||
177 | } |
||
178 | 40435 | jjdelcerro | |
179 | 43020 | jjdelcerro | } |
180 | 40435 | jjdelcerro | |
181 | 43020 | jjdelcerro | 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 | } |
||
207 | 40435 | jjdelcerro | |
208 | 43020 | jjdelcerro | return returnValue;
|
209 | } |
||
210 | 40435 | jjdelcerro | |
211 | 43020 | jjdelcerro | } |
212 | |||
213 | } |