1 | /* |
2 | * Copyright 2004-2014 H2 Group. Multiple-Licensed under the MPL 2.0, |
3 | * and the EPL 1.0 (http://h2database.com/html/license.html). |
4 | * Initial Developer: H2 Group |
5 | */ |
6 | package org.h2.value; |
7 | |
8 | import java.lang.reflect.Array; |
9 | import java.sql.PreparedStatement; |
10 | import java.util.ArrayList; |
11 | import org.h2.engine.Constants; |
12 | import org.h2.util.MathUtils; |
13 | import org.h2.util.New; |
14 | import org.h2.util.StatementBuilder; |
15 | |
16 | /** |
17 | * Implementation of the ARRAY data type. |
18 | */ |
19 | public class ValueArray extends Value { |
20 | |
21 | private final Class<?> componentType; |
22 | private final Value[] values; |
23 | private int hash; |
24 | |
25 | private ValueArray(Class<?> componentType, Value[] list) { |
26 | this.componentType = componentType; |
27 | this.values = list; |
28 | } |
29 | |
30 | private ValueArray(Value[] list) { |
31 | this(Object.class, list); |
32 | } |
33 | |
34 | /** |
35 | * Get or create a array value for the given value array. |
36 | * Do not clone the data. |
37 | * |
38 | * @param list the value array |
39 | * @return the value |
40 | */ |
41 | public static ValueArray get(Value[] list) { |
42 | return new ValueArray(list); |
43 | } |
44 | |
45 | /** |
46 | * Get or create a array value for the given value array. |
47 | * Do not clone the data. |
48 | * |
49 | * @param componentType the array class (null for Object[]) |
50 | * @param list the value array |
51 | * @return the value |
52 | */ |
53 | public static ValueArray get(Class<?> componentType, Value[] list) { |
54 | return new ValueArray(componentType, list); |
55 | } |
56 | |
57 | @Override |
58 | public int hashCode() { |
59 | if (hash != 0) { |
60 | return hash; |
61 | } |
62 | int h = 1; |
63 | for (Value v : values) { |
64 | h = h * 31 + v.hashCode(); |
65 | } |
66 | hash = h; |
67 | return h; |
68 | } |
69 | |
70 | public Value[] getList() { |
71 | return values; |
72 | } |
73 | |
74 | @Override |
75 | public int getType() { |
76 | return Value.ARRAY; |
77 | } |
78 | |
79 | public Class<?> getComponentType() { |
80 | return componentType; |
81 | } |
82 | |
83 | @Override |
84 | public long getPrecision() { |
85 | long p = 0; |
86 | for (Value v : values) { |
87 | p += v.getPrecision(); |
88 | } |
89 | return p; |
90 | } |
91 | |
92 | @Override |
93 | public String getString() { |
94 | StatementBuilder buff = new StatementBuilder("("); |
95 | for (Value v : values) { |
96 | buff.appendExceptFirst(", "); |
97 | buff.append(v.getString()); |
98 | } |
99 | return buff.append(')').toString(); |
100 | } |
101 | |
102 | @Override |
103 | protected int compareSecure(Value o, CompareMode mode) { |
104 | ValueArray v = (ValueArray) o; |
105 | if (values == v.values) { |
106 | return 0; |
107 | } |
108 | int l = values.length; |
109 | int ol = v.values.length; |
110 | int len = Math.min(l, ol); |
111 | for (int i = 0; i < len; i++) { |
112 | Value v1 = values[i]; |
113 | Value v2 = v.values[i]; |
114 | int comp = v1.compareTo(v2, mode); |
115 | if (comp != 0) { |
116 | return comp; |
117 | } |
118 | } |
119 | return l > ol ? 1 : l == ol ? 0 : -1; |
120 | } |
121 | |
122 | @Override |
123 | public Object getObject() { |
124 | int len = values.length; |
125 | Object[] list = (Object[]) Array.newInstance(componentType, len); |
126 | for (int i = 0; i < len; i++) { |
127 | list[i] = values[i].getObject(); |
128 | } |
129 | return list; |
130 | } |
131 | |
132 | @Override |
133 | public void set(PreparedStatement prep, int parameterIndex) { |
134 | throw throwUnsupportedExceptionForType("PreparedStatement.set"); |
135 | } |
136 | |
137 | @Override |
138 | public String getSQL() { |
139 | StatementBuilder buff = new StatementBuilder("("); |
140 | for (Value v : values) { |
141 | buff.appendExceptFirst(", "); |
142 | buff.append(v.getSQL()); |
143 | } |
144 | if (values.length == 1) { |
145 | buff.append(','); |
146 | } |
147 | return buff.append(')').toString(); |
148 | } |
149 | |
150 | @Override |
151 | public String getTraceSQL() { |
152 | StatementBuilder buff = new StatementBuilder("("); |
153 | for (Value v : values) { |
154 | buff.appendExceptFirst(", "); |
155 | buff.append(v == null ? "null" : v.getTraceSQL()); |
156 | } |
157 | return buff.append(')').toString(); |
158 | } |
159 | |
160 | @Override |
161 | public int getDisplaySize() { |
162 | long size = 0; |
163 | for (Value v : values) { |
164 | size += v.getDisplaySize(); |
165 | } |
166 | return MathUtils.convertLongToInt(size); |
167 | } |
168 | |
169 | @Override |
170 | public boolean equals(Object other) { |
171 | if (!(other instanceof ValueArray)) { |
172 | return false; |
173 | } |
174 | ValueArray v = (ValueArray) other; |
175 | if (values == v.values) { |
176 | return true; |
177 | } |
178 | int len = values.length; |
179 | if (len != v.values.length) { |
180 | return false; |
181 | } |
182 | for (int i = 0; i < len; i++) { |
183 | if (!values[i].equals(v.values[i])) { |
184 | return false; |
185 | } |
186 | } |
187 | return true; |
188 | } |
189 | |
190 | @Override |
191 | public int getMemory() { |
192 | int memory = 32; |
193 | for (Value v : values) { |
194 | memory += v.getMemory() + Constants.MEMORY_POINTER; |
195 | } |
196 | return memory; |
197 | } |
198 | |
199 | @Override |
200 | public Value convertPrecision(long precision, boolean force) { |
201 | if (!force) { |
202 | return this; |
203 | } |
204 | ArrayList<Value> list = New.arrayList(); |
205 | for (Value v : values) { |
206 | v = v.convertPrecision(precision, true); |
207 | // empty byte arrays or strings have precision 0 |
208 | // they count as precision 1 here |
209 | precision -= Math.max(1, v.getPrecision()); |
210 | if (precision < 0) { |
211 | break; |
212 | } |
213 | list.add(v); |
214 | } |
215 | Value[] array = new Value[list.size()]; |
216 | list.toArray(array); |
217 | return get(array); |
218 | } |
219 | |
220 | } |