KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.lsmp.djep.groupJep.interfaces.*;
12
13 import java.util.*;
14 import org.nfunk.jep.*;
15 /**
16  * @author Rich Morris
17  * Created on 05-Mar-2004
18  */

19 public class GMultiply extends PostfixMathCommand {
20     private RingI group = null;
21     /**
22      *
23      */

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

36     public void run(Stack stack) throws ParseException {
37         if(group==null) throw new ParseException("Multiply not implemented for this group.");
38         checkStack(stack);// check the stack
39

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