01: /*
02: * Copyright (c) 1998 - 2005 Versant Corporation
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * Versant Corporation - initial API and implementation
10: */
11: package com.versant.core.jdbc.sql.conv;
12:
13: import com.versant.core.jdbc.JdbcConverter;
14:
15: /**
16: * This converter converts double[] to and from SQL. It converts the double[]
17: * to and from a double[] and delegates to a nested converter.
18: * TODO This could be done much faster with java.nio buffers.
19: * @keep-all
20: */
21: public class DoubleArrayConverter extends TypeAsBytesConverterBase {
22:
23: public static class Factory extends
24: TypeAsBytesConverterBase.Factory {
25:
26: protected JdbcConverter createConverter(JdbcConverter nested) {
27: return new DoubleArrayConverter(nested);
28: }
29:
30: }
31:
32: public DoubleArrayConverter(JdbcConverter nested) {
33: super (nested);
34: }
35:
36: /**
37: * Convert a byte[] into an instance of our value class.
38: */
39: protected Object fromByteArray(byte[] buf) {
40: int n = buf.length / 8;
41: double[] a = new double[n];
42: int i = 0, j = 0;
43: for (; i < n;) {
44: a[i++] = Double
45: .longBitsToDouble(((long) (buf[j++] & 0xFF) << 56)
46: + ((long) (buf[j++] & 0xFF) << 48)
47: + ((long) (buf[j++] & 0xFF) << 40)
48: + ((long) (buf[j++] & 0xFF) << 32)
49: + ((long) (buf[j++] & 0xFF) << 24)
50: + ((buf[j++] & 0xFF) << 16)
51: + ((buf[j++] & 0xFF) << 8)
52: + (buf[j++] & 0xFF));
53: }
54: return a;
55: }
56:
57: /**
58: * Convert an instance of our value class into a byte[].
59: */
60: protected byte[] toByteArray(Object value) {
61: if (value == null)
62: return null;
63: double[] a = (double[]) value;
64: int n = a.length;
65: byte[] buf = new byte[n * 8];
66: int i = 0, j = 0;
67: for (; i < n;) {
68:
69: long x = Double.doubleToRawLongBits(a[i++]);
70:
71: buf[j++] = (byte) ((x >>> 56) & 0xFF);
72: buf[j++] = (byte) ((x >>> 48) & 0xFF);
73: buf[j++] = (byte) ((x >>> 40) & 0xFF);
74: buf[j++] = (byte) ((x >>> 32) & 0xFF);
75: buf[j++] = (byte) ((x >>> 24) & 0xFF);
76: buf[j++] = (byte) ((x >>> 16) & 0xFF);
77: buf[j++] = (byte) ((x >>> 8) & 0xFF);
78: buf[j++] = (byte) (x & 0xFF);
79: }
80: return buf;
81: }
82:
83: /**
84: * Get the type of our expected value objects (e.g. java.util.Locale
85: * for a converter for Locale's).
86: */
87: public Class getValueType() {
88: return double[].class;
89: }
90:
91: }
|