Statistics
| Revision:

svn-gvsig-desktop / branches / v02_desarrollo / libraries / sld / using-sld-model / org.gvsig.sldsupport / org.gvsig.sldsupport.lib / org.gvsig.sldsupport.lib.api / src / main / java / org / gvsig / sldsupport / filterencoding / operations / MinusOperator.java @ 40758

History | View | Annotate | Download (3.69 KB)

1
/* gvSIG. Sistema de Informaci�n Geogr�fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib��ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package org.gvsig.sldsupport.filterencoding.operations;
42

    
43
import java.util.ArrayList;
44
import java.util.Hashtable;
45

    
46
import org.gvsig.i18n.Messages;
47

    
48

    
49
/**
50
 * Implements the funcionality of the minus operator
51
 *
52
 * @author Pepe Vidal Salvador - jose.vidal.salvador@iver.es
53
 *
54
 */
55

    
56
public class MinusOperator extends Operator {
57

    
58

    
59

    
60
        private ArrayList<Expression> arguments = new ArrayList<Expression>();
61

    
62
        public String getName() {
63
                return OperationTags.MINUS_OP;
64
        }
65

    
66

    
67
        public MinusOperator(Hashtable<String, Object> symbol_table) {
68
                super(symbol_table);
69
        }
70

    
71
        public Object evaluate()throws ExpressionException {
72

    
73
                Double doubleResult = null;
74
                Integer intResult = null;
75

    
76

    
77
                try {
78

    
79
                        intResult = (Integer)((Expression)arguments.get(0)).evaluate();
80
                        if (intResult == null){
81
                                intResult=new Integer(0);
82
                        }
83
                        if (arguments.size() == 1)
84
                                return -1*intResult;
85

    
86
                        for (int i = 1; i < arguments.size(); i++){
87
                                Expression function = (Expression)arguments.get(i);
88
                                Integer eval=((Integer)function.evaluate());
89
                                if (eval==null)
90
                                        eval=new Integer(0);
91
                                intResult -= eval;
92
                        }
93

    
94
                        return intResult;
95

    
96

    
97
                } catch (ClassCastException e) {
98

    
99
                        doubleResult = new Double(((Expression)arguments.get(0)).evaluate().toString());
100
                        if (doubleResult == null){
101
                                doubleResult=new Double(0);
102
                        }
103
                        if(arguments.size() == 1)
104
                                return -1*doubleResult;
105
                        for (int i = 1; i < arguments.size(); i++){
106
                                Expression function = (Expression)arguments.get(i);
107
                                Double eval=(Double)((Expression)function).evaluate();
108
                                if (eval==null)
109
                                        eval=new Double(0);
110
                                doubleResult -= new Double(eval.toString());
111
                        }
112

    
113
                        return doubleResult;
114
                }
115

    
116
        }
117

    
118
        public void addArgument(int i, Expression arg) {
119
                arguments.add(i, arg);
120
        }
121

    
122

    
123
        public String getPattern() {
124
                return "("+Messages.getText(OperationTags.OPERAND)
125
                + OperationTags.MINUS_OP +Messages.getText(OperationTags.OPERAND)+ ")\n"+
126
                Messages.getText(OperationTags.OPERAND) +" = "+
127
                Messages.getText(OperationTags.NUMERIC_VALUE );
128
        }
129

    
130
        public ArrayList<Expression> getArguments() {
131
                return arguments;
132
        }
133

    
134
        public void setArguments(ArrayList<Expression> arguments) {
135
                this.arguments = arguments;
136
        }
137

    
138
        public void check() throws ExpressionException {
139

    
140
                for (int i = 0; i < arguments.size(); i++) {
141
                        if(!(arguments.get(i)instanceof NumericalConstant))
142
                                throw new ExpressionException(ExpressionException.CLASS_CASTING_EXCEPTION);
143
                }
144
        }
145

    
146
}