Debian Codegraph @

lang/java/src/com/sleepycat/persist/impl/WidenerInput.java

raw
1 /*-
2 * See the file LICENSE for redistribution information.
3 *
4 * Copyright (c) 2002, 2013 Oracle and/or its affiliates. All rights reserved.
5 *
6 */
7
8 package com.sleepycat.persist.impl;
9
10 import java.math.BigDecimal;
11 import java.math.BigInteger;
12
13 import com.sleepycat.compat.DbCompat;
14
15 /**
16 * Widens a value returned by another input when any readXxx method is called.
17 * Used to cause an Accessor to read a widened value.
18 *
19 * For non-key fields we support all Java primitive widening:
20 * - byte to short, int, long, float, double, BigInteger and BigDecimal
21 * - short to int, long, float, double, BigInteger and BigDecimal
22 * - char to int, long, float, double, BigInteger and BigDecimal
23 * - int to long, float, double, BigInteger and BigDecimal
24 * - long to float, double, BigInteger and BigDecimal
25 * - float to double
26 *
27 * For non-key fields we also support:
28 * - Java reference widening
29 * - primitive to primitive wrapper
30 * - Java primitive widening to corresponding primitive wrappers
31 * - Java widening of primitive wrapper to primitive wrapper
32 *
33 * For secondary keys fields we ONLY support:
34 * - primitive to primitive wrapper
35 *
36 * But for primary keys and composite key fields we ONLY support:
37 * - primitive to primitive wrapper
38 * - primitive wrapper to primitive
39 * These conversions don't require any converter, since the stored format is
40 * not changed. A WidenerInput is not used for these changes.
41 *
42 * @author Mark Hayes
43 */
44 class WidenerInput extends AbstractInput {
45
46 private EntityInput input;
47 private int fromFormatId;
48 private int toFormatId;
49
50 /**
51 * Returns whether widening is supported by this class. If false is
52 * returned by this method, then widening is disallowed and a field
53 * converter or deleter is necessary.
54 */
55 static boolean isWideningSupported(Format fromFormat,
56 Format toFormat,
57 boolean isSecKeyField) {
58 int fromFormatId = fromFormat.getId();
59 int toFormatId = toFormat.getId();
60
61 switch (fromFormatId) {
62 case Format.ID_BOOL:
63 switch (toFormatId) {
64 case Format.ID_BOOL_W:
65 return true;
66 default:
67 return false;
68 }
69 case Format.ID_BYTE:
70 switch (toFormatId) {
71 case Format.ID_BYTE_W:
72 return true;
73 case Format.ID_SHORT:
74 case Format.ID_SHORT_W:
75 case Format.ID_INT:
76 case Format.ID_INT_W:
77 case Format.ID_LONG:
78 case Format.ID_LONG_W:
79 case Format.ID_FLOAT:
80 case Format.ID_FLOAT_W:
81 case Format.ID_DOUBLE:
82 case Format.ID_DOUBLE_W:
83 case Format.ID_BIGINT:
84 return !isSecKeyField;
85 default:
86 return false;
87 }
88 case Format.ID_BYTE_W:
89 switch (toFormatId) {
90 case Format.ID_SHORT_W:
91 case Format.ID_INT_W:
92 case Format.ID_LONG_W:
93 case Format.ID_FLOAT_W:
94 case Format.ID_DOUBLE_W:
95 case Format.ID_BIGINT:
96 return !isSecKeyField;
97 default:
98 return false;
99 }
100 case Format.ID_SHORT:
101 switch (toFormatId) {
102 case Format.ID_SHORT_W:
103 return true;
104 case Format.ID_INT:
105 case Format.ID_INT_W:
106 case Format.ID_LONG:
107 case Format.ID_LONG_W:
108 case Format.ID_FLOAT:
109 case Format.ID_FLOAT_W:
110 case Format.ID_DOUBLE:
111 case Format.ID_DOUBLE_W:
112 case Format.ID_BIGINT:
113 return !isSecKeyField;
114 default:
115 return false;
116 }
117 case Format.ID_SHORT_W:
118 switch (toFormatId) {
119 case Format.ID_INT_W:
120 case Format.ID_LONG_W:
121 case Format.ID_FLOAT_W:
122 case Format.ID_DOUBLE_W:
123 case Format.ID_BIGINT:
124 return !isSecKeyField;
125 default:
126 return false;
127 }
128 case Format.ID_INT:
129 switch (toFormatId) {
130 case Format.ID_INT_W:
131 return true;
132 case Format.ID_LONG:
133 case Format.ID_LONG_W:
134 case Format.ID_FLOAT:
135 case Format.ID_FLOAT_W:
136 case Format.ID_DOUBLE:
137 case Format.ID_DOUBLE_W:
138 case Format.ID_BIGINT:
139 return !isSecKeyField;
140 default:
141 return false;
142 }
143 case Format.ID_INT_W:
144 switch (toFormatId) {
145 case Format.ID_LONG_W:
146 case Format.ID_FLOAT_W:
147 case Format.ID_DOUBLE_W:
148 case Format.ID_BIGINT:
149 return !isSecKeyField;
150 default:
151 return false;
152 }
153 case Format.ID_LONG:
154 switch (toFormatId) {
155 case Format.ID_LONG_W:
156 return true;
157 case Format.ID_FLOAT:
158 case Format.ID_FLOAT_W:
159 case Format.ID_DOUBLE:
160 case Format.ID_DOUBLE_W:
161 case Format.ID_BIGINT:
162 return !isSecKeyField;
163 default:
164 return false;
165 }
166 case Format.ID_LONG_W:
167 switch (toFormatId) {
168 case Format.ID_FLOAT_W:
169 case Format.ID_DOUBLE_W:
170 case Format.ID_BIGINT:
171 return !isSecKeyField;
172 default:
173 return false;
174 }
175 case Format.ID_FLOAT:
176 switch (toFormatId) {
177 case Format.ID_FLOAT_W:
178 return true;
179 case Format.ID_DOUBLE:
180 case Format.ID_DOUBLE_W:
181 return !isSecKeyField;
182 default:
183 return false;
184 }
185 case Format.ID_FLOAT_W:
186 switch (toFormatId) {
187 case Format.ID_DOUBLE_W:
188 return !isSecKeyField;
189 default:
190 return false;
191 }
192 case Format.ID_DOUBLE:
193 switch (toFormatId) {
194 case Format.ID_DOUBLE_W:
195 return true;
196 default:
197 return false;
198 }
199 case Format.ID_CHAR:
200 switch (toFormatId) {
201 case Format.ID_CHAR_W:
202 return true;
203 case Format.ID_INT:
204 case Format.ID_INT_W:
205 case Format.ID_LONG:
206 case Format.ID_LONG_W:
207 case Format.ID_FLOAT:
208 case Format.ID_FLOAT_W:
209 case Format.ID_DOUBLE:
210 case Format.ID_DOUBLE_W:
211 case Format.ID_BIGINT:
212 return !isSecKeyField;
213 default:
214 return false;
215 }
216 case Format.ID_CHAR_W:
217 switch (toFormatId) {
218 case Format.ID_INT_W:
219 case Format.ID_LONG_W:
220 case Format.ID_FLOAT_W:
221 case Format.ID_DOUBLE_W:
222 case Format.ID_BIGINT:
223 return !isSecKeyField;
224 default:
225 return false;
226 }
227 case Format.ID_STRING:
228 switch (toFormatId) {
229 case Format.ID_OBJECT:
230 return !isSecKeyField;
231 default:
232 return false;
233 }
234 default:
235 return false;
236 }
237 }
238
239 WidenerInput(EntityInput input, int fromFormatId, int toFormatId) {
240 super(input.getCatalog(), input.isRawAccess());
241 this.input = input;
242 this.fromFormatId = fromFormatId;
243 this.toFormatId = toFormatId;
244 }
245
246 public void registerPriKeyObject(Object o) {
247 input.registerPriKeyObject(o);
248 }
249
250 public void registerPriStringKeyObject(Object o) {
251 input.registerPriStringKeyObject(o);
252 }
253
254 public int readArrayLength() {
255 throw DbCompat.unexpectedState();
256 }
257
258 public int readEnumConstant(String[] names) {
259 throw DbCompat.unexpectedState();
260 }
261
262 public void skipField(Format declaredFormat) {
263 throw DbCompat.unexpectedState();
264 }
265
266 public String readString() {
267 throw DbCompat.unexpectedState();
268 }
269
270 public Object readKeyObject(Format fromFormat)
271 throws RefreshException {
272
273 return readObject();
274 }
275
276 public Object readObject()
277 throws RefreshException {
278
279 switch (fromFormatId) {
280 case Format.ID_BOOL:
281 checkToFormat(Format.ID_BOOL_W);
282 return input.readBoolean();
283 case Format.ID_BYTE:
284 return byteToObject(input.readByte());
285 case Format.ID_BYTE_W:
286 Byte b = (Byte) input.readObject();
287 return (b != null) ? byteToObject(b) : null;
288 case Format.ID_SHORT:
289 return shortToObject(input.readShort());
290 case Format.ID_SHORT_W:
291 Short s = (Short) input.readObject();
292 return (s != null) ? shortToObject(s) : null;
293 case Format.ID_INT:
294 return intToObject(input.readInt());
295 case Format.ID_INT_W:
296 Integer i = (Integer) input.readObject();
297 return (i != null) ? intToObject(i) : null;
298 case Format.ID_LONG:
299 return longToObject(input.readLong());
300 case Format.ID_LONG_W:
301 Long l = (Long) input.readObject();
302 return (l != null) ? longToObject(l) : null;
303 case Format.ID_FLOAT:
304 return floatToObject(input.readSortedFloat());
305 case Format.ID_FLOAT_W:
306 Float f = (Float) input.readObject();
307 return (f != null) ? floatToObject(f) : null;
308 case Format.ID_DOUBLE:
309 checkToFormat(Format.ID_DOUBLE_W);
310 return input.readSortedDouble();
311 case Format.ID_CHAR:
312 return charToObject(input.readChar());
313 case Format.ID_CHAR_W:
314 Character c = (Character) input.readObject();
315 return (c != null) ? charToObject(c) : null;
316 case Format.ID_STRING:
317 checkToFormat(Format.ID_OBJECT);
318 return input.readStringObject();
319 default:
320 throw DbCompat.unexpectedState(String.valueOf(fromFormatId));
321 }
322 }
323
324 private Object byteToObject(byte v) {
325 switch (toFormatId) {
326 case Format.ID_BYTE:
327 case Format.ID_BYTE_W:
328 return Byte.valueOf(v);
329 case Format.ID_SHORT:
330 case Format.ID_SHORT_W:
331 return Short.valueOf(v);
332 case Format.ID_INT:
333 case Format.ID_INT_W:
334 return Integer.valueOf(v);
335 case Format.ID_LONG:
336 case Format.ID_LONG_W:
337 return Long.valueOf(v);
338 case Format.ID_FLOAT:
339 case Format.ID_FLOAT_W:
340 return Float.valueOf(v);
341 case Format.ID_DOUBLE:
342 case Format.ID_DOUBLE_W:
343 return Double.valueOf(v);
344 case Format.ID_BIGINT:
345 return BigInteger.valueOf(v);
346 default:
347 throw DbCompat.unexpectedState(String.valueOf(toFormatId));
348 }
349 }
350
351 private Object shortToObject(short v) {
352 switch (toFormatId) {
353 case Format.ID_SHORT:
354 case Format.ID_SHORT_W:
355 return Short.valueOf(v);
356 case Format.ID_INT:
357 case Format.ID_INT_W:
358 return Integer.valueOf(v);
359 case Format.ID_LONG:
360 case Format.ID_LONG_W:
361 return Long.valueOf(v);
362 case Format.ID_FLOAT:
363 case Format.ID_FLOAT_W:
364 return Float.valueOf(v);
365 case Format.ID_DOUBLE:
366 case Format.ID_DOUBLE_W:
367 return Double.valueOf(v);
368 case Format.ID_BIGINT:
369 return BigInteger.valueOf(v);
370 default:
371 throw DbCompat.unexpectedState(String.valueOf(toFormatId));
372 }
373 }
374
375 private Object intToObject(int v) {
376 switch (toFormatId) {
377 case Format.ID_INT:
378 case Format.ID_INT_W:
379 return Integer.valueOf(v);
380 case Format.ID_LONG:
381 case Format.ID_LONG_W:
382 return Long.valueOf(v);
383 case Format.ID_FLOAT:
384 case Format.ID_FLOAT_W:
385 return Float.valueOf(v);
386 case Format.ID_DOUBLE:
387 case Format.ID_DOUBLE_W:
388 return Double.valueOf(v);
389 case Format.ID_BIGINT:
390 return BigInteger.valueOf(v);
391 default:
392 throw DbCompat.unexpectedState(String.valueOf(toFormatId));
393 }
394 }
395
396 private Object longToObject(long v) {
397 switch (toFormatId) {
398 case Format.ID_LONG:
399 case Format.ID_LONG_W:
400 return Long.valueOf(v);
401 case Format.ID_FLOAT:
402 case Format.ID_FLOAT_W:
403 return Float.valueOf(v);
404 case Format.ID_DOUBLE:
405 case Format.ID_DOUBLE_W:
406 return Double.valueOf(v);
407 case Format.ID_BIGINT:
408 return BigInteger.valueOf(v);
409 default:
410 throw DbCompat.unexpectedState(String.valueOf(toFormatId));
411 }
412 }
413
414 private Object floatToObject(float v) {
415 switch (toFormatId) {
416 case Format.ID_FLOAT:
417 case Format.ID_FLOAT_W:
418 return Float.valueOf(v);
419 case Format.ID_DOUBLE:
420 case Format.ID_DOUBLE_W:
421 return Double.valueOf(v);
422 default:
423 throw DbCompat.unexpectedState(String.valueOf(toFormatId));
424 }
425 }
426
427 private Object charToObject(char v) {
428 switch (toFormatId) {
429 case Format.ID_CHAR:
430 case Format.ID_CHAR_W:
431 return Character.valueOf(v);
432 case Format.ID_INT:
433 case Format.ID_INT_W:
434 return Integer.valueOf(v);
435 case Format.ID_LONG:
436 case Format.ID_LONG_W:
437 return Long.valueOf(v);
438 case Format.ID_FLOAT:
439 case Format.ID_FLOAT_W:
440 return Float.valueOf(v);
441 case Format.ID_DOUBLE:
442 case Format.ID_DOUBLE_W:
443 return Double.valueOf(v);
444 case Format.ID_BIGINT:
445 return BigInteger.valueOf(v);
446 default:
447 throw DbCompat.unexpectedState(String.valueOf(toFormatId));
448 }
449 }
450
451 public char readChar() {
452 throw DbCompat.unexpectedState(String.valueOf(fromFormatId));
453 }
454
455 public boolean readBoolean() {
456 throw DbCompat.unexpectedState(String.valueOf(fromFormatId));
457 }
458
459 public byte readByte() {
460 throw DbCompat.unexpectedState(String.valueOf(fromFormatId));
461 }
462
463 public short readShort()
464 throws RefreshException {
465
466 checkToFormat(Format.ID_SHORT);
467 switch (fromFormatId) {
468 case Format.ID_BYTE:
469 return input.readByte();
470 default:
471 throw DbCompat.unexpectedState(String.valueOf(fromFormatId));
472 }
473 }
474
475 public int readInt()
476 throws RefreshException {
477
478 checkToFormat(Format.ID_INT);
479 switch (fromFormatId) {
480 case Format.ID_BYTE:
481 return input.readByte();
482 case Format.ID_SHORT:
483 return input.readShort();
484 case Format.ID_CHAR:
485 return input.readChar();
486 default:
487 throw DbCompat.unexpectedState(String.valueOf(fromFormatId));
488 }
489 }
490
491 public long readLong()
492 throws RefreshException {
493
494 checkToFormat(Format.ID_LONG);
495 switch (fromFormatId) {
496 case Format.ID_BYTE:
497 return input.readByte();
498 case Format.ID_SHORT:
499 return input.readShort();
500 case Format.ID_INT:
501 return input.readInt();
502 case Format.ID_CHAR:
503 return input.readChar();
504 default:
505 throw DbCompat.unexpectedState(String.valueOf(fromFormatId));
506 }
507 }
508
509 public float readSortedFloat()
510 throws RefreshException {
511
512 checkToFormat(Format.ID_FLOAT);
513 switch (fromFormatId) {
514 case Format.ID_BYTE:
515 return input.readByte();
516 case Format.ID_SHORT:
517 return input.readShort();
518 case Format.ID_INT:
519 return input.readInt();
520 case Format.ID_LONG:
521 return input.readLong();
522 case Format.ID_CHAR:
523 return input.readChar();
524 default:
525 throw DbCompat.unexpectedState(String.valueOf(fromFormatId));
526 }
527 }
528
529 public double readSortedDouble()
530 throws RefreshException {
531
532 checkToFormat(Format.ID_DOUBLE);
533 switch (fromFormatId) {
534 case Format.ID_BYTE:
535 return input.readByte();
536 case Format.ID_SHORT:
537 return input.readShort();
538 case Format.ID_INT:
539 return input.readInt();
540 case Format.ID_LONG:
541 return input.readLong();
542 case Format.ID_FLOAT:
543 return input.readSortedFloat();
544 case Format.ID_CHAR:
545 return input.readChar();
546 default:
547 throw DbCompat.unexpectedState(String.valueOf(fromFormatId));
548 }
549 }
550
551 public BigInteger readBigInteger()
552 throws RefreshException {
553
554 checkToFormat(Format.ID_BIGINT);
555 switch (fromFormatId) {
556 case Format.ID_BYTE:
557 return BigInteger.valueOf(input.readByte());
558 case Format.ID_SHORT:
559 return BigInteger.valueOf(input.readShort());
560 case Format.ID_INT:
561 return BigInteger.valueOf(input.readInt());
562 case Format.ID_LONG:
563 return BigInteger.valueOf(input.readLong());
564 case Format.ID_CHAR:
565 return BigInteger.valueOf(input.readChar());
566 default:
567 throw DbCompat.unexpectedState(String.valueOf(fromFormatId));
568 }
569 }
570
571 public BigDecimal readSortedBigDecimal()
572 throws RefreshException {
573 checkToFormat(Format.ID_BIGDEC);
574 switch (fromFormatId) {
575 case Format.ID_BYTE:
576 return BigDecimal.valueOf(input.readByte());
577 case Format.ID_SHORT:
578 return BigDecimal.valueOf(input.readShort());
579 case Format.ID_INT:
580 return BigDecimal.valueOf(input.readInt());
581 case Format.ID_LONG:
582 return BigDecimal.valueOf(input.readLong());
583 case Format.ID_CHAR:
584 return BigDecimal.valueOf(input.readChar());
585 default:
586 throw DbCompat.unexpectedState(String.valueOf(fromFormatId));
587 }
588 }
589
590 private void checkToFormat(int id) {
591 if (toFormatId != id) {
592 throw DbCompat.unexpectedState(String.valueOf(toFormatId));
593 }
594 }
595
596 public Object readStringObject(){
597 throw DbCompat.unexpectedState();
598 }
599 }