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