KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > medor > filter > lib > IsNull


1 /**
2  * MEDOR: Middleware Enabling Distributed Object Requests
3  *
4  * Copyright (C) 2001-2003 France Telecom R&D
5  * Contact: alexandre.lefebvre@rd.francetelecom.com
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  *
21  * Initial developers: M. Alia, A. Lefebvre
22  */

23
24 package org.objectweb.medor.filter.lib;
25
26 import org.objectweb.medor.expression.api.ExpressionException;
27 import org.objectweb.medor.expression.api.MalformedExpressionException;
28 import org.objectweb.medor.expression.api.ParameterOperand;
29 import org.objectweb.medor.expression.api.TypingException;
30 import org.objectweb.medor.expression.api.UnaryOperator;
31 import org.objectweb.medor.expression.api.Expression;
32 import org.objectweb.medor.expression.api.Operand;
33 import org.objectweb.medor.expression.lib.BasicUnaryOperator;
34 import org.objectweb.medor.expression.lib.BasicVariableOperand;
35 import org.objectweb.medor.expression.type.ExpressionTypeHelper;
36 import org.objectweb.medor.type.lib.PTypeSpaceMedor;
37 import org.objectweb.medor.type.lib.QTypeTuple;
38
39 import java.util.Map JavaDoc;
40
41 /**
42  * IsNull operates on an Expression. Its evaluation returns true if the
43  * Expression is null, false otherwise.
44  */

45 public class IsNull extends BasicUnaryOperator implements UnaryOperator {
46
47     boolean not = false;
48
49     public IsNull() {
50         super(PTypeSpaceMedor.BOOLEAN);
51     }
52
53     public IsNull(Expression e) {
54         super(PTypeSpaceMedor.BOOLEAN, e);
55     }
56
57     public IsNull(Expression e, boolean not) {
58         super(PTypeSpaceMedor.BOOLEAN, e);
59         this.not = not;
60     }
61
62     public Object JavaDoc clone(Object JavaDoc clone, Map JavaDoc obj2clone) throws CloneNotSupportedException JavaDoc {
63         clone = super.clone(clone, obj2clone);
64         ((IsNull) clone).not = not;
65         return clone;
66     }
67
68     public void setNot(boolean not) {
69         this.not = not;
70     }
71
72     public boolean getNot() {
73         return not;
74     }
75
76     public org.objectweb.medor.expression.api.Operand evaluate(ParameterOperand[] pos, Object JavaDoc o)
77         throws ExpressionException {
78         try {
79             Operand subResult = expressions[0].evaluate(pos, o);
80             switch (subResult.getType().getTypeCode()) {
81             case QTypeTuple.TYPECODE_OBJBOOLEAN:
82             case QTypeTuple.TYPECODE_OBJCHAR:
83             case QTypeTuple.TYPECODE_OBJBYTE:
84             case QTypeTuple.TYPECODE_OBJSHORT:
85             case QTypeTuple.TYPECODE_OBJINT:
86             case QTypeTuple.TYPECODE_OBJLONG:
87             case QTypeTuple.TYPECODE_OBJFLOAT:
88             case QTypeTuple.TYPECODE_OBJDOUBLE:
89             case QTypeTuple.TYPECODE_BIGDECIMAL:
90             case QTypeTuple.TYPECODE_BIGINTEGER:
91             case QTypeTuple.TYPECODE_STRING:
92             case QTypeTuple.TYPECODE_DATE:
93             case QTypeTuple.TYPECODE_CHARARRAY:
94             case QTypeTuple.TYPECODE_BYTEARRAY:
95             case QTypeTuple.TYPECODE_SERIALIZED:
96             case QTypeTuple.TYPECODE_REFERENCE:
97             case QTypeTuple.TYPECODE_TUPLE:
98             case QTypeTuple.TYPECODE_TUPLE_COLLECTION:
99                 result.setValue(evaluate(subResult.getObject()));
100                 break;
101             default:
102                 throw new MalformedExpressionException(
103                     "Unauthorized expression element");
104             }
105         }
106         catch (NullPointerException JavaDoc e) {
107             throw new ExpressionException("Unevaluable Expression: Not compiled");
108         }
109         return result;
110     }
111
112     public boolean evaluate(Object JavaDoc op) {
113         return (not ? op != null : op == null);
114     }
115
116     public Operand compileExpression()
117         throws ExpressionException, MalformedExpressionException {
118         if (expressions[0] != null) {
119             expressions[0].compileExpression();
120             if ((ExpressionTypeHelper.isArithmeticType(expressions[0].getType())) ||
121                 (expressions[0].getType().getTypeCode()==QTypeTuple.TYPECODE_BOOLEAN) ||
122                 (expressions[0].getType().getTypeCode()==QTypeTuple.TYPECODE_CHAR) ||
123                 (expressions[0].getType().getTypeCode()==QTypeTuple.TYPECODE_BYTE)) {
124                 throw new TypingException("IsNull does not work on primitive types");
125             }
126             else {
127                 result = new BasicVariableOperand(type);
128                 verified = true;
129             }
130         }
131         else
132         //Operands not yet assigned
133
throw new MalformedExpressionException("null children value");
134         return result;
135     }
136
137     public String JavaDoc getOperatorString() {
138         return (not ? "IS NOT NULL" : "IS NULL");
139     }
140 }
141
Popular Tags