KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.nfunk.jep.function.*;
12
13 import java.util.*;
14 import org.nfunk.jep.*;
15
16 public class GLogical extends PostfixMathCommand
17 {
18     int id;
19     
20     public GLogical(int id_in)
21     {
22         id = id_in;
23         numberOfParameters = 2;
24     }
25     
26     public void run(Stack inStack)
27         throws ParseException
28     {
29         checkStack(inStack);// check the stack
30

31         Object JavaDoc param2 = inStack.pop();
32         Object JavaDoc param1 = inStack.pop();
33
34         if ((param1 instanceof Boolean JavaDoc) && (param2 instanceof Boolean JavaDoc))
35         {
36             boolean a = ((Boolean JavaDoc)param1).booleanValue();
37             boolean b = ((Boolean JavaDoc)param2).booleanValue();
38             boolean flag=false;
39             
40             switch (id)
41             {
42                 case Logical.AND:
43                     flag = a && b;
44                     break;
45                 case Logical.OR:
46                     flag = a || b;
47                     break;
48                 default:
49                     throw new ParseException("Illegal logical operator");
50             }
51             inStack.push(flag ? Boolean.TRUE : Boolean.FALSE);//push the result on the inStack
52
}
53         else
54         {
55             throw new ParseException("Invalid parameter type");
56         }
57         return;
58     }
59 }
60
Popular Tags