KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nfunk > jep > function > Add


1 /*****************************************************************************
2
3 JEP - Java Math Expression Parser 2.3.0
4       October 3 2004
5       (c) Copyright 2004, Nathan Funk and Richard Morris
6       See LICENSE.txt for license information.
7
8 *****************************************************************************/

9
10 package org.nfunk.jep.function;
11
12 import java.util.*;
13 import org.nfunk.jep.*;
14 import org.nfunk.jep.type.*;
15
16 public class Add extends PostfixMathCommand
17 {
18     
19     public Add()
20     {
21         numberOfParameters = -1;
22     }
23     
24     /**
25      * Calculates the result of applying the "+" operator to the arguments from
26      * the stack and pushes it back on the stack.
27      */

28     public void run(Stack stack) throws ParseException {
29         checkStack(stack);// check the stack
30

31         Object JavaDoc sum = stack.pop();
32         Object JavaDoc param;
33         int i = 1;
34         
35         // repeat summation for each one of the current parameters
36
while (i < curNumberOfParameters) {
37             // get the parameter from the stack
38
param = stack.pop();
39             
40             // add it to the sum (order is important for String arguments)
41
sum = add(param, sum);
42             
43             i++;
44         }
45                 
46         stack.push(sum);
47         
48         return;
49     }
50
51     public Object JavaDoc add(Object JavaDoc param1, Object JavaDoc param2) throws ParseException {
52         if (param1 instanceof Complex)
53         {
54             if (param2 instanceof Complex)
55                 return add((Complex)param1, (Complex)param2);
56             else if (param2 instanceof Number JavaDoc)
57                 return add((Complex)param1, (Number JavaDoc)param2);
58         }
59         else if (param1 instanceof Number JavaDoc)
60         {
61             if (param2 instanceof Complex)
62                 return add((Complex)param2, (Number JavaDoc)param1);
63             else if (param2 instanceof Number JavaDoc)
64                 return add((Number JavaDoc)param1, (Number JavaDoc)param2);
65         }
66         else if ((param1 instanceof String JavaDoc) && (param2 instanceof String JavaDoc)) {
67             return (String JavaDoc)param1 + (String JavaDoc)param2;
68         }
69         
70         throw new ParseException("Invalid parameter type");
71     }
72     
73     public Double JavaDoc add(Number JavaDoc d1, Number JavaDoc d2) {
74         return new Double JavaDoc(d1.doubleValue() + d2.doubleValue());
75     }
76     
77     public Complex add(Complex c1, Complex c2) {
78         return new Complex(c1.re() + c2.re(), c1.im() + c2.im());
79     }
80     
81     public Complex add(Complex c, Number JavaDoc d) {
82         return new Complex(c.re() + d.doubleValue(), c.im());
83     }
84 }
85
Popular Tags