KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * MEDOR: Middleware Enabling Distributed Object Requests
3  *
4  * Copyright (C) 2001-2004 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, S. Chassande-Barrioz
22  */

23
24 package org.objectweb.medor.filter.lib;
25
26 import org.objectweb.medor.api.UnevaluableExpressionException;
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.api.Expression;
34 import org.objectweb.medor.expression.lib.BasicVariableOperand;
35 import org.objectweb.medor.expression.lib.BasicOperator;
36 import org.objectweb.medor.filter.api.FieldOperand;
37 import org.objectweb.medor.query.api.QueryLeaf;
38 import org.objectweb.medor.query.api.QueryTree;
39 import org.objectweb.medor.query.api.QueryTreeField;
40 import org.objectweb.medor.type.lib.PTypeSpaceMedor;
41
42 import java.util.List JavaDoc;
43
44 /**
45  * The MemberOf class is an operator testing whether the value of a list of
46  * Fields (the first n operands, which are supposed to be FieldOperands or
47  * ParameterOperands) are
48  * equal to one of the values of another list of Fields (the second n operands,
49  * which are supposed to be FieldOperands).
50  * <p>As an example, consider the left list (fopA, fopB) and the right list
51  * (fopC, fopD). Consider that fopA and fopB are FieldOperands for fields A and
52  * B of QueryTree qt1 and that fopC and fopD are FieldOperands for fields C and
53  * D of QueryTree qt2.
54  * <p>Consider the following data:
55  * <table border="1">
56  * <tr><td colspan="2"><b>qt1</b></td><td colspan="2"><b>qt2</b></td></tr>
57  * <tr><td width="40"><b>a</b></td><td width="40"><b>b</b></td><td width="40"><b>c</b></td><td width="40"><b>d</b></td></tr>
58  * <tr><td>a1</td><td>b1</td><td>a1</td><td>b1</td></tr>
59  * <tr><td>a2</td><td>b2</td><td>a3</td><td>b3</td></tr>
60  * <tr><td>a3</td><td>b3</td><td>a3</td><td>b3</td></tr>
61  * <tr><td></td><td></td><td>a4</td><td>b4</td></tr>
62  * </table>
63  * <p>(a1,b1) and (a3,b3), values of (a,b), are members of the list of values
64  * for (c,d), and appear in the result only once. Even (a3,b3) which appears
65  * twice in (c,d) is only once in the result. This would not have been the case
66  * with a join.
67  * <p>Note that, in the case of PNames, we only allow comparison of two
68  * PNames, that is, the number of operands is supposed to be two and not more.
69  */

70 public class MemberOf extends BasicOperator implements Operator {
71
72     /**
73      * Constructor for the operator.
74      * The operand array of this operators are the union both list parameter (
75      * left + right)
76      * @param left a list of FieldOperands to test.
77      * @param right the list of FieldOperands of which the left ones should be
78      * members.
79      * @throws org.objectweb.medor.expression.api.MalformedExpressionException
80      */

81     public MemberOf(List JavaDoc left, List JavaDoc right) throws MalformedExpressionException {
82         super(PTypeSpaceMedor.BOOLEAN);
83         int size = left.size();
84         //check that they are the same size
85
if (right.size() != size)
86             throw new MalformedExpressionException(
87                 "Left list has size " + size +
88                 " whereas right list has size " + right.size() +
89                 ". They should be the same.");
90         //check that no two Fields of left and right are built on the same
91
//QueryTree
92
expressions = new Expression[size * 2];
93         for(int i=0; i<size; i++) {
94             expressions[i] = (Expression) left.get(i);
95             expressions[i + size] = (Expression) right.get(i);
96             if (expressions[i] instanceof FieldOperand) {
97                 QueryTree qt1 = ((QueryTreeField) ((FieldOperand)expressions[i]).getField())
98                     .getQueryTree();
99                 QueryTree qt2 = ((QueryTreeField) ((FieldOperand)expressions[i + size]).getField())
100                         .getQueryTree();
101                 if (!(qt1 instanceof QueryLeaf) && (qt1 == qt2))
102                     throw new MalformedExpressionException(
103                         "The MemberOf fields may not be built on the same QueryTree " + qt1);
104             }
105         }
106     }
107
108     //Implementation of the Expression interface
109
public Operand evaluate(ParameterOperand[] pos, Object JavaDoc o)
110         throws ExpressionException {
111         //memory evaluation is not supported yet
112
throw new UnevaluableExpressionException(
113             "Unevaluable Expression: MemberOf currently not implemented");
114     }
115
116     public Operand compileExpression()
117         throws TypingException, MalformedExpressionException {
118         if (expressions != null) {
119             result = new BasicVariableOperand(type);
120             verified = true;
121         }
122         else
123         // Operands not yet assigned
124
throw new MalformedExpressionException("null children value");
125         return result;
126     }
127
128     public Operand getResult() throws IllegalStateException JavaDoc {
129         if (verified) {
130             return result;
131         }
132         else
133             throw new IllegalStateException JavaDoc("Can't get result of an uncompiled expression");
134     }
135
136     //Implementation of the Operator interface
137
public String JavaDoc getOperatorString() {
138         return Operator.MEMBEROF;
139     }
140 }
141
Popular Tags