KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lsmp > djep > groupJep > GroupJep


1 /* @author rich
2  * Created on 19-Dec-2003
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;
9
10 import org.nfunk.jep.*;
11 import org.nfunk.jep.type.Complex;
12 import org.lsmp.djep.groupJep.values.*;
13 /**
14  * An extension of JEP which allows calculations over arbitary groups,
15  * such as the integers(exact answers) and rationals.
16  *
17  * @author Rich Morris
18  * Created on 19-Dec-2003
19  */

20 public class GroupJep extends JEP {
21     protected GroupI group;
22     /**
23      * Create a new GroupJep instance with calculations over the given group.
24      *
25      * @param group The group to calculate over.
26      */

27     public GroupJep(GroupI group) {
28         super();
29         this.group = group;
30         super.numberFactory = group.getNumberFactory();
31         opSet = new GOperatorSet(group);
32     }
33     private GroupJep() {}
34     public void addStandardFunctions()
35     {
36 // Certinally don't want the jep standard functions
37
// super.addStandardFunctions();
38
group.addStandardFunctions(this);
39     }
40
41     public void addStandardConstants()
42     {
43         group.addStandardConstants(this);
44     }
45
46     public GroupJep(JEP j) {
47         super(j);
48     }
49
50     public GroupI getGroup()
51     {
52         return group;
53     }
54
55     /**
56      * Calcuates the value of the expression and returns the
57      * result as a complex number.
58      */

59     public Complex getComplexValue() {
60         Object JavaDoc num = this.getValueAsObject();
61         if(num instanceof Complex) return (Complex) num;
62         else if(num instanceof HasComplexValueI)
63             return ((HasComplexValueI) num).getComplexValue();
64         else if(num instanceof Number JavaDoc)
65             return new Complex((Number JavaDoc) num);
66         return super.getComplexValue();
67     }
68
69     /**
70      * A utility function which returns the complex aproximation of a number.
71      * @see HasComplexValueI
72      * @param num the object to be converted
73      * @return the complex aproximation or null if conversion to complex is not posible.
74      **/

75     public static Complex getComplexValue(Object JavaDoc num)
76     {
77         if(num instanceof Complex) return (Complex) num;
78         else if(num instanceof HasComplexValueI)
79             return ((HasComplexValueI) num).getComplexValue();
80         else if(num instanceof Number JavaDoc)
81             return new Complex((Number JavaDoc) num);
82         else return null;
83     }
84
85 }
86
Popular Tags