KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > persist > impl > WidenerInput


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: WidenerInput.java,v 1.3 2006/11/14 23:30:49 mark Exp $
7  */

8
9 package com.sleepycat.persist.impl;
10
11 import java.math.BigInteger JavaDoc;
12
13 /**
14  * Widens a value returned by another input when any readXxx method is called.
15  * Used to cause an Accessor to read a widened value.
16  *
17  * For non-key fields we support all Java primitive widening:
18  * - byte to short, int, long, float, double or BigInteger
19  * - short to int, long, float, double or BigInteger
20  * - char to int, long, float, double or BigInteger
21  * - int to long, float, double or BigInteger
22  * - long to float, double or BigInteger
23  * - float to double
24  *
25  * For non-key fields we also support:
26  * - Java reference widening
27  * - primitive to primitive wrapper
28  * - Java primitive widening to corresponding primitive wrappers
29  * - Java widening of primitive wrapper to primitive wrapper
30  *
31  * For secondary keys fields we ONLY support:
32  * - primitive to primitive wrapper
33  *
34  * But for primary keys and composite key fields we ONLY support:
35  * - primitive to primitive wrapper
36  * - primitive wrapper to primitive
37  * These conversions don't require any converter, since the stored format is
38  * not changed. A WidenerInput is not used for these changes.
39  *
40  * @author Mark Hayes
41  */

42 class WidenerInput extends AbstractInput {
43
44     private EntityInput input;
45     private int fromFormatId;
46     private int toFormatId;
47
48     /**
49      * Returns whether widening is supported by this class. If false is
50      * returned by this method, then widening is disallowed and a field
51      * converter or deleter is necessary.
52      */

53     static boolean isWideningSupported(Format fromFormat,
54                                        Format toFormat,
55                                        boolean isSecKeyField) {
56         int fromFormatId = fromFormat.getId();
57         int toFormatId = toFormat.getId();
58
59         switch (fromFormatId) {
60         case Format.ID_BOOL:
61             switch (toFormatId) {
62             case Format.ID_BOOL_W:
63                 return true;
64             default:
65                 return false;
66             }
67         case Format.ID_BYTE:
68             switch (toFormatId) {
69             case Format.ID_BYTE_W:
70                 return true;
71             case Format.ID_SHORT:
72             case Format.ID_SHORT_W:
73             case Format.ID_INT:
74             case Format.ID_INT_W:
75             case Format.ID_LONG:
76             case Format.ID_LONG_W:
77             case Format.ID_FLOAT:
78             case Format.ID_FLOAT_W:
79             case Format.ID_DOUBLE:
80             case Format.ID_DOUBLE_W:
81             case Format.ID_BIGINT:
82                 return !isSecKeyField;
83             default:
84                 return false;
85             }
86         case Format.ID_BYTE_W:
87             switch (toFormatId) {
88             case Format.ID_SHORT_W:
89             case Format.ID_INT_W:
90             case Format.ID_LONG_W:
91             case Format.ID_FLOAT_W:
92             case Format.ID_DOUBLE_W:
93             case Format.ID_BIGINT:
94                 return !isSecKeyField;
95             default:
96                 return false;
97             }
98         case Format.ID_SHORT:
99             switch (toFormatId) {
100             case Format.ID_SHORT_W:
101                 return true;
102             case Format.ID_INT:
103             case Format.ID_INT_W:
104             case Format.ID_LONG:
105             case Format.ID_LONG_W:
106             case Format.ID_FLOAT:
107             case Format.ID_FLOAT_W:
108             case Format.ID_DOUBLE:
109             case Format.ID_DOUBLE_W:
110             case Format.ID_BIGINT:
111                 return !isSecKeyField;
112             default:
113                 return false;
114             }
115         case Format.ID_SHORT_W:
116             switch (toFormatId) {
117             case Format.ID_INT_W:
118             case Format.ID_LONG_W:
119             case Format.ID_FLOAT_W:
120             case Format.ID_DOUBLE_W:
121             case Format.ID_BIGINT:
122                 return !isSecKeyField;
123             default:
124                 return false;
125             }
126         case Format.ID_INT:
127             switch (toFormatId) {
128             case Format.ID_INT_W:
129                 return true;
130             case Format.ID_LONG:
131             case Format.ID_LONG_W:
132             case Format.ID_FLOAT:
133             case Format.ID_FLOAT_W:
134             case Format.ID_DOUBLE:
135             case Format.ID_DOUBLE_W:
136             case Format.ID_BIGINT:
137                 return !isSecKeyField;
138             default:
139                 return false;
140             }
141         case Format.ID_INT_W:
142             switch (toFormatId) {
143             case Format.ID_LONG_W:
144             case Format.ID_FLOAT_W:
145             case Format.ID_DOUBLE_W:
146             case Format.ID_BIGINT:
147                 return !isSecKeyField;
148             default:
149                 return false;
150             }
151         case Format.ID_LONG:
152             switch (toFormatId) {
153             case Format.ID_LONG_W:
154                 return true;
155             case Format.ID_FLOAT:
156             case Format.ID_FLOAT_W:
157             case Format.ID_DOUBLE:
158             case Format.ID_DOUBLE_W:
159             case Format.ID_BIGINT:
160                 return !isSecKeyField;
161             default:
162                 return false;
163             }
164         case Format.ID_LONG_W:
165             switch (toFormatId) {
166             case Format.ID_FLOAT_W:
167             case Format.ID_DOUBLE_W:
168             case Format.ID_BIGINT:
169                 return !isSecKeyField;
170             default:
171                 return false;
172             }
173         case Format.ID_FLOAT:
174             switch (toFormatId) {
175             case Format.ID_FLOAT_W:
176                 return true;
177             case Format.ID_DOUBLE:
178             case Format.ID_DOUBLE_W:
179                 return !isSecKeyField;
180             default:
181                 return false;
182             }
183         case Format.ID_FLOAT_W:
184             switch (toFormatId) {
185             case Format.ID_DOUBLE_W:
186                 return !isSecKeyField;
187             default:
188                 return false;
189             }
190         case Format.ID_DOUBLE:
191             switch (toFormatId) {
192             case Format.ID_DOUBLE_W:
193                 return true;
194             default:
195                 return false;
196             }
197         case Format.ID_CHAR:
198             switch (toFormatId) {
199             case Format.ID_CHAR_W:
200                 return true;
201             case Format.ID_INT:
202             case Format.ID_INT_W:
203             case Format.ID_LONG:
204             case Format.ID_LONG_W:
205             case Format.ID_FLOAT:
206             case Format.ID_FLOAT_W:
207             case Format.ID_DOUBLE:
208             case Format.ID_DOUBLE_W:
209             case Format.ID_BIGINT:
210                 return !isSecKeyField;
211             default:
212                 return false;
213             }
214         case Format.ID_CHAR_W:
215             switch (toFormatId) {
216             case Format.ID_INT_W:
217             case Format.ID_LONG_W:
218             case Format.ID_FLOAT_W:
219             case Format.ID_DOUBLE_W:
220             case Format.ID_BIGINT:
221                 return !isSecKeyField;
222             default:
223                 return false;
224             }
225         default:
226             return false;
227         }
228     }
229
230     WidenerInput(EntityInput input, int fromFormatId, int toFormatId) {
231         super(input.getCatalog(), input.isRawAccess());
232         this.input = input;
233         this.fromFormatId = fromFormatId;
234         this.toFormatId = toFormatId;
235     }
236
237     public void registerPriKeyObject(Object JavaDoc o) {
238         input.registerPriKeyObject(o);
239     }
240
241     public int readArrayLength() {
242         throw new UnsupportedOperationException JavaDoc();
243     }
244
245     public int readEnumConstant(String JavaDoc[] names) {
246         throw new UnsupportedOperationException JavaDoc();
247     }
248
249     public void skipField(Format declaredFormat) {
250         throw new UnsupportedOperationException JavaDoc();
251     }
252
253     public String JavaDoc readString() {
254         throw new UnsupportedOperationException JavaDoc();
255     }
256
257     public Object JavaDoc readKeyObject(Format fromFormat) {
258         return readObject();
259     }
260
261     public Object JavaDoc readObject() {
262         switch (fromFormatId) {
263         case Format.ID_BOOL:
264             checkToFormat(Format.ID_BOOL_W);
265             return input.readBoolean();
266         case Format.ID_BYTE:
267             return byteToObject(input.readByte());
268         case Format.ID_BYTE_W:
269             Byte JavaDoc b = (Byte JavaDoc) input.readObject();
270             return (b != null) ? byteToObject(b) : null;
271         case Format.ID_SHORT:
272             return shortToObject(input.readShort());
273         case Format.ID_SHORT_W:
274             Short JavaDoc s = (Short JavaDoc) input.readObject();
275             return (s != null) ? shortToObject(s) : null;
276         case Format.ID_INT:
277             return intToObject(input.readInt());
278         case Format.ID_INT_W:
279             Integer JavaDoc i = (Integer JavaDoc) input.readObject();
280             return (i != null) ? intToObject(i) : null;
281         case Format.ID_LONG:
282             return longToObject(input.readLong());
283         case Format.ID_LONG_W:
284             Long JavaDoc l = (Long JavaDoc) input.readObject();
285             return (l != null) ? longToObject(l) : null;
286         case Format.ID_FLOAT:
287             return floatToObject(input.readSortedFloat());
288         case Format.ID_FLOAT_W:
289             Float JavaDoc f = (Float JavaDoc) input.readObject();
290             return (f != null) ? floatToObject(f) : null;
291         case Format.ID_DOUBLE:
292             checkToFormat(Format.ID_DOUBLE_W);
293             return input.readSortedDouble();
294         case Format.ID_CHAR:
295             return charToObject(input.readChar());
296         case Format.ID_CHAR_W:
297             Character JavaDoc c = (Character JavaDoc) input.readObject();
298             return (c != null) ? charToObject(c) : null;
299         default:
300             throw new IllegalStateException JavaDoc(String.valueOf(fromFormatId));
301         }
302     }
303
304     private Object JavaDoc byteToObject(byte v) {
305         switch (toFormatId) {
306         case Format.ID_BYTE:
307         case Format.ID_BYTE_W:
308             return Byte.valueOf(v);
309         case Format.ID_SHORT:
310         case Format.ID_SHORT_W:
311             return Short.valueOf(v);
312         case Format.ID_INT:
313         case Format.ID_INT_W:
314             return Integer.valueOf(v);
315         case Format.ID_LONG:
316         case Format.ID_LONG_W:
317             return Long.valueOf(v);
318         case Format.ID_FLOAT:
319         case Format.ID_FLOAT_W:
320             return Float.valueOf(v);
321         case Format.ID_DOUBLE:
322         case Format.ID_DOUBLE_W:
323             return Double.valueOf(v);
324         case Format.ID_BIGINT:
325             return BigInteger.valueOf(v);
326         default:
327             throw new IllegalStateException JavaDoc(String.valueOf(toFormatId));
328         }
329     }
330
331     private Object JavaDoc shortToObject(short v) {
332         switch (toFormatId) {
333         case Format.ID_SHORT:
334         case Format.ID_SHORT_W:
335             return Short.valueOf(v);
336         case Format.ID_INT:
337         case Format.ID_INT_W:
338             return Integer.valueOf(v);
339         case Format.ID_LONG:
340         case Format.ID_LONG_W:
341             return Long.valueOf(v);
342         case Format.ID_FLOAT:
343         case Format.ID_FLOAT_W:
344             return Float.valueOf(v);
345         case Format.ID_DOUBLE:
346         case Format.ID_DOUBLE_W:
347             return Double.valueOf(v);
348         case Format.ID_BIGINT:
349             return BigInteger.valueOf(v);
350         default:
351             throw new IllegalStateException JavaDoc(String.valueOf(toFormatId));
352         }
353     }
354
355     private Object JavaDoc intToObject(int v) {
356         switch (toFormatId) {
357         case Format.ID_INT:
358         case Format.ID_INT_W:
359             return Integer.valueOf(v);
360         case Format.ID_LONG:
361         case Format.ID_LONG_W:
362             return Long.valueOf(v);
363         case Format.ID_FLOAT:
364         case Format.ID_FLOAT_W:
365             return Float.valueOf(v);
366         case Format.ID_DOUBLE:
367         case Format.ID_DOUBLE_W:
368             return Double.valueOf(v);
369         case Format.ID_BIGINT:
370             return BigInteger.valueOf(v);
371         default:
372             throw new IllegalStateException JavaDoc(String.valueOf(toFormatId));
373         }
374     }
375
376     private Object JavaDoc longToObject(long v) {
377         switch (toFormatId) {
378         case Format.ID_LONG:
379         case Format.ID_LONG_W:
380             return Long.valueOf(v);
381         case Format.ID_FLOAT:
382         case Format.ID_FLOAT_W:
383             return Float.valueOf(v);
384         case Format.ID_DOUBLE:
385         case Format.ID_DOUBLE_W:
386             return Double.valueOf(v);
387         case Format.ID_BIGINT:
388             return BigInteger.valueOf(v);
389         default:
390             throw new IllegalStateException JavaDoc(String.valueOf(toFormatId));
391         }
392     }
393
394     private Object JavaDoc floatToObject(float v) {
395         switch (toFormatId) {
396         case Format.ID_FLOAT:
397         case Format.ID_FLOAT_W:
398             return Float.valueOf(v);
399         case Format.ID_DOUBLE:
400         case Format.ID_DOUBLE_W:
401             return Double.valueOf(v);
402         default:
403             throw new IllegalStateException JavaDoc(String.valueOf(toFormatId));
404         }
405     }
406
407     private Object JavaDoc charToObject(char v) {
408         switch (toFormatId) {
409         case Format.ID_CHAR:
410         case Format.ID_CHAR_W:
411             return Character.valueOf(v);
412         case Format.ID_INT:
413         case Format.ID_INT_W:
414             return Integer.valueOf(v);
415         case Format.ID_LONG:
416         case Format.ID_LONG_W:
417             return Long.valueOf(v);
418         case Format.ID_FLOAT:
419         case Format.ID_FLOAT_W:
420             return Float.valueOf(v);
421         case Format.ID_DOUBLE:
422         case Format.ID_DOUBLE_W:
423             return Double.valueOf(v);
424         case Format.ID_BIGINT:
425             return BigInteger.valueOf(v);
426         default:
427             throw new IllegalStateException JavaDoc(String.valueOf(toFormatId));
428         }
429     }
430
431     public char readChar() {
432         throw new IllegalStateException JavaDoc(String.valueOf(fromFormatId));
433     }
434
435     public boolean readBoolean() {
436         throw new IllegalStateException JavaDoc(String.valueOf(fromFormatId));
437     }
438
439     public byte readByte() {
440         throw new IllegalStateException JavaDoc(String.valueOf(fromFormatId));
441     }
442
443     public short readShort() {
444         checkToFormat(Format.ID_SHORT);
445         switch (fromFormatId) {
446         case Format.ID_BYTE:
447             return input.readByte();
448         default:
449             throw new IllegalStateException JavaDoc(String.valueOf(fromFormatId));
450         }
451     }
452
453     public int readInt() {
454         checkToFormat(Format.ID_INT);
455         switch (fromFormatId) {
456         case Format.ID_BYTE:
457             return input.readByte();
458         case Format.ID_SHORT:
459             return input.readShort();
460         case Format.ID_CHAR:
461             return input.readChar();
462         default:
463             throw new IllegalStateException JavaDoc(String.valueOf(fromFormatId));
464         }
465     }
466
467     public long readLong() {
468         checkToFormat(Format.ID_LONG);
469         switch (fromFormatId) {
470         case Format.ID_BYTE:
471             return input.readByte();
472         case Format.ID_SHORT:
473             return input.readShort();
474         case Format.ID_INT:
475             return input.readInt();
476         case Format.ID_CHAR:
477             return input.readChar();
478         default:
479             throw new IllegalStateException JavaDoc(String.valueOf(fromFormatId));
480         }
481     }
482
483     public float readSortedFloat() {
484         checkToFormat(Format.ID_FLOAT);
485         switch (fromFormatId) {
486         case Format.ID_BYTE:
487             return input.readByte();
488         case Format.ID_SHORT:
489             return input.readShort();
490         case Format.ID_INT:
491             return input.readInt();
492         case Format.ID_LONG:
493             return input.readLong();
494         case Format.ID_CHAR:
495             return input.readChar();
496         default:
497             throw new IllegalStateException JavaDoc(String.valueOf(fromFormatId));
498         }
499     }
500
501     public double readSortedDouble() {
502         checkToFormat(Format.ID_DOUBLE);
503         switch (fromFormatId) {
504         case Format.ID_BYTE:
505             return input.readByte();
506         case Format.ID_SHORT:
507             return input.readShort();
508         case Format.ID_INT:
509             return input.readInt();
510         case Format.ID_LONG:
511             return input.readLong();
512         case Format.ID_FLOAT:
513             return input.readSortedFloat();
514         case Format.ID_CHAR:
515             return input.readChar();
516         default:
517             throw new IllegalStateException JavaDoc(String.valueOf(fromFormatId));
518         }
519     }
520
521     public BigInteger JavaDoc readBigInteger() {
522         checkToFormat(Format.ID_BIGINT);
523         switch (fromFormatId) {
524         case Format.ID_BYTE:
525             return BigInteger.valueOf(input.readByte());
526         case Format.ID_SHORT:
527             return BigInteger.valueOf(input.readShort());
528         case Format.ID_INT:
529             return BigInteger.valueOf(input.readInt());
530         case Format.ID_LONG:
531             return BigInteger.valueOf(input.readLong());
532         case Format.ID_CHAR:
533             return BigInteger.valueOf(input.readChar());
534         default:
535             throw new IllegalStateException JavaDoc(String.valueOf(fromFormatId));
536         }
537     }
538     
539     private void checkToFormat(int id) {
540         if (toFormatId != id) {
541             throw new IllegalStateException JavaDoc(String.valueOf(toFormatId));
542         }
543     }
544 }
545
Popular Tags