Statistics
| Revision:

svn-gvsig-desktop / branches / v02_desarrollo / libraries / sld / org.gvsig.sldsupport / org.gvsig.sldsupport.lib / org.gvsig.sldsupport.lib.api / src / main / java / org / gvsig / sldsupport / filterencoding / operations / MultOperator.java @ 40754

History | View | Annotate | Download (3.63 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
 * Implements the funcionality of the multiplication operator
50
 *
51
 * @author Pepe Vidal Salvador - jose.vidal.salvador@iver.es
52
 *
53
 */
54
public class MultOperator extends Operator{
55

    
56

    
57

    
58
        private ArrayList<Expression> arguments = new ArrayList<Expression>();
59

    
60
        public String getName() {
61
                return OperationTags.MULT_OP;
62
        }
63

    
64
        public MultOperator(Hashtable<String, Object> symbol_table) {
65
                super(symbol_table);
66
        }
67

    
68
        public Object evaluate()throws ExpressionException {
69

    
70
                Double doubleResult = null;
71
                Integer intResult = null;
72

    
73

    
74
                try {
75

    
76
                        intResult = (Integer)((Expression)arguments.get(0)).evaluate();
77
                        if (intResult==null)
78
                                return new Integer(0);
79
                        if (arguments.size() == 1)
80
                                return intResult;
81

    
82
                        for (int i = 1; i < arguments.size(); i++){
83
                                Expression function = (Expression)arguments.get(i);
84
                                Integer eval=(Integer)function.evaluate();
85
                                if (eval==null)
86
                                        eval=new Integer(0);
87
                                intResult *= eval;
88
                        }
89

    
90
                        return intResult;
91

    
92

    
93
                } catch (ClassCastException e) {
94

    
95
                        doubleResult = new Double(((Expression)arguments.get(0)).evaluate().toString());
96
                        if (doubleResult==null)
97
                                return new Double(0);
98
                        if(arguments.size() == 1)
99
                                return doubleResult;
100
                        for (int i = 1; i < arguments.size(); i++){
101
                                Expression function = (Expression)arguments.get(i);
102
                                Object eval=function.evaluate();
103
                                if (eval==null)
104
                                        eval=new Double(0);
105
                                doubleResult *= new Double(eval.toString());
106
                        }
107

    
108
                        return doubleResult;
109
                }
110

    
111
        }
112

    
113
        public void addArgument(int i, Expression arg) {
114
                arguments.add(i, arg);
115

    
116
        }
117

    
118
        public String getPattern() {
119
                return "("+Messages.getText(OperationTags.OPERAND)
120
                + OperationTags.MULT_OP +Messages.getText(OperationTags.OPERAND)+ ")\n"+
121
                Messages.getText(OperationTags.OPERAND) +" = "+
122
                Messages.getText(OperationTags.NUMERIC_VALUE);
123
                }
124

    
125
        public ArrayList<Expression> getArguments() {
126
                return arguments;
127
        }
128

    
129
        public void setArguments(ArrayList<Expression> arguments) {
130
                this.arguments = arguments;
131
        }
132

    
133
        public void check() throws ExpressionException {
134

    
135
                for (int i = 0; i < arguments.size(); i++) {
136
                        if(!(arguments.get(i)instanceof NumericalConstant))
137
                                throw new ExpressionException(ExpressionException.CLASS_CASTING_EXCEPTION);
138
                }
139
        }
140
}