KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.objectweb.jorm.type.api.PType;
27 import org.objectweb.medor.api.MedorException;
28 import org.objectweb.medor.clone.lib.BasicCloneable;
29 import org.objectweb.medor.expression.api.TypingException;
30 import org.objectweb.medor.expression.api.VariableOperand;
31 import org.objectweb.medor.expression.api.Operand;
32 import org.objectweb.medor.expression.lib.BasicVariableOperand;
33 import org.objectweb.medor.tuple.api.Tuple;
34 import org.objectweb.medor.tuple.api.TupleCollection;
35 import org.objectweb.medor.type.lib.QTypeTuple;
36
37 import java.util.Date JavaDoc;
38 import java.util.Map JavaDoc;
39 import java.math.BigDecimal JavaDoc;
40 import java.math.BigInteger JavaDoc;
41
42
43 /**
44  * This class represent a tuple interface in a memory. Java array is used to stores fienlds Values.
45  * Rather than getXXX() methods, setXXX() allow updating the content of the Tuple.
46  */

47 public class MemoryTuple extends BasicCloneable implements Tuple {
48
49     private VariableOperand[] uplet;
50
51     public MemoryTuple() {
52     }
53
54     public MemoryTuple(PType types[]) {
55         uplet = new BasicVariableOperand[uplet.length];
56         for (int cpt = 0; cpt < types.length; cpt++) {
57             uplet[cpt] = new BasicVariableOperand(types[cpt]);
58         }
59     }
60
61     public MemoryTuple(VariableOperand fields[]) {
62         uplet = fields;
63     }
64
65     public MemoryTuple(Operand fields[]) throws TypingException {
66         PType type;
67         uplet = new BasicVariableOperand[fields.length];
68         for (int i = 0; i < uplet.length; i++) {
69             type = fields[i].getType();
70             uplet[i] = new BasicVariableOperand(type);
71             switch (type.getTypeCode()) {
72             case QTypeTuple.TYPECODE_INT:
73                 uplet[i].setValue(fields[i].getInt());
74                 break;
75             case QTypeTuple.TYPECODE_SHORT:
76                 uplet[i].setValue(fields[i].getShort());
77                 break;
78             case QTypeTuple.TYPECODE_BYTE:
79                 uplet[i].setValue(fields[i].getByte());
80                 break;
81             case QTypeTuple.TYPECODE_LONG:
82                 uplet[i].setValue(fields[i].getLong());
83                 break;
84             case QTypeTuple.TYPECODE_DOUBLE:
85                 uplet[i].setValue(fields[i].getDouble());
86                 break;
87             case QTypeTuple.TYPECODE_BOOLEAN:
88                 uplet[i].setValue(fields[i].getBoolean());
89                 break;
90             case QTypeTuple.TYPECODE_FLOAT:
91                 uplet[i].setValue(fields[i].getFloat());
92                 break;
93             case QTypeTuple.TYPECODE_STRING:
94                 uplet[i].setValue(fields[i].getString());
95                 break;
96             case QTypeTuple.TYPECODE_CHAR:
97                 uplet[i].setValue(fields[i].getChar());
98                 break;
99             case QTypeTuple.TYPECODE_DATE:
100                 uplet[i].setValue(fields[i].getDate());
101                 break;
102             case QTypeTuple.TYPECODE_OBJBOOLEAN:
103             case QTypeTuple.TYPECODE_OBJBYTE:
104             case QTypeTuple.TYPECODE_OBJCHAR:
105             case QTypeTuple.TYPECODE_OBJDOUBLE:
106             case QTypeTuple.TYPECODE_OBJFLOAT:
107             case QTypeTuple.TYPECODE_OBJINT:
108             case QTypeTuple.TYPECODE_OBJLONG:
109             case QTypeTuple.TYPECODE_OBJSHORT:
110                 uplet[i].setValue(fields[i].getObject());
111                 break;
112                 // TO DO: Add TupleCollection type and Tuple Types
113
}
114         }
115     }
116
117     public Object JavaDoc clone(Object JavaDoc clone,
118                         Map JavaDoc obj2clone) throws CloneNotSupportedException JavaDoc {
119         clone = super.clone(clone, obj2clone);
120         if (uplet != null) {
121             MemoryTuple mt = (MemoryTuple) clone;
122             mt.uplet = new VariableOperand[uplet.length];
123             for(int i=0; i<uplet.length; i++) {
124                 mt.uplet[i] = (VariableOperand) getClone(uplet[i], obj2clone);
125             }
126         }
127         return clone;
128     }
129
130     public boolean isDefined(int i) {
131         return uplet[i - 1].isDefined();
132     }
133
134     public int getSize() {
135         return uplet.length;
136     }
137
138     public Operand[] toOperandArray() {
139         return uplet;
140     }
141
142     public int getInt(int i) throws MedorException {
143         if ((i <= 0) || (i > uplet.length)) {
144             throw new MedorException("Invalid Field index: " + i);
145         } else {
146             try {
147                 return uplet[i - 1].getInt();
148             } catch (TypingException e) {
149                 throw new MedorException(e);
150             }
151         }
152     }
153
154     public void setInt(int x, int i) throws MedorException {
155         if ((i <= 0) || (i > uplet.length)) {
156             throw new MedorException("Invalid Field index: " + i);
157         } else {
158             try {
159                 uplet[i - 1].setValue(x);
160             } catch (TypingException e) {
161                 throw new MedorException(e);
162             }
163         }
164     }
165
166     public byte getByte(int i) throws MedorException {
167         if ((i <= 0) || (i > uplet.length)) {
168             throw new MedorException("Invalid Field index: " + i);
169         } else {
170             try {
171                 return uplet[i - 1].getByte();
172             } catch (TypingException e) {
173                 throw new MedorException(e);
174             }
175         }
176     }
177
178     public byte[] getByteArray(int i) throws MedorException {
179         if ((i <= 0) || (i > uplet.length)) {
180             throw new MedorException("Invalid Field index: " + i);
181         } else {
182             try {
183                 return uplet[i - 1].getByteArray();
184             } catch (TypingException e) {
185                 throw new MedorException(e);
186             }
187         }
188     }
189
190     public void setByte(byte x, int i) throws MedorException {
191         if ((i <= 0) || (i > uplet.length)) {
192             throw new MedorException("Invalid Field index: " + i);
193         } else
194             try {
195                 uplet[i - 1].setValue(x);
196             } catch (TypingException e) {
197                 throw new MedorException(e);
198             }
199     }
200
201     public double getDouble(int i) throws MedorException {
202         if ((i <= 0) || (i > uplet.length)) {
203             throw new MedorException("Invalid Field index: " + i);
204         } else {
205             try {
206                 return uplet[i - 1].getDouble();
207             } catch (TypingException e) {
208                 throw new MedorException(e);
209             }
210         }
211     }
212
213     public void setBigDecimal(BigDecimal JavaDoc x, int i) throws MedorException {
214         if ((i <= 0) || (i > uplet.length)) {
215             throw new MedorException("Invalid Field index: " + i);
216         } else
217             try {
218                 uplet[i - 1].setValue(x);
219             } catch (TypingException e) {
220                 throw new MedorException(e);
221             }
222     }
223
224     public BigDecimal JavaDoc getBigDecimal(int i) throws MedorException {
225         if ((i <= 0) || (i > uplet.length)) {
226             throw new MedorException("Invalid Field index: " + i);
227         } else {
228             try {
229                 return uplet[i - 1].getBigDecimal();
230             } catch (TypingException e) {
231                 throw new MedorException(e);
232             }
233         }
234     }
235
236     public void setBigInteger(BigInteger JavaDoc x, int i) throws MedorException {
237         if ((i <= 0) || (i > uplet.length)) {
238             throw new MedorException("Invalid Field index: " + i);
239         } else
240             try {
241                 uplet[i - 1].setValue(x);
242             } catch (TypingException e) {
243                 throw new MedorException(e);
244             }
245     }
246
247     public BigInteger JavaDoc getBigInteger(int i) throws MedorException {
248         if ((i <= 0) || (i > uplet.length)) {
249             throw new MedorException("Invalid Field index: " + i);
250         } else {
251             try {
252                 return uplet[i - 1].getBigInteger();
253             } catch (TypingException e) {
254                 throw new MedorException(e);
255             }
256         }
257     }
258
259     public void setDouble(double x, int i) throws MedorException {
260         if ((i <= 0) || (i > uplet.length)) {
261             throw new MedorException("Invalid Field index: " + i);
262         } else
263             try {
264                 uplet[i - 1].setValue(x);
265             } catch (TypingException e) {
266                 throw new MedorException(e);
267             }
268     }
269
270     public float getFloat(int i) throws MedorException {
271         if ((i <= 0) || (i > uplet.length)) {
272             throw new MedorException("Invalid Field index: " + i);
273         } else {
274             try {
275                 return uplet[i - 1].getFloat();
276             } catch (TypingException e) {
277                 throw new MedorException(e);
278             }
279         }
280     }
281
282     public void setFloat(float x, int i) throws MedorException {
283         if ((i <= 0) || (i > uplet.length)) {
284             throw new MedorException("Invalid Field index: " + i);
285         } else
286             try {
287                 uplet[i - 1].setValue(x);
288             } catch (TypingException e) {
289                 throw new MedorException(e);
290             }
291     }
292
293
294     public short getShort(int i) throws MedorException {
295         if ((i <= 0) || (i > uplet.length)) {
296             throw new MedorException("Invalid Field index: " + i);
297         } else
298             try {
299                 return uplet[i - 1].getShort();
300             } catch (TypingException e) {
301                 throw new MedorException(e);
302             }
303     }
304
305     public void setShort(short x, int i) throws MedorException {
306         if ((i <= 0) || (i > uplet.length)) {
307             throw new MedorException("Invalid Field index: " + i);
308         } else
309             try {
310                 uplet[i - 1].setValue(x);
311             } catch (TypingException e) {
312                 throw new MedorException(e);
313             }
314     }
315
316     public String JavaDoc getString(int i) throws MedorException {
317         if ((i <= 0) || (i > uplet.length)) {
318             throw new MedorException("Invalid Field index: " + i);
319         } else
320             try {
321                 return uplet[i - 1].getString();
322             } catch (TypingException e) {
323                 throw new MedorException(e);
324             }
325     }
326
327     public void setString(String JavaDoc x, int i) throws MedorException {
328         if ((i <= 0) || (i > uplet.length)) {
329             throw new MedorException("Invalid Field index: " + i);
330         } else
331             try {
332                 uplet[i - 1].setValue(x);
333             } catch (TypingException e) {
334                 throw new MedorException(e);
335             }
336     }
337
338     public long getLong(int i) throws MedorException {
339         if ((i <= 0) || (i > uplet.length)) {
340             throw new MedorException("Invalid Field index: " + i);
341         } else
342             try {
343                 return uplet[i - 1].getLong();
344             } catch (TypingException e) {
345                 throw new MedorException(e);
346             }
347     }
348
349     public void setLong(long x, int i) throws MedorException {
350         if ((i <= 0) || (i > uplet.length)) {
351             throw new MedorException("Invalid Field index: " + i);
352         } else
353             try {
354                 uplet[i - 1].setValue(x);
355             } catch (TypingException e) {
356                 throw new MedorException(e);
357             }
358     }
359
360     public Object JavaDoc getObject(int i) throws MedorException {
361         Object JavaDoc result = null;
362         if ((i <= 0) || (i > uplet.length)) {
363             throw new MedorException("Invalid Field index: " + i);
364         } else {
365             try {
366                 switch (uplet[i - 1].getType().getTypeCode()) {
367                 case QTypeTuple.TYPECODE_INT:
368                     result = new Integer JavaDoc(uplet[i - 1].getInt());
369                     break;
370                 case QTypeTuple.TYPECODE_SHORT:
371                     result = new Short JavaDoc(uplet[i - 1].getShort());
372                     break;
373                 case QTypeTuple.TYPECODE_BYTE:
374                     result = new Byte JavaDoc(uplet[i - 1].getByte());
375                     break;
376                 case QTypeTuple.TYPECODE_LONG:
377                     result = new Long JavaDoc(uplet[i - 1].getLong());
378                     break;
379                 case QTypeTuple.TYPECODE_DOUBLE:
380                     result = new Double JavaDoc(uplet[i - 1].getDouble());
381                     break;
382                 case QTypeTuple.TYPECODE_BOOLEAN:
383                     result = new Boolean JavaDoc(uplet[i - 1].getBoolean());
384                     break;
385                 case QTypeTuple.TYPECODE_FLOAT:
386                     result = new Float JavaDoc(uplet[i - 1].getFloat());
387                     break;
388                 case QTypeTuple.TYPECODE_STRING:
389                     String JavaDoc s = uplet[i - 1].getString();
390                     result = (s == null ? null : new String JavaDoc(s));
391                     break;
392                 case QTypeTuple.TYPECODE_CHAR:
393                     result = new Character JavaDoc(uplet[i - 1].getChar());
394                     break;
395                 case QTypeTuple.TYPECODE_DATE:
396                     result = uplet[i - 1].getDate();
397                     break;
398
399                 default :
400                     if (uplet[i - 1].getType().getJormName().equalsIgnoreCase("collection")) {
401                         result = uplet[i - 1].getObject();
402                         break;
403                     } else
404                         result = uplet[i - 1].getObject();
405                 }
406             } catch (TypingException e) {
407                 throw new MedorException(e);
408             }
409         }
410         return result;
411     }
412
413     public boolean getBoolean(int i) throws MedorException {
414         if ((i <= 0) || (i > uplet.length)) {
415             throw new MedorException("Invalid Field index: " + i);
416         } else
417             try {
418                 return uplet[i - 1].getBoolean();
419             } catch (TypingException e) {
420                 throw new MedorException(e);
421             }
422     }
423
424     public void setBoolean(boolean x, int i) throws MedorException {
425         if ((i <= 0) || (i > uplet.length)) {
426             throw new MedorException("Invalid Field index: " + i);
427         } else
428             try {
429                 uplet[i - 1].setValue(x);
430             } catch (TypingException e) {
431                 throw new MedorException(e);
432             }
433     }
434
435     public char getChar(int i) throws MedorException {
436         if ((i <= 0) || (i > uplet.length)) {
437             throw new MedorException("Invalid Field index: " + i);
438         } else
439             try {
440                 return uplet[i - 1].getChar();
441             } catch (TypingException e) {
442                 throw new MedorException(e);
443             }
444     }
445
446     public char[] getCharArray(int i) throws MedorException {
447         if ((i <= 0) || (i > uplet.length)) {
448             throw new MedorException("Invalid Field index: " + i);
449         } else
450             try {
451                 return uplet[i - 1].getCharArray();
452             } catch (TypingException e) {
453                 throw new MedorException(e);
454             }
455     }
456
457     public void setChar(char x, int i) throws MedorException {
458         if ((i <= 0) || (i > uplet.length)) {
459             throw new MedorException("Invalid Field index: " + i);
460         } else
461             try {
462                 uplet[i - 1].setValue(x);
463             } catch (TypingException e) {
464                 throw new MedorException(e);
465             }
466     }
467
468     public Date JavaDoc getDate(int i) throws MedorException {
469         if ((i <= 0) || (i > uplet.length)) {
470             throw new MedorException("Invalid Field index: " + i);
471         } else
472             try {
473                 return uplet[i - 1].getDate();
474             } catch (TypingException e) {
475                 throw new MedorException(e);
476             }
477     }
478
479     public void setDate(Date JavaDoc x, int i) throws MedorException {
480         if ((i <= 0) || (i > uplet.length)) {
481             throw new MedorException("Invalid Field index: " + i);
482         } else
483             try {
484                 uplet[i - 1].setValue(x);
485             } catch (TypingException e) {
486                 throw new MedorException(e);
487             }
488     }
489
490     public TupleCollection getTupleCollection(int i) throws MedorException {
491         if ((i <= 0) || (i > uplet.length)) {
492             throw new MedorException("Invalid Field index: " + i);
493         } else
494             return (TupleCollection) uplet[i - 1].getObject();
495     }
496
497     public Operand getLikeOperand(int i) throws MedorException {
498         if ((i <= 0) || (i > uplet.length)) {
499             throw new MedorException("Invalid Field index: " + i);
500         } else
501             return uplet[i - 1];
502     }
503
504 }
505
Popular Tags