| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.value; |
| 7 |
|
| 8 |
import org.h2.engine.CastDataProvider; |
| 9 |
import org.h2.engine.Constants; |
| 10 |
import org.h2.message.DbException; |
| 11 |
|
| 12 |
/** |
| 13 |
* Implementation of the ARRAY data type. |
| 14 |
*/ |
| 15 |
public final class ValueArray extends ValueCollectionBase { |
| 16 |
|
| 17 |
/** |
| 18 |
* Empty array. |
| 19 |
*/ |
| 20 |
public static final ValueArray EMPTY = get(TypeInfo.TYPE_NULL, Value.EMPTY_VALUES, null); |
| 21 |
|
| 22 |
private TypeInfo type; |
| 23 |
|
| 24 |
private final TypeInfo componentType; |
| 25 |
|
| 26 |
private ValueArray(TypeInfo componentType, Value[] list, CastDataProvider provider) { |
| 27 |
super(list); |
| 28 |
int length = list.length; |
| 29 |
if (length > Constants.MAX_ARRAY_CARDINALITY) { |
| 30 |
String typeName = getTypeName(getValueType()); |
| 31 |
throw DbException.getValueTooLongException(typeName, typeName, length); |
| 32 |
} |
| 33 |
for (int i = 0; i < length; i++) { |
| 34 |
list[i] = list[i].castTo(componentType, provider); |
| 35 |
} |
| 36 |
this.componentType = componentType; |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Get or create a array value for the given value array. |
| 41 |
* Do not clone the data. |
| 42 |
* |
| 43 |
* @param list the value array |
| 44 |
* @param provider the cast information provider |
| 45 |
* @return the value |
| 46 |
*/ |
| 47 |
public static ValueArray get(Value[] list, CastDataProvider provider) { |
| 48 |
return new ValueArray(TypeInfo.getHigherType(list), list, provider); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Get or create a array value for the given value array. |
| 53 |
* Do not clone the data. |
| 54 |
* |
| 55 |
* @param componentType the type of elements, or {@code null} |
| 56 |
* @param list the value array |
| 57 |
* @param provider the cast information provider |
| 58 |
* @return the value |
| 59 |
*/ |
| 60 |
public static ValueArray get(TypeInfo componentType, Value[] list, CastDataProvider provider) { |
| 61 |
return new ValueArray(componentType, list, provider); |
| 62 |
} |
| 63 |
|
| 64 |
@Override |
| 65 |
public TypeInfo getType() { |
| 66 |
TypeInfo type = this.type; |
| 67 |
if (type == null) { |
| 68 |
TypeInfo componentType = getComponentType(); |
| 69 |
this.type = type = TypeInfo.getTypeInfo(getValueType(), values.length, 0, componentType); |
| 70 |
} |
| 71 |
return type; |
| 72 |
} |
| 73 |
|
| 74 |
@Override |
| 75 |
public int getValueType() { |
| 76 |
return ARRAY; |
| 77 |
} |
| 78 |
|
| 79 |
public TypeInfo getComponentType() { |
| 80 |
return componentType; |
| 81 |
} |
| 82 |
|
| 83 |
@Override |
| 84 |
public String getString() { |
| 85 |
StringBuilder builder = new StringBuilder().append('['); |
| 86 |
for (int i = 0; i < values.length; i++) { |
| 87 |
if (i > 0) { |
| 88 |
builder.append(", "); |
| 89 |
} |
| 90 |
builder.append(values[i].getString()); |
| 91 |
} |
| 92 |
return builder.append(']').toString(); |
| 93 |
} |
| 94 |
|
| 95 |
@Override |
| 96 |
public int compareTypeSafe(Value o, CompareMode mode, CastDataProvider provider) { |
| 97 |
ValueArray v = (ValueArray) o; |
| 98 |
if (values == v.values) { |
| 99 |
return 0; |
| 100 |
} |
| 101 |
int l = values.length; |
| 102 |
int ol = v.values.length; |
| 103 |
int len = Math.min(l, ol); |
| 104 |
for (int i = 0; i < len; i++) { |
| 105 |
Value v1 = values[i]; |
| 106 |
Value v2 = v.values[i]; |
| 107 |
int comp = v1.compareTo(v2, provider, mode); |
| 108 |
if (comp != 0) { |
| 109 |
return comp; |
| 110 |
} |
| 111 |
} |
| 112 |
return Integer.compare(l, ol); |
| 113 |
} |
| 114 |
|
| 115 |
@Override |
| 116 |
public StringBuilder getSQL(StringBuilder builder, int sqlFlags) { |
| 117 |
builder.append("ARRAY ["); |
| 118 |
int length = values.length; |
| 119 |
for (int i = 0; i < length; i++) { |
| 120 |
if (i > 0) { |
| 121 |
builder.append(", "); |
| 122 |
} |
| 123 |
values[i].getSQL(builder, sqlFlags); |
| 124 |
} |
| 125 |
return builder.append(']'); |
| 126 |
} |
| 127 |
|
| 128 |
@Override |
| 129 |
public boolean equals(Object other) { |
| 130 |
if (!(other instanceof ValueArray)) { |
| 131 |
return false; |
| 132 |
} |
| 133 |
ValueArray v = (ValueArray) other; |
| 134 |
if (values == v.values) { |
| 135 |
return true; |
| 136 |
} |
| 137 |
int len = values.length; |
| 138 |
if (len != v.values.length) { |
| 139 |
return false; |
| 140 |
} |
| 141 |
for (int i = 0; i < len; i++) { |
| 142 |
if (!values[i].equals(v.values[i])) { |
| 143 |
return false; |
| 144 |
} |
| 145 |
} |
| 146 |
return true; |
| 147 |
} |
| 148 |
|
| 149 |
} |