KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > calipso > reportgenerator > reportcalculator > arithmetic > OperationArithmeticExp


1 package com.calipso.reportgenerator.reportcalculator.arithmetic;
2
3 import com.calipso.reportgenerator.reportcalculator.arithmetic.ArithmeticExpression;
4
5 import java.util.Map JavaDoc;
6 import java.util.HashMap JavaDoc;
7 import java.util.Collection JavaDoc;
8 import java.io.Serializable JavaDoc;
9
10 /**
11  * Genera las operaciones
12  */

13
14 public class OperationArithmeticExp extends ArithmeticExpression implements Serializable JavaDoc {
15    private ArithmeticExpression m1;
16    private ArithmeticExpression m2;
17    private String JavaDoc operator;
18    private static Map JavaDoc operations;
19
20   /**
21    * Crea un Map y lo inicializa con todos los resultados
22    * posibles en base a las operaciones soportadas.
23    * @param operator
24    * @return
25    */

26   private Operation fromOperator(String JavaDoc operator) {
27       if (operations == null){
28         operations = new HashMap JavaDoc();
29         operations.put("+", new Addition());
30         operations.put("-", new Substraction());
31         operations.put("*", new Multiplication());
32         operations.put("/", new Divission());
33       }
34       return (Operation) operations.get(operator);
35    }
36
37     /**
38      * Retorna el resultado a partir de un Map.
39      * @param context
40      * @return
41      */

42     public float value(Map JavaDoc context) {
43         return fromOperator(operator).calculate(m1.value(context), m2.value(context));
44     }
45
46     /**
47      * Metodo toString de la clase
48      * @return
49      */

50     public String JavaDoc toString() {
51       String JavaDoc str = m1.toString() + " " + operator + " " + m2.toString();
52       if (operator.equals("+") || operator.equals("-")) {
53         return "(" + str + ")";
54       }
55       return str;
56     }
57
58     /**
59      * Genera una nueva instancia de <code>OperationArithmeticExp</code>
60      * en base a dos valores y su operador.
61      * @param subExp1
62      * @param operator
63      * @param subExp2
64      * @return
65      */

66     public static ArithmeticExpression newOperationFrom(ArithmeticExpression subExp1, String JavaDoc operator, ArithmeticExpression subExp2) {
67         return new OperationArithmeticExp(subExp1, operator, subExp2);
68     }
69
70     /**
71      * Inicializa una instancia de <code>OperationArithmeticExp</code>
72      * @param subExp1
73      * @param operator
74      * @param subExp2
75      */

76     public OperationArithmeticExp(ArithmeticExpression subExp1, String JavaDoc operator, ArithmeticExpression subExp2) {
77         this.m1 = subExp1;
78         this.m2 = subExp2;
79         this.operator = operator;
80     }
81
82     /**
83      * Inner class que calcula para dos valores
84      * todas las operaciones soportadas
85      */

86     private abstract class Operation {
87       public abstract float calculate(float value1, float value2);
88     }
89
90     /**
91      * Inner class que calcula la suma para dos valores
92      */

93     private class Addition extends Operation {
94         public float calculate(float value1, float value2){
95             return value1 + value2;
96         }
97     }
98
99     /**
100      * Inner class que calcula la resta para dos valores
101      */

102     private class Substraction extends Operation {
103         public float calculate(float value1, float value2){
104             return value1 - value2;
105         }
106     }
107
108     /**
109      * Inner class que calcula la multiplicacion para dos valores
110      */

111     private class Multiplication extends Operation {
112         public float calculate(float value1, float value2){
113             return value1 * value2;
114         }
115     }
116
117     /**
118      * Inner class que calcula la division para dos valores
119      */

120     private class Divission extends Operation {
121         public float calculate(float value1, float value2){
122             return value1 / value2;
123         }
124     }
125
126   public void getVariables(Collection JavaDoc variables) {
127     m1.getVariables(variables);
128     m2.getVariables(variables);
129   }
130
131 }
Popular Tags