KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > medor > query > lib > BasicCalculatedField


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.query.lib;
25
26 import org.objectweb.jorm.type.api.PType;
27 import org.objectweb.medor.api.Field;
28 import org.objectweb.medor.api.MedorException;
29 import org.objectweb.medor.expression.api.BinaryOperator;
30 import org.objectweb.medor.filter.api.FieldOperand;
31 import org.objectweb.medor.expression.api.Operator;
32 import org.objectweb.medor.expression.api.Expression;
33 import org.objectweb.medor.query.api.CalculatedField;
34 import org.objectweb.medor.query.api.QueryTree;
35
36 import java.util.ArrayList JavaDoc;
37 import java.util.Map JavaDoc;
38
39 /**
40  *
41  * @author Sebastien Chassande-Barrioz
42  */

43 public class BasicCalculatedField extends BasicQueryTreeField implements CalculatedField {
44
45     protected Expression e = null;
46
47     public BasicCalculatedField() {
48     }
49
50     public BasicCalculatedField(String JavaDoc name,
51                                 PType type,
52                                 QueryTree qt,
53                                 Expression e) throws MedorException {
54         super(name, type, qt);
55         this.e = e;
56     }
57
58     public BasicCalculatedField(Expression e) {
59         this.e = e;
60     }
61
62     public Object JavaDoc clone(Object JavaDoc clone,
63                         Map JavaDoc obj2clone) throws CloneNotSupportedException JavaDoc {
64         clone = super.clone(clone, obj2clone);
65         ((BasicCalculatedField) clone).e = e;
66         return clone;
67     }
68
69     public Expression getExpression() {
70         return e;
71     }
72
73     public void setExpression(Expression e) {
74         this.e = e;
75     }
76
77     public Field[] getUsedFields() {
78         return (Field[]) getUsed().toArray(new Field[0]);
79     }
80
81     public Field[] getOriginUsedFields() {
82         return (Field[]) getOriginUsedFields(getUsed(), new ArrayList JavaDoc())
83             .toArray(new Field[0]);
84     }
85
86     protected ArrayList JavaDoc getOriginUsedFields(ArrayList JavaDoc src, ArrayList JavaDoc dst) {
87         ArrayList JavaDoc res = dst;
88         for (int i=0; i<src.size(); i++) {
89             Field f = (Field) src.get(i);
90             if (f instanceof BasicPropagatedField) {
91                 res = ((BasicPropagatedField) f).getOriginFields(
92                     ((BasicPropagatedField) f).getPrevious(), res);
93             }
94             else if (f instanceof BasicCalculatedField) {
95                 res = ((BasicCalculatedField) f).getOriginUsedFields(
96                     ((BasicCalculatedField) f).getUsed(), res);
97             }
98             else {
99                 res.add(f);
100             }
101         }
102         return res;
103     }
104
105     public ArrayList JavaDoc getUsed() {
106         ArrayList JavaDoc res = new ArrayList JavaDoc();
107         ArrayList JavaDoc todo = new ArrayList JavaDoc();
108         todo.add(e);
109         while(todo.size()>0) {
110             Expression current = (Expression) todo.remove(0);
111             if (current instanceof BinaryOperator) {
112                 for (int i = 0; i<((Operator) current).getOperandNumber(); i++) {
113                     todo.add( ((Operator) current).getExpression(i) );
114                 }
115             }
116             else if (current instanceof FieldOperand) {
117                 res.add(((FieldOperand) current).getField());
118             }
119         }
120         return res;
121     }
122
123     public short getNullStatus() {
124         // TO DO
125
return 99;
126     }
127 }
128
Popular Tags