KickJava   Java API By Example, From Geeks To Geeks.

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


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 GPower extends PostfixMathCommand {
20     /** null if power not implemented */
21     private HasPowerI group=null;
22     /**
23      *
24      */

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

37     public void run(Stack stack) throws ParseException {
38         checkStack(stack);// check the stack
39
Object JavaDoc exponant = stack.pop();
40         Object JavaDoc param = stack.pop();
41         Object JavaDoc res = pow(param, exponant);
42         stack.push(res);
43         return;
44     }
45
46     public Object JavaDoc pow(Object JavaDoc param1, Object JavaDoc param2) throws ParseException {
47         if(group==null) throw new ParseException("Power not implemented for this group.");
48         if (param1 instanceof Number JavaDoc) {
49             if (param2 instanceof Number JavaDoc) {
50                 return group.pow((Number JavaDoc)param1, (Number JavaDoc)param2);
51             }
52         }
53         
54         throw new ParseException("Invalid parameter type");
55     }
56 }
57
Popular Tags