KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.calipso.reportgenerator.reportcalculator.arithmetic;
2
3 import java.util.Map JavaDoc;
4 import java.util.Collection JavaDoc;
5 import java.io.Serializable JavaDoc;
6
7 /**
8  * Resuelve expressiones aritmeticas
9  */

10
11 public abstract class ArithmeticExpression implements Serializable JavaDoc{
12
13   /**
14    * Retorna el resultado para la expresion que recibe
15    * por parametro.
16    * @param expression
17    * @return
18    */

19   public static ArithmeticExpression newFrom(String JavaDoc expression) {
20     String JavaDoc exp = prepareExpression(expression);
21     Object JavaDoc[] tokens = getTokens(exp);
22     if (tokens.length == 1) {
23       return ValueArithmeticExp.newValueExpFrom(prepareExpression(tokens[0].toString()));
24     } else {
25       ArithmeticExpression subExp1 = ArithmeticExpression.newFrom(tokens[0].toString());
26       ArithmeticExpression subExp2 = ArithmeticExpression.newFrom(tokens[2].toString());
27       return OperationArithmeticExp.newOperationFrom(subExp1, tokens[1].toString(), subExp2);
28     }
29   }
30
31   /**
32    * Prepara una expression previamente a ser resuelta.
33    * @param expression
34    * @return
35    */

36   private static String JavaDoc prepareExpression(String JavaDoc expression) {
37     String JavaDoc exp = expression.trim();
38     if (exp.charAt(0) == '(' && exp.charAt(exp.length() - 1) == ')') {
39       if(!(exp.indexOf(")") < exp.indexOf("(", 1))){
40         return exp.substring(1, exp.length() - 1);
41       }
42     }
43     return exp;
44   }
45
46   /**
47    * Devuelve el indice del operador primario de la expresion
48    * @param expression
49    * @return
50    */

51   private static int indexOfOperator(String JavaDoc expression) {
52     int index = -1;
53     int alternateIndex = -1;
54     int parenthesis = 0;
55     for (int i = 0; i < expression.length(); i++) {
56       char current = expression.charAt(i);
57       if (current == '(') {
58         parenthesis++;
59       } else {
60         if (current == ')') {
61           parenthesis--;
62         } else {
63           if ((parenthesis == 0)) {
64             if (current == '+' || current == '-') {
65               index = i;
66               break;
67             } else {
68               if (current == '*' || current == '/') {
69                 alternateIndex = i;
70               }
71             }
72           }
73         }
74       }
75     }
76     if (index == -1) {
77       index = alternateIndex;
78     }
79     return index;
80   }
81
82   /**
83    * En base al indice del operador primario parte en dos
84    * la expresion, ubica cada porcion en un array de 3 posiciones
85    * siendo la posicion del medio la que contiene el operador
86    * primario y retorna dicho array.
87    * @param expression
88    * @return
89    */

90   private static Object JavaDoc[] getTokens(String JavaDoc expression) {
91     int index = indexOfOperator(expression);
92     if (index == -1) {
93       return new Object JavaDoc[]{expression};
94     } else {
95       Object JavaDoc[] array = new Object JavaDoc[3];
96       array[0] = expression.substring(0, index);
97       array[1] = expression.substring(index, index +1);
98       array[2] = expression.substring(index + 1);
99       return array;
100     }
101   }
102
103   public abstract float value(Map JavaDoc context);
104
105   public void getVariables(Collection JavaDoc variables) {
106     //Do nothing
107
}
108 }
Popular Tags