KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lsmp > djep > groupJep > function > GAdd


1 /* @author rich
2  * Created on 05-Mar-2004
3  *
4  * This code is covered by a Creative Commons
5  * Attribution, Non Commercial, Share Alike license
6  * <a HREF="http://creativecommons.org/licenses/by-nc-sa/1.0">License</a>
7  */

8 package org.lsmp.djep.groupJep.function;
9 import org.nfunk.jep.function.*;
10 import org.lsmp.djep.groupJep.*;
11
12 import java.util.*;
13 import org.nfunk.jep.*;
14 /**
15  * @author Rich Morris
16  * Created on 05-Mar-2004
17  */

18 public class GAdd extends PostfixMathCommand {
19     private GroupI group;
20     /**
21      *
22      */

23     private GAdd() { }
24     public GAdd(GroupI group)
25     {
26         numberOfParameters = -1;
27         this.group = group;
28     }
29
30     /**
31      * Calculates the result of applying the "+" operator to the arguments from
32      * the stack and pushes it back on the stack.
33      */

34     public void run(Stack stack) throws ParseException {
35         checkStack(stack);// check the stack
36

37         Object JavaDoc sum = stack.pop();
38         Object JavaDoc param;
39         int i = 1;
40         
41         // repeat summation for each one of the current parameters
42
while (i < curNumberOfParameters) {
43             // get the parameter from the stack
44
param = stack.pop();
45             // add it to the sum (order is important for String arguments)
46
sum = add(param, sum);
47             i++;
48         }
49         stack.push(sum);
50         return;
51     }
52
53     public Object JavaDoc add(Object JavaDoc param1, Object JavaDoc param2) throws ParseException {
54         if (param1 instanceof Number JavaDoc) {
55             if (param2 instanceof Number JavaDoc) {
56                 return group.add((Number JavaDoc)param1, (Number JavaDoc)param2);
57             }
58         }
59         
60         throw new ParseException("Invalid parameter type");
61     }
62 }
63
Popular Tags