User: norbert
Date: 00/05/22 14:44:47
Modified: src/java/org/spyderMQ/selectors Operator.java
Log:
Fix for selectors ( no more instanceof )
Revision Changes Path
1.7 +109 -52 spyderMQ/src/java/org/spyderMQ/selectors/Operator.java
Index: Operator.java
===================================================================
RCS file:
/products/cvs/ejboss/spyderMQ/src/java/org/spyderMQ/selectors/Operator.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- Operator.java 2000/05/22 18:43:37 1.6
+++ Operator.java 2000/05/22 21:44:47 1.7
@@ -11,14 +11,20 @@
*
* @author Norbert Lataille ([EMAIL PROTECTED])
*
- * @version $Revision: 1.6 $
+ * @version $Revision: 1.7 $
*/
public class Operator
{
int operation;
Object oper1;
Object oper2;
- Object oper3;
+ Object oper3;
+
+ Object arg1;
+ Object arg2;
+
+ int class1;
+ int class2;
public final static int EQUAL = 0; //Done for string - integer
public final static int NOT = 1; //Done
@@ -45,6 +51,11 @@
public final static int IN = 22;
public final static int NOT_IN = 23;
+ public final static int STRING = 0;
+ public final static int DOUBLE = 1;
+ public final static int LONG = 2;
+ public final static int BOOLEAN = 3;
+
public Operator(int operation, Object oper1, Object oper2, Object oper3)
{
this.operation=operation;
@@ -71,31 +82,31 @@
Object not() throws Exception
{
- Object arg1=computeArgument(oper1);
+ computeArgument1();
if (arg1==null) return null;
- if (!(arg1 instanceof Boolean)) throw new Exception("NOT: Bad object
type");
+ if (class1!=BOOLEAN) throw new Exception("NOT: Bad object type");
if (((Boolean)arg1).booleanValue()) return Boolean.FALSE;
else return Boolean.TRUE;
}
Object and() throws Exception
{
- Object arg1=computeArgument(oper1);
+ computeArgument1();
if (arg1==null) {
- Object arg2=computeArgument(oper2);
+ computeArgument2();
if (arg2==null) return null;
- if (!(arg2 instanceof Boolean)) throw new Exception("AND: Bad
object type");
+ if (class2!=BOOLEAN) throw new Exception("AND: Bad object
type");
if (!((Boolean)arg2).booleanValue()) return Boolean.FALSE;
return null;
}
- if (arg1 instanceof Boolean) {
+ if (class1==BOOLEAN) {
if (!((Boolean)arg1).booleanValue()) return Boolean.FALSE;
- Object arg2=computeArgument(oper2);
+ computeArgument2();
if (arg2==null) return null;
- if (!(arg2 instanceof Boolean)) throw new Exception("AND: Bad
object type");
+ if (class2!=BOOLEAN) throw new Exception("AND: Bad object
type");
return arg2;
}
@@ -104,21 +115,21 @@
Object or() throws Exception
{
- Object arg1=computeArgument(oper1);
+ computeArgument1();
if (arg1==null) {
- Object arg2=computeArgument(oper2);
+ computeArgument2();
if (arg2==null) return null;
- if (!(arg2 instanceof Boolean)) throw new Exception("OR: Bad
object type");
+ if (class2!=BOOLEAN) throw new Exception("OR: Bad object
type");
if (((Boolean)arg2).booleanValue()) return Boolean.TRUE;
return null;
}
- if (arg1 instanceof Boolean) {
+ if (class1==BOOLEAN) {
if (((Boolean)arg1).booleanValue()) return Boolean.TRUE;
- Object arg2=computeArgument(oper2);
+ computeArgument2();
if (arg2==null) return null;
- if (!(arg2 instanceof Boolean)) throw new Exception("OR: Bad
object type");
+ if (class2!=BOOLEAN) throw new Exception("OR: Bad object
type");
return arg2;
}
@@ -127,34 +138,36 @@
Object equal() throws Exception
{
- Object arg1=computeArgument(oper1);
+ computeArgument1();
if (arg1==null) return Boolean.FALSE;
- if (arg1 instanceof String) {
- Object arg2=computeArgument(oper2);
- if (arg2==null) return Boolean.FALSE;
- if (!(arg2 instanceof String)) throw new Exception("EQUAL: Bad
object type");
- return new Boolean(arg1.equals(arg2));
- } else if (arg1 instanceof Long) {
- Object arg2=computeArgument(oper2);
- if (arg2==null) return Boolean.FALSE;
- if (!(arg2 instanceof Long)) throw new Exception("EQUAL: Bad
object type");
- return new Boolean(arg1.equals(arg2));
+ switch (class1) {
+ case STRING:
+ computeArgument2();
+ if (arg2==null) return Boolean.FALSE;
+ if (class2!=STRING) throw new Exception("EQUAL: Bad
object type");
+ return new Boolean(arg1.equals(arg2));
+ case LONG:
+ computeArgument2();
+ if (arg2==null) return Boolean.FALSE;
+ if (class2!=LONG) throw new Exception("EQUAL: Bad
object type");
+ return new Boolean(arg1.equals(arg2));
+ default:
+ throw new Exception("EQUAL: Bad object type");
}
- throw new Exception("EQUAL: Bad object type");
}
Object gt() throws Exception
{
- Object arg1=computeArgument(oper1);
+ computeArgument1();
if (arg1==null) return Boolean.FALSE;
- if (arg1 instanceof Long) {
- Object arg2=computeArgument(oper2);
+ if (class1==LONG) {
+ computeArgument2();
if (arg2==null) return Boolean.FALSE;
- if (!(arg2 instanceof Long)) throw new Exception("GT: Bad
object type");
+ if (class2!=LONG) throw new Exception("GT: Bad object type");
return new Boolean(((Long)arg1).longValue() >
((Long)arg2).longValue());
}
@@ -163,34 +176,76 @@
Object different() throws Exception
{
- Object arg1=computeArgument(oper1);
+ computeArgument1();
if (arg1==null) return Boolean.FALSE;
- if (arg1 instanceof String) {
- Object arg2=computeArgument(oper2);
- if (arg2==null) return Boolean.FALSE;
- if (!(arg2 instanceof String)) throw new Exception("DIFFERENT:
Bad object type");
- return new Boolean(!arg1.equals(arg2));
- } else if (arg1 instanceof Long) {
- Object arg2=computeArgument(oper2);
- if (arg2==null) return Boolean.FALSE;
- if (!(arg2 instanceof Long)) throw new Exception("DIFFERENT:
Bad object type");
- return new Boolean(!arg1.equals(arg2));
+ switch (class1) {
+ case STRING:
+ computeArgument2();
+ if (arg2==null) return Boolean.FALSE;
+ if (class2!=STRING) throw new Exception("DIFFERENT:
Bad object type");
+ return new Boolean(!arg1.equals(arg2));
+ case LONG:
+ computeArgument2();
+ if (arg2==null) return Boolean.FALSE;
+ if (class2!=LONG) throw new Exception("DIFFERENT: Bad
object type");
+ return new Boolean(!arg1.equals(arg2));
+ default:
+ throw new Exception("DIFFERENT: Bad object type");
}
-
- throw new Exception("DIFFERENT: Bad object type");
}
- Object computeArgument(Object arg) throws Exception
- {
- if (arg instanceof Identifier) {
- return ((Identifier)arg).value;
- } else if (oper1 instanceof Operator) {
- return ((Operator)arg).apply();
- } else return arg;
+ void computeArgument1() throws Exception
+ {
+ String className=oper1.getClass().getName();
+
+ if (className.equals("Identifier")) {
+ arg1=((Identifier)oper1).value;
+ } else if (className.equals("Operator")) {
+ arg1=((Operator)oper1).apply();
+ } else arg1=oper1;
+
+ if (arg1==null) {
+ class1=0;
+ return;
+ }
+
+ className=arg1.getClass().getName();
+
+ if (className.equals("String")) class1=1;
+ else if (className.equals("Double")) class1=2;
+ else if (className.equals("Long")) class1=3;
+ else if (className.equals("Boolean")) class1=4;
+
+ throw new Exception("ARG1: Bad object type");
}
+ void computeArgument2() throws Exception
+ {
+ String className=oper2.getClass().getName();
+
+ if (className.equals("Identifier")) {
+ arg2=((Identifier)oper2).value;
+ } else if (className.equals("Operator")) {
+ arg2=((Operator)oper2).apply();
+ } else arg2=oper2;
+
+ if (arg1==null) {
+ class2=0;
+ return;
+ }
+
+ className=arg2.getClass().getName();
+
+ if (className.equals("String")) class2=1;
+ else if (className.equals("Double")) class2=2;
+ else if (className.equals("Long")) class2=3;
+ else if (className.equals("Boolean")) class2=4;
+
+ throw new Exception("ARG2: Bad object type");
+ }
+
Object apply() throws Exception
{
@@ -209,6 +264,8 @@
}
+
+
//--
public String toString()