KickJava   Java API By Example, From Geeks To Geeks.

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


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.jorm.type.api.PType;
27 import org.objectweb.medor.expression.api.ExpressionException;
28 import org.objectweb.medor.expression.api.MalformedExpressionException;
29 import org.objectweb.medor.expression.api.Operator;
30 import org.objectweb.medor.expression.api.ParameterOperand;
31 import org.objectweb.medor.expression.api.TypingException;
32 import org.objectweb.medor.expression.api.Operand;
33 import org.objectweb.medor.expression.lib.BasicBinaryOperator;
34 import org.objectweb.medor.expression.lib.BasicVariableOperand;
35 import org.objectweb.medor.filter.api.FieldOperand;
36 import org.objectweb.medor.type.lib.PTypeSpaceMedor;
37
38 import java.util.Collection JavaDoc;
39 import java.util.Iterator JavaDoc;
40 import java.util.Map JavaDoc;
41
42 /**
43  * The InCollection class is an operator testing whether the value of a
44  * Field (the left operand, which is supposed to be a FieldOperand) is equal to
45  * one of the elements of a collecton (the right Operand)
46  */

47 public class InCollection extends BasicBinaryOperator {
48
49     /**
50      * elemType is the type of the elements of the collection
51      */

52     private PType elemType;
53
54
55     public InCollection(FieldOperand left, Operand right,
56                         PType elemType) {
57         super(PTypeSpaceMedor.BOOLEAN, left, right);
58         this.elemType = elemType;
59     }
60
61     public Object JavaDoc clone(Object JavaDoc clone, Map JavaDoc obj2clone) throws CloneNotSupportedException JavaDoc {
62         clone = super.clone(clone, obj2clone);
63         ((InCollection) clone).elemType = elemType;
64         return clone;
65     }
66
67     /**
68      * Returns the declared PType of the elements.
69      */

70     public PType getElemType() {
71         return elemType;
72     }
73
74     public Operand evaluate(ParameterOperand[] pos, Object JavaDoc o)
75         throws ExpressionException {
76         try {
77             expressions[0].evaluate(pos, o);
78             Collection JavaDoc c = null;
79             if (expressions[1] instanceof ParameterOperand) {
80                 String JavaDoc pn = ((ParameterOperand) expressions[1]).getName();
81                 if (pos == null || pos.length == 0)
82                     throw new ExpressionException("No Parameter");
83                 int i = 0;
84                 while (i < pos.length && !pn.equals(pos[i].getName()))
85                     i++;
86                 if (i < pos.length)
87                     c = (Collection JavaDoc) pos[i].getObject();
88                 else
89                     throw new ExpressionException("Parameter " + pn + " not found");
90             } else
91                 c = (Collection JavaDoc) ((Operand) expressions[1]).getObject();
92             Iterator JavaDoc it = c.iterator();
93             boolean in = false;
94             Object JavaDoc obj = ((FieldOperand)expressions[0]).getObject();
95             while (it.hasNext() && !in)
96                 in = it.next().equals(obj);
97             result.setValue(in);
98         }
99         catch (NullPointerException JavaDoc e) {
100             // result == null or missing children objects
101
throw new IllegalStateException JavaDoc("Unevaluable Expression: Not compiled");
102         }
103         return result;
104     }
105
106     public Operand compileExpression()
107         throws TypingException, MalformedExpressionException {
108         if ((expressions[0] != null) && (expressions[0] != null)) {
109             result = new BasicVariableOperand(type);
110             verified = true;
111         }
112         else
113         // Operands not yet assigned
114
throw new MalformedExpressionException("null children value");
115         return result;
116     }
117
118     public String JavaDoc getOperatorString() {
119         return Operator.IN;
120     }
121 }
122
Popular Tags