KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nfunk > jep > OperatorSet


1 /* @author rich
2  * Created on 03-Aug-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.nfunk.jep;
9
10 import org.nfunk.jep.function.*;
11 import org.nfunk.jep.Operator;
12
13 /**
14  * The standard set of operators used in JEP.
15  * <p>
16  *
17  * <pre>
18  * OperatorSet opSet = new OperatorSet();
19  * Operator myOp = opSet.getAdd();
20  * </pre>
21  * <p>
22  * @author Rich Morris
23  * Created on 19-Oct-2003
24  */

25 public class OperatorSet {
26         
27     /** everyone can read but not write these operators **/
28     protected Operator OP_GT = new Operator(">",new Comparative(Comparative.GT));
29     protected Operator OP_LT = new Operator("<",new Comparative(Comparative.LT));
30     protected Operator OP_EQ = new Operator("==",new Comparative(Comparative.EQ));
31     protected Operator OP_LE = new Operator("<=",new Comparative(Comparative.LE));
32     protected Operator OP_GE = new Operator(">=",new Comparative(Comparative.GE));
33     protected Operator OP_NE = new Operator("!=",new Comparative(Comparative.NE));
34
35     protected Operator OP_AND = new Operator("&&",new Logical(0));
36     protected Operator OP_OR = new Operator("||",new Logical(1));
37     protected Operator OP_NOT = new Operator("!",new Not());
38
39     protected Operator OP_ADD = new Operator("+",new Add());
40     protected Operator OP_SUBTRACT = new Operator("-",new Subtract());
41     protected Operator OP_UMINUS = new Operator("UMinus","-",new UMinus());
42
43     protected Operator OP_MULTIPLY = new Operator("*",new Multiply());
44     protected Operator OP_DIVIDE = new Operator("/",new Divide());
45     protected Operator OP_MOD = new Operator("%",new Modulus());
46     /** unary division i.e. 1/x or x^(-1) **/
47     protected Operator OP_UDIVIDE = new Operator("UDivide","^-1",null);
48
49     protected Operator OP_POWER = new Operator("^",new Power());
50
51     protected Operator OP_ASSIGN = new Operator("=",new Assign());
52     protected Operator OP_DOT = new Operator(".",new Dot());
53     protected Operator OP_CROSS = new Operator("^^",new Cross());
54     protected Operator OP_LIST = new Operator("LIST",new List());
55     
56     public OperatorSet()
57     {
58     }
59     
60     /** Gets the list of operators. Note subclasses should override this method. */
61     public Operator[] getOperators() {
62         Operator ops[] = new Operator[]{
63         OP_GT,OP_LT,OP_GE,OP_LE,OP_EQ,OP_NE,OP_AND,OP_OR,OP_NOT,
64         OP_ADD,OP_SUBTRACT,OP_UMINUS,OP_MULTIPLY,
65         OP_DIVIDE,OP_MOD,OP_POWER,
66         OP_ASSIGN,OP_DOT,OP_CROSS,OP_LIST};
67         return ops;
68     }
69
70     public void printOperators()
71     {
72         Operator ops[] = getOperators();
73         for(int i=0;i<ops.length;++i)
74             System.out.println(ops[i].toString());
75     }
76
77     public Operator getAdd() {return OP_ADD; }
78     public Operator getSubtract() {return OP_SUBTRACT; }
79     public Operator getUMinus() {return OP_UMINUS; }
80     public Operator getMultiply() {return OP_MULTIPLY; }
81     public Operator getDivide() {return OP_DIVIDE; }
82     public Operator getMod() {return OP_MOD; }
83     public Operator getPower() {return OP_POWER; }
84
85     public Operator getEQ() {return OP_EQ; }
86     public Operator getNE() {return OP_NE; }
87     public Operator getGE() {return OP_GE; }
88     public Operator getGT() {return OP_GT; }
89     public Operator getLE() {return OP_LE; }
90     public Operator getLT() {return OP_LT; }
91
92     public Operator getAnd() { return OP_AND; }
93     public Operator getOr() {return OP_OR; }
94     public Operator getNot() {return OP_NOT; }
95     public Operator getAssign() {return OP_ASSIGN; }
96     public Operator getDot() {return OP_DOT; }
97     public Operator getCross() {return OP_CROSS; }
98     public Operator getList() {return OP_LIST; }
99
100 }
101
Popular Tags