KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > medor > eval > lib > UnionEvaluatedTC


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 /**
25  * Package definition.
26  */

27 package org.objectweb.medor.eval.lib;
28
29 import org.objectweb.medor.api.MedorException;
30 import org.objectweb.medor.api.TupleStructure;
31 import org.objectweb.medor.eval.api.BinaryEvaluatedTC;
32 import org.objectweb.medor.eval.api.NodeEvaluator;
33 import org.objectweb.medor.expression.api.ParameterOperand;
34 import org.objectweb.medor.query.api.QueryNode;
35 import org.objectweb.medor.tuple.api.Tuple;
36 import org.objectweb.medor.tuple.api.TupleCollection;
37
38 /**
39  * This class represent a result of a Union operation. The result is equal to the left TupleCollection + the right
40  * TupleCollection in this Order.
41  */

42 class UnionEvaluatedTC
43         extends BasicBinaryEvalutedTC
44         implements BinaryEvaluatedTC {
45
46     private boolean second = false;// This variable tells us if we are iterating over the
47
// left TupleCollection or the right one
48

49     private TupleCollection leftTCResult, rightTCResult;
50     private QueryNode query;
51
52
53     public UnionEvaluatedTC(QueryNode query,
54                             NodeEvaluator leftNodeEvaluator,
55                             NodeEvaluator rightNodeEvaluator,
56                             ParameterOperand[] parameters) throws MedorException {
57         leftTCResult = leftNodeEvaluator.fetchData(parameters);
58         rightTCResult = rightNodeEvaluator.fetchData(parameters);
59         if (leftTCResult.isEmpty()) second = true;
60     }
61
62     public void close() throws MedorException {
63         super.close();
64         if (leftTCResult != null) {
65             leftTCResult.close();
66         }
67         if (rightTCResult != null) {
68             rightTCResult.close();
69         }
70     }
71
72     public TupleStructure getMetaData() throws MedorException {
73         return query.getTupleStructure();
74     }
75
76     public boolean isLast() throws MedorException {
77         boolean end = false;
78         if (!rightTCResult.isEmpty()) {
79             end = rightTCResult.isLast();
80         } else {
81             end = leftTCResult.isLast();
82         }
83         return end;
84     }
85
86     public boolean next() throws MedorException {
87         if (!second) {
88             if (!leftTCResult.next()) {
89                 second = true;
90                 return (!rightTCResult.isEmpty());
91             } else
92                 return true;
93         } else
94             return rightTCResult.next();
95     }
96
97     public void first() throws MedorException {
98         leftTCResult.first();
99         rightTCResult.first();
100         second = false;
101     }
102
103     public int getRow() throws MedorException {
104         if (!second)
105             return leftTCResult.getRow();
106         else
107             return leftTCResult.getRow() + rightTCResult.getRow();
108     }
109
110     public Tuple getTuple() throws MedorException {
111         if (second)
112             return rightTCResult.getTuple();
113         else
114             return leftTCResult.getTuple();
115     }
116
117     public Tuple getTuple(int row) throws MedorException {
118         if (row <=0){
119             throw new MedorException("Invalid Tuple index: "+ row);
120         }else{
121             try{
122                 return leftTCResult.getTuple(row);
123             }catch (MedorException me){
124                 return rightTCResult.getTuple(row - getLeftCard()); // Test in the row value is to be added
125
}
126         }
127     }
128
129     /**
130      * This function will calculate the cardinality of the left TupleCollection
131      * @return int size of the TupleCollection
132      */

133     private int getLeftCard(){
134         return 0; // TO DO
135
}
136
137     public boolean isEmpty() throws MedorException {
138         return (leftTCResult.isEmpty() & rightTCResult.isEmpty());
139     }
140
141     public boolean row(int numTuple) throws MedorException {
142         first();
143         int cpt = 1;
144         boolean go = true;
145         while ((cpt <= numTuple) && (cpt > 1) && go) {
146             if (!next()) go = false;
147         }
148         if (!go)
149             return false;
150         else {
151             return true;
152         }
153     }
154
155     public synchronized int getLeftTCCursor() throws MedorException {
156         return leftTCResult.getRow();
157     }
158
159     public synchronized int getRightTCCursor() throws MedorException {
160         return rightTCResult.getRow();
161     }
162
163 }
164
Popular Tags