KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > medor > tuple > lib > ExplicitTupleCollection


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.tuple.lib;
25
26
27 import org.objectweb.medor.clone.api.Cloneable;
28 import org.objectweb.medor.clone.lib.BasicCloneable;
29 import org.objectweb.medor.api.MedorException;
30 import org.objectweb.medor.api.TupleStructure;
31 import org.objectweb.medor.tuple.api.Tuple;
32 import org.objectweb.medor.tuple.api.TupleCollection;
33
34 import java.util.Collection JavaDoc;
35 import java.util.Date JavaDoc;
36 import java.util.Iterator JavaDoc;
37 import java.util.LinkedList JavaDoc;
38 import java.util.List JavaDoc;
39 import java.util.Map JavaDoc;
40
41 /**
42  * This interface is an implementation of the TupleCollection interface using
43  * Java collections. It can be used when we have obejcts fetched in the memory.
44  */

45
46 public class ExplicitTupleCollection
47     extends BasicCloneable
48     implements TupleCollection {
49
50     private TupleStructure schema;
51     private List JavaDoc tuples;
52     private int cursor;
53     boolean top = true;
54     boolean end = false;
55     boolean closed = false;
56
57     public ExplicitTupleCollection() {
58     }
59
60     public ExplicitTupleCollection(TupleStructure schema) throws MedorException {
61         this.schema = schema;
62         tuples = new LinkedList JavaDoc();
63         cursor = 0;
64     }
65
66     public Object JavaDoc clone(Object JavaDoc clone,
67                         Map JavaDoc obj2clone) throws CloneNotSupportedException JavaDoc {
68         clone = super.clone(clone, obj2clone);
69         ExplicitTupleCollection tc = (ExplicitTupleCollection) clone;
70         tc.closed = closed;
71         tc.end = end;
72         tc.top = top;
73         tc.cursor = cursor;
74         tc.schema = (TupleStructure) getClone(schema, obj2clone);
75         if (tuples != null) {
76             tuples = new LinkedList JavaDoc();
77             for(Iterator JavaDoc it = tuples.iterator(); it.hasNext();) {
78                 tc.tuples.add(getClone((Cloneable JavaDoc) it.next(), obj2clone));
79             }
80         }
81         return clone;
82     }
83
84     public void close() throws MedorException {
85         closed = true;
86     }
87
88     public synchronized TupleStructure getMetaData() throws MedorException {
89         if (closed) {
90             throw new MedorException("Impossible to use a closed TupleCollection");
91         }
92         return schema;
93     }
94
95     public synchronized boolean isEmpty() throws MedorException {
96         if (closed) {
97             throw new MedorException("Impossible to use a closed TupleCollection");
98         }
99         return (tuples.size() == 0);
100     }
101
102
103     public synchronized boolean isLast() throws MedorException {
104         if (closed) {
105             throw new MedorException("Impossible to use a closed TupleCollection");
106         }
107         // Testing if the TC is empty and if we are out of of bound of the TC(cursor == -1)
108
if ((cursor == -1) || (tuples.size() == 0))
109             throw new MedorException(" No elements fetched in this tupleCollection");
110         else
111             return (tuples.size() == getRow());
112     }
113
114
115     public synchronized int size() {
116         return tuples.size();
117     }
118
119     public synchronized boolean next() throws MedorException {
120         if (closed) {
121             throw new MedorException("Impossible to use a closed TupleCollection");
122         }
123         if (!(tuples.size() == 0)) { // Not empty
124
if (cursor == tuples.size()) { // We are at the end of this TC object
125
end = true; // the cursor is out of the bound of thIS tC object
126
return false;
127             } else if (top) {
128                 cursor = 1; // The first tuple
129
top = false;
130                 return true;
131             } else {
132                 cursor++;
133                 return true;
134             }
135         } else
136             return false;
137     }
138
139     public synchronized boolean previous() throws MedorException {
140         boolean prev = true;
141         if (!isEmpty()) {
142             if (end) {
143                 end = false;
144                 prev = true;
145             } else if (getRow() == 1) {
146                 top = true;
147                 prev = false;
148             } else {
149                 cursor--;
150                 prev = true;
151             }
152         } else {
153             prev = false;
154         }
155         return prev;
156     }
157
158     public synchronized boolean row(int row) throws MedorException {
159         if (closed) {
160             throw new MedorException("Impossible to use a closed TupleCollection");
161         }
162         if ((row >= 1) && (row <= tuples.size())) {
163             cursor = row;
164             return true;
165         } else
166             return false;
167     }
168
169     public synchronized void first() throws MedorException {
170         if (closed) {
171             throw new MedorException("Impossible to use a closed TupleCollection");
172         }
173         if (!isEmpty()) {
174             end = false;
175             top = false;
176             cursor = 1;
177         }
178     }
179
180     public synchronized int getRow() throws MedorException {
181         if (closed) {
182             throw new MedorException("Impossible to use a closed TupleCollection");
183         }
184         if (isEmpty() || top || end)
185             return -1;
186         else
187             return cursor;
188     }
189
190     /**
191      * Insert the tuple T at the designated row number of the TupleCollection
192      * @param i the first tuple is 1, the second is 2, ...
193      * @param T is the tuple to be inserted
194      * @throws MedorException if data source access error or invalid tuple number
195      */

196     public synchronized void insertTuple(int i, Tuple T) throws MedorException {
197         if ((i >= 1) && (i <= size()))
198             tuples.add((i - 1), T);
199         else
200             throw new MedorException("its impossible to insert in this cursor: " + i);
201     }
202
203     /**
204      * Insert the tuple T at the last of this TupleCollection object
205      * @param T is the tuple to be inserted
206      * @throws MedorException if data source access error
207      */

208     public synchronized void insertTuple(Tuple T) throws MedorException {
209         tuples.add(T);
210     }
211
212     /**
213      * Remove the tuple of the designated row number from the TupleCollection object
214      * @param row the first tuple is 1, the second is 2, ...
215      * @throws MedorException if data source access error or invalid tuple number
216      */

217     public synchronized void removeTuple(int row) throws MedorException {
218         if ((row >= 1) && (row <= size())) {
219             tuples.remove(row - 1);
220             if (tuples.size() == 0) {
221                 end = false;
222                 cursor = -1;
223             } else
224                 cursor = row - 1;
225         } else
226             throw new MedorException("its impossible to remove from this cursor");
227     }
228
229     /**
230      * Returns the value of the designated row number of the TupleCollection
231      * as a Tuple Object.
232      * @param i the first tuple is 1, the second is 2, ...
233      * @return a Tuple Object
234      * @throws MedorException if data source access error
235      */

236     public synchronized Tuple getTuple(int i) throws MedorException {
237         if (closed) {
238             throw new MedorException("Impossible to use a closed TupleCollection");
239         }
240         if (i > 0 && i <= tuples.size()) {
241             return (Tuple) tuples.get(i - 1);
242         } else
243             throw new MedorException("its impossible de get tuple from this cursor " + i);
244
245     }
246
247     public synchronized Tuple getTuple() throws MedorException {
248         if (closed) {
249             throw new MedorException("Impossible to use a closed TupleCollection");
250         }
251         if ((!isEmpty()) && (!top) && (!end)) {
252             return (Tuple) tuples.get(cursor - 1);
253
254         } else
255             throw new MedorException("No elements fetched in this tupleCollection");
256     }
257
258     /**
259      * Returns the value of the designated column in the current row of this Tuple
260      * as a Boolean of java programming language
261      * @param i the first column is 1, the second is 2, ...
262      * @return a boolean value
263      * @throws MedorException if data source access error or invalid cursor
264      */

265     public synchronized boolean getBoolean(int i) throws MedorException {
266         if (!isEmpty() && !top && !end) {
267             return this.getTuple().getBoolean(i);
268         } else
269             throw new MedorException("No elements fetched in this tupleCollection");
270     }
271
272     /**
273      * Returns the value of the designated column in the current row of this Tuple
274      * as a Byte of java programming language
275      * @param i the first column is 1, the second is 2, ...
276      * @return a byte value
277      * @throws MedorException if data source access error or invalid cursor
278      */

279     public synchronized byte getByte(int i) throws MedorException {
280         if (!isEmpty() && !top && !end) {
281             return this.getTuple().getByte(i);
282         } else
283             throw new MedorException("No elements fetched in this tupleCollection");
284     }
285
286     /**
287      * Returns the value of the designated column in the current row of this Tuple
288      * as a java.util.Date of java programming language
289      * @param i the first column is 1, the second is 2, ...
290      * @return a Date value as SQL type
291      * @throws MedorException if data source access error or invalid cursor
292      */

293     public synchronized Date JavaDoc getDate(int i) throws MedorException {
294         if (!isEmpty() && !top && !end) {
295             return this.getTuple().getDate(i);
296         } else
297             throw new MedorException("No elements fetched in this tupleCollection");
298     }
299
300     /**
301      * Returns the value of the designated column in the current row of this Tuple
302      * as a double of java programming language
303      * @param i the first column is 1, the second is 2, ...
304      * @return a double value
305      * @throws MedorException if data source access error or invalid cursor
306      */

307     public synchronized double getDouble(int i) throws MedorException {
308         if (!isEmpty() && !top && !end) {
309             return this.getTuple().getInt(i);
310         } else
311             throw new MedorException("No elements fetched in this tupleCollection");
312     }
313
314     /**
315      * Returns the value of the designated column in the current row of this Tuple
316      * as a float of java programming language
317      * @param i the first column is 1, the second is 2, ...
318      * @return a float value
319      * @throws MedorException if data source access error or invalid cursor
320      */

321     public synchronized float getFloat(int i) throws MedorException {
322         if (!isEmpty() && !top && !end) {
323             return this.getTuple().getFloat(i);
324         } else
325             throw new MedorException("No elements fetched in this tupleCollection");
326     }
327
328     /**
329      * Returns the value of the designated column in the current row of this Tuple
330      * as an int of java programming language
331      * @param i the first column is 1, the second is 2, ...
332      * @return an integer value
333      * @throws MedorException if data source access error or invalid cursor
334      */

335     public synchronized int getInt(int i) throws MedorException {
336         if (!isEmpty() && !top && !end) {
337             return this.getTuple().getInt(i);
338         } else
339             throw new MedorException("No elements fetched in this tupleCollection");
340     }
341
342     /**
343      * Returns the value of the designated column in the current row of this Tuple
344      * as a char of java programming language
345      * @param i the first column is 1, the second is 2, ...
346      * @return a character value
347      * @throws MedorException if data source access error or invalid cursor
348      */

349     public synchronized char getChar(int i) throws MedorException {
350         if (!isEmpty() && !top && !end) {
351             return this.getTuple().getChar(i);
352         } else
353             throw new MedorException("No elements fetched in this tupleCollection");
354     }
355
356     /**
357      * Returns the value of the designated column in the current row of this Tuple
358      * as a long of java programming language
359      * @param i the first column is 1, the second is 2, ...
360      * @return a long value
361      * @throws MedorException if data source access error or invalid cursor
362      */

363     public synchronized long getLong(int i) throws MedorException {
364         if (!isEmpty() && !top && !end) {
365             return this.getTuple().getLong(i);
366         } else
367             throw new MedorException("No elements fetched in this tupleCollection");
368     }
369
370     /**
371      * Returns the value of the designated column in the current row of this Tuple
372      * as a short of java programming language
373      * @param i the first column is 1, the second is 2, ...
374      * @return a short value
375      * @throws MedorException if data source access error or invalid cursor
376      */

377     public synchronized short getShort(int i) throws MedorException {
378         if (!isEmpty() && !top && !end) {
379             return this.getTuple().getShort(i);
380         } else
381             throw new MedorException("No elements fetched in this tupleCollection");
382     }
383
384     /**
385      * Returns the value of the designated column in the current row of this Tuple
386      * as a java.lang.String of java programming language
387      * @param i the first column is 1, the second is 2, ...
388      * @return a String value
389      * @throws MedorException if data source access error or invalid cursor
390      */

391     public synchronized String JavaDoc getString(int i) throws MedorException {
392         if (!isEmpty() && !top && !end) {
393             return this.getTuple().getString(i);
394         } else
395             throw new MedorException("No elements fetched in this tupleCollection");
396     }
397
398     public synchronized void setBoolean(boolean x, int i) throws MedorException {
399         //i represent the current row
400
if (!isEmpty() && !top && !end) {
401             Tuple t = this.getTuple();
402             MemoryTuple upletI = (MemoryTuple) t;
403             upletI.setBoolean(x, i);
404         } else
405             throw new MedorException("No elements fetched in this tupleCollection");
406     }
407
408
409     public synchronized void setByte(byte x, int i) throws MedorException {
410         if (!isEmpty() && !top && !end) {
411             Tuple t = this.getTuple();
412             MemoryTuple upletI = (MemoryTuple) t;
413             upletI.setByte(x, i);
414         } else
415             throw new MedorException("No elements fetched in this tupleCollection");
416     }
417
418     public synchronized void setDate(Date JavaDoc x, int i) throws MedorException {
419         if (!isEmpty() && !top && !end) {
420             Tuple t = this.getTuple();
421             MemoryTuple upletI = (MemoryTuple) t;
422             upletI.setDate(x, i);
423         } else
424             throw new MedorException("No elements fetched in this tupleCollection");
425     }
426
427     public synchronized void setDouble(double x, int i) throws MedorException {
428         if (!isEmpty() && !top && !end) {
429             Tuple t = this.getTuple();
430             MemoryTuple upletI = (MemoryTuple) t;
431             upletI.setDouble(x, i);
432             tuples.add(i, upletI);
433         } else
434             throw new MedorException("No elements fetched in this tupleCollection");
435     }
436
437     public synchronized void setFloat(float x, int i) throws MedorException {
438         if (!isEmpty() && !top && !end) {
439             Tuple t = this.getTuple();
440             MemoryTuple upletI = (MemoryTuple) t;
441             upletI.setFloat(x, i);
442         } else
443             throw new MedorException("No elements fetched in this tupleCollection");
444     }
445
446
447     public synchronized void setInt(int x, int i) throws MedorException {
448         if (!isEmpty() && !top && !end) {
449             Tuple t = this.getTuple();
450             MemoryTuple upletI = (MemoryTuple) t;
451             upletI.setInt(x, i);
452         } else
453             throw new MedorException("No elements fetched in this tupleCollection");
454     }
455
456     public synchronized void setShort(short x, int i) throws MedorException {
457         if (!isEmpty() && !top && !end) {
458             Tuple t = this.getTuple();
459             MemoryTuple upletI = (MemoryTuple) t;
460             upletI.setShort(x, i);
461         } else
462             throw new MedorException("No elements fetched in this tupleCollection");
463     }
464
465     public synchronized void setString(String JavaDoc x, int i) throws MedorException {
466         if (!isEmpty() && !top && !end) {
467             Tuple t = this.getTuple();
468             MemoryTuple upletI = (MemoryTuple) t;
469             upletI.setString(x, i);
470         } else
471             throw new MedorException("No elements fetched in this tupleCollection");
472     }
473
474     /**
475      * Some operations of researchs and iterations.
476      */

477
478     public synchronized boolean contains(Tuple T) {
479         return tuples.contains(T);
480     }
481
482     public synchronized void display() throws MedorException {
483         TupleCollectionView frame = new TupleCollectionView(this);
484         frame.pack();
485         frame.setVisible(true);
486     }
487
488     public synchronized Iterator JavaDoc iteratorOf(int i) throws MedorException {
489         Object JavaDoc o = getTuple().getObject(i);
490         if (o instanceof Collection) {
491             Collection c = (Collection) o;
492             return c.iterator();
493         } else {
494             throw new MedorException("the Attribute number " + i + " is not a collection Object");
495         }
496     }
497
498     public synchronized Iterator JavaDoc iteratorAll() throws MedorException {
499         return tuples.iterator();
500     }
501 }
Popular Tags