KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*****************************************************************************
2
3 JEP - Java Math Expression Parser 2.3.0
4       October 3 2004
5       (c) Copyright 2004, Nathan Funk and Richard Morris
6       See LICENSE.txt for license information.
7
8 *****************************************************************************/

9
10 package org.lsmp.djep.groupJep.function;
11
12 import java.util.*;
13 import org.nfunk.jep.*;
14 import org.nfunk.jep.function.*;
15 import org.lsmp.djep.groupJep.*;
16 import org.lsmp.djep.groupJep.interfaces.*;
17
18 /**
19  * Implements logical operations on a a group.
20  * Will always return Boolean results.
21  *
22  * @author Rich Morris
23  * Created on 06-Mar-2004
24  */

25 public class GComparative extends PostfixMathCommand
26 {
27     int id;
28     GroupI group;
29         
30     public GComparative(GroupI group,int id_in)
31     {
32         id = id_in;
33         numberOfParameters = 2;
34         this.group = group;
35     }
36     
37     public void run(Stack inStack)
38         throws ParseException
39     {
40         checkStack(inStack);// check the stack
41

42         Object JavaDoc param2 = inStack.pop();
43         Object JavaDoc param1 = inStack.pop();
44         
45         if ((param1 instanceof Number JavaDoc) && (param2 instanceof Number JavaDoc))
46         {
47             Number JavaDoc num1 = (Number JavaDoc)param1;
48             Number JavaDoc num2 = (Number JavaDoc)param2;
49             boolean flag;
50             if(group instanceof OrderedSetI)
51             {
52                 int comp = ((OrderedSetI) group).compare(num1,num2);
53                 switch (id)
54                 {
55                     case Comparative.LT:
56                         flag = comp < 0;
57                         break;
58                     case Comparative.GT:
59                         flag = comp > 0;
60                         break;
61                     case Comparative.LE:
62                         flag = comp <= 0;
63                         break;
64                     case Comparative.GE:
65                         flag = comp >= 0;
66                         break;
67                     case Comparative.NE:
68                         flag = comp != 0;
69                         break;
70                     case Comparative.EQ:
71                         flag = comp == 0;
72                         break;
73                     default:
74                         throw new ParseException("Unknown relational operator");
75                 }
76             }
77             else
78             {
79                 switch (id)
80                 {
81                 case Comparative.NE:
82                     flag = !group.equals(num1,num2);
83                     break;
84                 case Comparative.EQ:
85                     flag = group.equals(num1,num2);
86                     break;
87                 default:
88                     throw new ParseException("Unknown relational operator");
89                 }
90             }
91             inStack.push(flag ? Boolean.TRUE : Boolean.FALSE);//push the result on the inStack
92
}
93         else if ((param1 instanceof Boolean JavaDoc) && (param2 instanceof Boolean JavaDoc))
94         {
95             boolean num1 = ((Boolean JavaDoc)param1).booleanValue();
96             boolean num2 = ((Boolean JavaDoc)param2).booleanValue();
97             boolean flag;
98             switch (id)
99             {
100             case Comparative.NE:
101                 flag = num1 != num2;
102                 break;
103             case Comparative.EQ:
104                 flag = num1 == num2;
105                 break;
106             default:
107                 throw new ParseException("Unknown relational operator");
108             }
109             inStack.push(flag ? Boolean.TRUE : Boolean.FALSE);//push the result on the inStack
110
}
111         else throw new ParseException("Invalid parameters for comparitive op");
112             
113     }
114 }
115
Popular Tags