svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.labeling.app / org.gvsig.labeling.app.mainplugin / src / main / java / org / gvsig / labeling / operations / DivOperator.java @ 40665
History | View | Annotate | Download (2.96 KB)
1 |
package org.gvsig.labeling.operations; |
---|---|
2 |
|
3 |
import java.util.ArrayList; |
4 |
import java.util.Hashtable; |
5 |
|
6 |
import com.hardcode.gdbms.engine.values.Value; |
7 |
import com.iver.cit.gvsig.fmap.Messages; |
8 |
|
9 |
|
10 |
|
11 |
/**
|
12 |
* Implements the funcionality of a division operator
|
13 |
*
|
14 |
* @author Pepe Vidal Salvador - jose.vidal.salvador@iver.es
|
15 |
*
|
16 |
*/
|
17 |
public class DivOperator extends Operator { |
18 |
|
19 |
private ArrayList<Expression> arguments = new ArrayList<Expression>(); |
20 |
|
21 |
public String getName() { |
22 |
return OperationTags.DIV_OP;
|
23 |
} |
24 |
|
25 |
public DivOperator(Hashtable<String, Value> symbol_table) { |
26 |
super(symbol_table);
|
27 |
} |
28 |
|
29 |
public Object evaluate()throws ExpressionException { |
30 |
|
31 |
Double doubleResult = null; |
32 |
Integer intResult = null; |
33 |
|
34 |
|
35 |
try {
|
36 |
|
37 |
intResult = (Integer)((Expression)arguments.get(0)).evaluate(); |
38 |
if (intResult==null) |
39 |
return new Integer(0); |
40 |
if (arguments.size() == 1) |
41 |
return intResult;
|
42 |
|
43 |
for (int i = 1; i < arguments.size(); i++){ |
44 |
Expression function = (Expression)arguments.get(i); |
45 |
Integer value2=((Integer)function.evaluate()); |
46 |
if (value2==null) |
47 |
value2=new Integer(1); |
48 |
intResult /= value2; |
49 |
} |
50 |
|
51 |
return intResult;
|
52 |
|
53 |
|
54 |
} catch (ClassCastException e) { |
55 |
Object value1=((Expression)arguments.get(0)).evaluate(); |
56 |
if (value1==null) |
57 |
return new Double(0); |
58 |
doubleResult = new Double(value1.toString()); |
59 |
if(arguments.size() == 1) |
60 |
return doubleResult;
|
61 |
for (int i = 1; i < arguments.size(); i++){ |
62 |
Expression function = (Expression)arguments.get(i); |
63 |
Object value2=((Expression)function).evaluate(); |
64 |
if (value2==null){ |
65 |
value2=new Double(1); |
66 |
} |
67 |
doubleResult /= new Double(((Expression)function).evaluate().toString()); |
68 |
} |
69 |
|
70 |
return doubleResult;
|
71 |
} |
72 |
} |
73 |
|
74 |
public void addArgument(int i, Expression arg) { |
75 |
arguments.add(i, arg); |
76 |
|
77 |
} |
78 |
|
79 |
public String getPattern() { |
80 |
return "("+Messages.getString(OperationTags.OPERAND) |
81 |
+ OperationTags.DIV_OP +Messages.getString(OperationTags.OPERAND)+ ")\n"+
|
82 |
Messages.getString(OperationTags.OPERAND) +" = "+
|
83 |
Messages.getString(OperationTags.NUMERIC_VALUE); } |
84 |
|
85 |
public ArrayList<Expression> getArguments() { |
86 |
return arguments;
|
87 |
} |
88 |
|
89 |
public void setArguments(ArrayList<Expression> arguments) { |
90 |
this.arguments = arguments;
|
91 |
} |
92 |
|
93 |
public void check() throws ExpressionException { |
94 |
|
95 |
// if (arguments.size() == 2 && arguments.get(1).evaluate(symbol_table).equals(0.0))
|
96 |
// throw new ExpressionException(ExpressionException.DIVIDED_BY_CERO);
|
97 |
|
98 |
|
99 |
for (int i = 0; i < arguments.size(); i++) { |
100 |
if(!(arguments.get(i)instanceof NumericalConstant)) |
101 |
|
102 |
throw new ExpressionException(ExpressionException.CLASS_CASTING_EXCEPTION); |
103 |
|
104 |
|
105 |
Double doubleResult = new Double(((Expression)arguments.get(i)).evaluate().toString()); |
106 |
|
107 |
|
108 |
if((Double.isInfinite(doubleResult)) |
109 |
|| (Double.isNaN(doubleResult))
|
110 |
|| (i!= 0 && doubleResult.equals(0.0))) |
111 |
|
112 |
throw new ExpressionException(ExpressionException.DIVIDED_BY_CERO); |
113 |
|
114 |
} |
115 |
|
116 |
} |
117 |
} |