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 / DivOperator.java @ 40754
History | View | Annotate | Download (4.16 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 |
/**
|
51 |
* Implements the funcionality of a division operator
|
52 |
*
|
53 |
* @author Pepe Vidal Salvador - jose.vidal.salvador@iver.es
|
54 |
*
|
55 |
*/
|
56 |
public class DivOperator extends Operator { |
57 |
|
58 |
private ArrayList<Expression> arguments = new ArrayList<Expression>(); |
59 |
|
60 |
public String getName() { |
61 |
return OperationTags.DIV_OP;
|
62 |
} |
63 |
|
64 |
public DivOperator(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 value2=((Integer)function.evaluate()); |
85 |
if (value2==null) |
86 |
value2=new Integer(1); |
87 |
intResult /= value2; |
88 |
} |
89 |
|
90 |
return intResult;
|
91 |
|
92 |
|
93 |
} catch (ClassCastException e) { |
94 |
Object value1=((Expression)arguments.get(0)).evaluate(); |
95 |
if (value1==null) |
96 |
return new Double(0); |
97 |
doubleResult = new Double(value1.toString()); |
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 value2=((Expression)function).evaluate(); |
103 |
if (value2==null){ |
104 |
value2=new Double(1); |
105 |
} |
106 |
doubleResult /= new Double(((Expression)function).evaluate().toString()); |
107 |
} |
108 |
|
109 |
return doubleResult;
|
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.DIV_OP +Messages.getText(OperationTags.OPERAND)+ ")\n"+
|
121 |
Messages.getText(OperationTags.OPERAND) +" = "+
|
122 |
Messages.getText(OperationTags.NUMERIC_VALUE); } |
123 |
|
124 |
public ArrayList<Expression> getArguments() { |
125 |
return arguments;
|
126 |
} |
127 |
|
128 |
public void setArguments(ArrayList<Expression> arguments) { |
129 |
this.arguments = arguments;
|
130 |
} |
131 |
|
132 |
public void check() throws ExpressionException { |
133 |
|
134 |
// if (arguments.size() == 2 && arguments.get(1).evaluate(symbol_table).equals(0.0))
|
135 |
// throw new ExpressionException(ExpressionException.DIVIDED_BY_CERO);
|
136 |
|
137 |
|
138 |
for (int i = 0; i < arguments.size(); i++) { |
139 |
if(!(arguments.get(i)instanceof NumericalConstant)) |
140 |
|
141 |
throw new ExpressionException(ExpressionException.CLASS_CASTING_EXCEPTION); |
142 |
|
143 |
|
144 |
Double doubleResult = new Double(((Expression)arguments.get(i)).evaluate().toString()); |
145 |
|
146 |
|
147 |
if((Double.isInfinite(doubleResult)) |
148 |
|| (Double.isNaN(doubleResult))
|
149 |
|| (i!= 0 && doubleResult.equals(0.0))) |
150 |
|
151 |
throw new ExpressionException(ExpressionException.DIVIDED_BY_CERO); |
152 |
|
153 |
} |
154 |
|
155 |
} |
156 |
} |