File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\value\ValueJavaObject.java.html

1 /*
2 * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
3 * Version 1.0, and under the Eclipse Public License, Version 1.0
4 * (http://h2database.com/html/license.html).
5 * Initial Developer: H2 Group
6 */
7 package org.h2.value;
8
9 import java.sql.PreparedStatement;
10 import java.sql.SQLException;
11 import java.sql.Types;
12
13 import org.h2.constant.SysProperties;
14 import org.h2.util.Utils;
15
16 /**
17 * Implementation of the OBJECT data type.
18 */
19 public class ValueJavaObject extends ValueBytes {
20
21 private static final ValueJavaObject EMPTY = new ValueJavaObject(Utils.EMPTY_BYTES);
22
23 protected ValueJavaObject(byte[] v) {
24 super(v);
25 }
26
27 /**
28 * Get or create a java object value for the given byte array.
29 * Do not clone the data.
30 *
31 * @param javaObject the object
32 * @param b the byte array
33 * @return the value
34 */
35 public static ValueJavaObject getNoCopy(Object javaObject, byte[] b) {
36 if (b != null && b.length == 0) {
37 return EMPTY;
38 }
39 ValueJavaObject obj;
40 if (SysProperties.serializeJavaObject) {
41 if (b == null) {
42 b = Utils.serialize(javaObject);
43 }
44 obj = new ValueJavaObject(b);
45 } else {
46 obj = new NotSerialized(javaObject, b);
47 }
48 if (b == null || b.length > SysProperties.OBJECT_CACHE_MAX_PER_ELEMENT_SIZE) {
49 return obj;
50 }
51 return (ValueJavaObject) Value.cache(obj);
52 }
53
54 public int getType() {
55 return Value.JAVA_OBJECT;
56 }
57
58 public void set(PreparedStatement prep, int parameterIndex) throws SQLException {
59 Object obj = Utils.deserialize(getBytesNoCopy());
60 prep.setObject(parameterIndex, obj, Types.JAVA_OBJECT);
61 }
62
63 /**
64 * Value which serializes java object only for I/O operations.
65 * Used when property {@link SysProperties#serializeJavaObject} is disabled.
66 *
67 * @author Sergi Vladykin
68 */
69 private static class NotSerialized extends ValueJavaObject {
70
71 private Object javaObject;
72
73 private int displaySize = -1;
74
75 NotSerialized(Object javaObject, byte[] v) {
76 super(v);
77 this.javaObject = javaObject;
78 }
79
80 @Override
81 public void set(PreparedStatement prep, int parameterIndex) throws SQLException {
82 prep.setObject(parameterIndex, getObject(), Types.JAVA_OBJECT);
83 }
84
85 @Override
86 public byte[] getBytesNoCopy() {
87 if (value == null) {
88 value = Utils.serialize(javaObject);
89 }
90 return value;
91 }
92
93 @Override
94 protected int compareSecure(Value v, CompareMode mode) {
95 Object o1 = getObject();
96 Object o2 = v.getObject();
97
98 boolean o1Comparable = o1 instanceof Comparable;
99 boolean o2Comparable = o2 instanceof Comparable;
100
101 if (o1Comparable && o2Comparable &&
102 Utils.haveCommonComparableSuperclass(o1.getClass(), o2.getClass())) {
103 @SuppressWarnings("unchecked")
104 Comparable<Object> c1 = (Comparable<Object>) o1;
105 return c1.compareTo(o2);
106 }
107
108 // group by types
109 if (o1.getClass() != o2.getClass()) {
110 if (o1Comparable != o2Comparable) {
111 return o1Comparable ? -1 : 1;
112 }
113 return o1.getClass().getName().compareTo(o2.getClass().getName());
114 }
115
116 // compare hash codes
117 int h1 = hashCode();
118 int h2 = v.hashCode();
119
120 if (h1 == h2) {
121 if (o1.equals(o2)) {
122 return 0;
123 }
124
125 return Utils.compareNotNull(getBytesNoCopy(), v.getBytesNoCopy());
126 }
127
128 return h1 > h2 ? 1 : -1;
129 }
130
131 @Override
132 public String getString() {
133 String str = getObject().toString();
134 if (displaySize == -1) {
135 displaySize = str.length();
136 }
137 return str;
138 }
139
140 @Override
141 public long getPrecision() {
142 return 0;
143 }
144
145 @Override
146 public int hashCode() {
147 if (hash == 0) {
148 hash = getObject().hashCode();
149 }
150 return hash;
151 }
152
153 @Override
154 public Object getObject() {
155 if (javaObject == null) {
156 javaObject = Utils.deserialize(value);
157 }
158 return javaObject;
159 }
160
161 @Override
162 public int getDisplaySize() {
163 if (displaySize == -1) {
164 displaySize = getString().length();
165 }
166 return displaySize;
167 }
168
169 @Override
170 public int getMemory() {
171 if (value == null) {
172 return DataType.getDataType(getType()).memory;
173 }
174 int mem = super.getMemory();
175 if (javaObject != null) {
176 mem *= 2;
177 }
178 return mem;
179 }
180
181 @Override
182 public boolean equals(Object other) {
183 if (!(other instanceof NotSerialized)) {
184 return false;
185 }
186 return getObject().equals(((NotSerialized) other).getObject());
187 }
188
189 @Override
190 public Value convertPrecision(long precision, boolean force) {
191 return this;
192 }
193 }
194 }
195