KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nfunk > jep > function > Logical


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.nfunk.jep.function;
11
12 import java.util.*;
13 import org.nfunk.jep.*;
14
15 public class Logical extends PostfixMathCommand
16 {
17     int id;
18     public static final int AND = 0;
19     public static final int OR = 1;
20     public Logical(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         
35         if ((param1 instanceof Number JavaDoc) && (param2 instanceof Number JavaDoc))
36         {
37             double x = ((Number JavaDoc)param1).doubleValue();
38             double y = ((Number JavaDoc)param2).doubleValue();
39             int r;
40             
41             switch (id)
42             {
43                 case 0:
44                     // AND
45
r = ((x!=0d) && (y!=0d)) ? 1 : 0;
46                     break;
47                 case 1:
48                     // OR
49
r = ((x!=0d) || (y!=0d)) ? 1 : 0;
50                     break;
51                 default:
52                     r = 0;
53             }
54             
55             inStack.push(new Double JavaDoc(r)); // push the result on the inStack
56
}
57         else
58         {
59             throw new ParseException("Invalid parameter type");
60         }
61         return;
62     }
63 }
64
Popular Tags