Statistics
| Revision:

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 / DefaultFeatureComparator.java @ 40559

History | View | Annotate | Download (2.78 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;
25

    
26
import java.util.Comparator;
27
import java.util.Iterator;
28

    
29
import org.gvsig.fmap.dal.exception.DataEvaluatorRuntimeException;
30
import org.gvsig.fmap.dal.feature.Feature;
31
import org.gvsig.fmap.dal.feature.FeatureQueryOrder;
32
import org.gvsig.tools.evaluator.Evaluator;
33
import org.gvsig.tools.evaluator.EvaluatorData;
34
import org.gvsig.tools.evaluator.EvaluatorException;
35

    
36
public class DefaultFeatureComparator implements Comparator {
37

    
38
        private FeatureQueryOrder order;
39

    
40

    
41
        public DefaultFeatureComparator(FeatureQueryOrder order) {
42
                this.order = order;
43
                // TODO optimizar en un array???
44

    
45
        }
46

    
47

    
48
        private int myCompare(Object arg0, Object arg1) {
49
                if (arg0 == null){
50
                        if (arg1 == null){
51
                                return 0;
52
                        } else{
53
                                return 1;
54
                        }
55
                } else if (arg1 == null){
56
                        if (arg0 == null) {
57
                                return 0;
58
                        } else {
59
                                return 1;
60
                        }
61
                }
62
                if (arg0 instanceof Comparable) {
63
                        return ((Comparable) arg0).compareTo(arg1);
64
                } else if (arg1 instanceof Comparable) {
65
                        return ((Comparable) arg1).compareTo(arg0) * -1;
66
                }
67

    
68
                if (arg0.equals(arg1)){
69
                        return 0;
70
                } else{
71
                        return -1;
72
                }
73

    
74
        }
75

    
76
        public int compare(Object arg0, Object arg1) {
77
                Iterator iter = this.order.iterator();
78
                int returnValue = 0;
79
                Feature f0 = (Feature) arg0;
80
                Feature f1 = (Feature) arg1;
81
                Object item;
82
                String attrName;
83
                Evaluator evaluator;
84
                while (returnValue == 0 && iter.hasNext()) {
85
                        item = iter.next();
86
                        if (item instanceof String) {
87
                                attrName = (String) item;
88
                                returnValue = this
89
                                                .myCompare(f0.get(attrName), f1
90
                                                .get(attrName));
91
                        } else {
92
                                evaluator = (Evaluator) item;
93
                                try {
94
                                        returnValue = this.myCompare(evaluator
95
                                                        .evaluate((EvaluatorData) f0), evaluator
96
                                                        .evaluate((EvaluatorData) f1));
97
                                } catch (EvaluatorException e) {
98
                                        throw new DataEvaluatorRuntimeException(e);
99
                                }
100
                        }
101
                }
102

    
103
                return returnValue;
104
        }
105

    
106
}