KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.nfunk.jep.type.*;
15
16 /**
17  * Implements the comparative operations <, >, <=, >=, != and ==.
18  * Caverts should work where arguments are Double, Complex or String
19  * for the last two only != and == work.
20  * For other types care might be needed.
21  *
22  * @author N Funk and R Morris
23  * @since 2.3.0 beta 1 a bit of a rewrite to make sub classing easier, now allows Complex to be compared to Double i.e. 1+0 i == 1.
24  * @since 2.3.0 beta 2 changed the internal lt,gt,le,ge,ne and eq method to return boolean.
25  * If this breaks anything use
26  * if(lt(obj1,obj2)) inStack.push(new Double(1));
27  * else inStack.push(new Double(0));
28  */

29 public class Comparative extends PostfixMathCommand
30 {
31     int id;
32     double tolerance;
33     public static final int LT = 0;
34     public static final int GT = 1;
35     public static final int LE = 2;
36     public static final int GE = 3;
37     public static final int NE = 4;
38     public static final int EQ = 5;
39
40     public Comparative(int id_in)
41     {
42         id = id_in;
43         numberOfParameters = 2;
44         tolerance = 1e-6;
45     }
46     
47     public void run(Stack inStack) throws ParseException
48     {
49         checkStack(inStack);// check the stack
50

51         Object JavaDoc param2 = inStack.pop();
52         Object JavaDoc param1 = inStack.pop();
53         boolean res=false;
54         switch(id)
55         {
56         case LT: res = lt(param1,param2); break;
57         case GT: res = gt(param1,param2); break;
58         case LE: res = le(param1,param2); break;
59         case GE: res = ge(param1,param2); break;
60         case NE: res = ne(param1,param2); break;
61         case EQ: res = eq(param1,param2); break;
62         }
63         if(res) inStack.push(new Double JavaDoc(1));
64         else inStack.push(new Double JavaDoc(0));
65     }
66     
67     public boolean lt(Object JavaDoc param1,Object JavaDoc param2) throws ParseException
68     {
69         if ((param1 instanceof Complex) || (param2 instanceof Complex))
70             throw new ParseException("< not defined for complex numbers");
71         if ((param1 instanceof Number JavaDoc) && (param2 instanceof Number JavaDoc))
72         {
73             double x = ((Number JavaDoc)param1).doubleValue();
74             double y = ((Number JavaDoc)param2).doubleValue();
75             return (x<y);
76         }
77         throw new ParseException("< not defined for object of type "+param1.getClass().getName()+" and "+param1.getClass().getName());
78     }
79     public boolean gt(Object JavaDoc param1,Object JavaDoc param2) throws ParseException
80     {
81         if ((param1 instanceof Complex) || (param2 instanceof Complex))
82             throw new ParseException("> not defined for complex numbers");
83         if ((param1 instanceof Number JavaDoc) && (param2 instanceof Number JavaDoc))
84         {
85             double x = ((Number JavaDoc)param1).doubleValue();
86             double y = ((Number JavaDoc)param2).doubleValue();
87             return (x>y);
88         }
89         throw new ParseException("> not defined for object of type "+param1.getClass().getName()+" and "+param1.getClass().getName());
90     }
91     public boolean le(Object JavaDoc param1,Object JavaDoc param2) throws ParseException
92     {
93         if ((param1 instanceof Complex) || (param2 instanceof Complex))
94             throw new ParseException("<= not defined for complex numbers");
95         if ((param1 instanceof Number JavaDoc) && (param2 instanceof Number JavaDoc))
96         {
97             double x = ((Number JavaDoc)param1).doubleValue();
98             double y = ((Number JavaDoc)param2).doubleValue();
99             return (x<=y);
100         }
101         throw new ParseException("<= not defined for object of type "+param1.getClass().getName()+" and "+param1.getClass().getName());
102     }
103     public boolean ge(Object JavaDoc param1,Object JavaDoc param2) throws ParseException
104     {
105         if ((param1 instanceof Complex) || (param2 instanceof Complex))
106             throw new ParseException(">= not defined for complex numbers");
107         if ((param1 instanceof Number JavaDoc) && (param2 instanceof Number JavaDoc))
108         {
109             double x = ((Number JavaDoc)param1).doubleValue();
110             double y = ((Number JavaDoc)param2).doubleValue();
111             return (x>=y);
112         }
113         throw new ParseException(">= not defined for object of type "+param1.getClass().getName()+" and "+param1.getClass().getName());
114     }
115
116     public boolean eq(Object JavaDoc param1,Object JavaDoc param2) throws ParseException
117     {
118         if ((param1 instanceof Complex) && (param2 instanceof Complex))
119         {
120             return ((Complex)param1).equals((Complex)param2,tolerance);
121         }
122         if ((param1 instanceof Complex) && (param2 instanceof Double JavaDoc))
123         {
124             return ((Complex)param1).equals(new Complex((Number JavaDoc) param2),tolerance);
125         }
126         if ((param2 instanceof Complex) && (param1 instanceof Double JavaDoc))
127         {
128             return ((Complex)param2).equals(new Complex((Number JavaDoc) param1),tolerance);
129         }
130         return param1.equals(param2);
131     }
132     
133     public boolean ne(Object JavaDoc param1,Object JavaDoc param2) throws ParseException
134     {
135         if ((param1 instanceof Complex) && (param2 instanceof Complex))
136         {
137             return !((Complex)param1).equals((Complex)param2,tolerance);
138         }
139         if ((param1 instanceof Complex) && (param2 instanceof Double JavaDoc))
140         {
141             return !((Complex)param1).equals(new Complex((Number JavaDoc) param2),tolerance);
142         }
143         if ((param2 instanceof Complex) && (param1 instanceof Double JavaDoc))
144         {
145             return !((Complex)param2).equals(new Complex((Number JavaDoc) param1),tolerance);
146         }
147         return !param1.equals(param2);
148     }
149
150
151 /* old code
152         if ((param1 instanceof Complex) || (param2 instanceof Complex))
153             throw new ParseException(">= not defined for complex numbers");
154         if ((param1 instanceof Number) && (param2 instanceof Number))
155         {
156             double x = ((Number)param1).doubleValue();
157             double y = ((Number)param2).doubleValue();
158             int r = (x>=y) ? 1 : 0;
159             return new Double(r);
160         }
161         throw new ParseException(">= not defined for object of type "+param1.getClass().getName()+" and "+param1.getClass().getName());
162     }
163     
164         {
165             int r;
166             
167             switch (id)
168             {
169                 case NE:
170                     r = ((Complex)param1).equals((Complex)param2,tolerance) ? 0 : 1;
171                     break;
172                 case EQ:
173                     r = ((Complex)param1).equals((Complex)param2,tolerance) ? 1 : 0;
174                     break;
175                 default:
176                     throw new ParseException("Relational operator type error");
177             }
178             
179             inStack.push(new Double(r));//push the result on the inStack
180         }
181         else if ((param1 instanceof Number) && (param2 instanceof Number))
182         {
183             double x = ((Number)param1).doubleValue();
184             double y = ((Number)param2).doubleValue();
185             int r;
186             
187             switch (id)
188             {
189                 case LT:
190                     r = (x<y) ? 1 : 0;
191                     break;
192                 case GT:
193                     r = (x>y) ? 1 : 0;
194                     break;
195                 case LE:
196                     r = (x<=y) ? 1 : 0;
197                     break;
198                 case GE:
199                     r = (x>=y) ? 1 : 0;
200                     break;
201                 case NE:
202                     r = (x!=y) ? 1 : 0;
203                     break;
204                 case EQ:
205                     r = (x==y) ? 1 : 0;
206                     break;
207                 default:
208                     throw new ParseException("Unknown relational operator");
209             }
210             
211             inStack.push(new Double(r));//push the result on the inStack
212         }
213         else if ((param1 instanceof String) && (param2 instanceof String))
214         {
215             int r;
216             
217             switch (id)
218             {
219                 case NE:
220                     r = ((String)param1).equals((String)param2) ? 0 : 1;
221                     break;
222                 case EQ:
223                     r = ((String)param1).equals((String)param2) ? 1 : 0;
224                     break;
225                 default:
226                     throw new ParseException("Relational operator type error");
227             }
228             
229             inStack.push(new Double(r));//push the result on the inStack
230         } else
231         {
232             throw new ParseException("Invalid parameter type");
233         }
234         
235         
236         return;
237     }
238     */

239     /**
240      * Returns the tolerance used for comparing complex numbers
241      */

242     public double getTolerance() {
243         return tolerance;
244     }
245
246     /**
247      * Sets the tolerance used for comparing complex numbers
248      * @param d
249      */

250     public void setTolerance(double d) {
251         tolerance = d;
252     }
253
254 }
255
Popular Tags