Debian Codegraph @

src/main/org/h2/value/ValueCollectionBase.java

raw
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.api.ErrorCode;
9 import org.h2.engine.CastDataProvider;
10 import org.h2.engine.Constants;
11 import org.h2.message.DbException;
12
13 /**
14 * Base class for ARRAY and ROW values.
15 */
16 public abstract class ValueCollectionBase extends Value {
17
18 /**
19 * Values.
20 */
21 final Value[] values;
22
23 private int hash;
24
25 ValueCollectionBase(Value[] values) {
26 this.values = values;
27 }
28
29 public Value[] getList() {
30 return values;
31 }
32
33 @Override
34 public int hashCode() {
35 if (hash != 0) {
36 return hash;
37 }
38 int h = getValueType();
39 for (Value v : values) {
40 h = h * 31 + v.hashCode();
41 }
42 hash = h;
43 return h;
44 }
45
46 @Override
47 public int compareWithNull(Value v, boolean forEquality, CastDataProvider provider, CompareMode compareMode) {
48 if (v == ValueNull.INSTANCE) {
49 return Integer.MIN_VALUE;
50 }
51 ValueCollectionBase l = this;
52 int leftType = l.getValueType();
53 int rightType = v.getValueType();
54 if (rightType != leftType) {
55 throw v.getDataConversionError(leftType);
56 }
57 ValueCollectionBase r = (ValueCollectionBase) v;
58 Value[] leftArray = l.values, rightArray = r.values;
59 int leftLength = leftArray.length, rightLength = rightArray.length;
60 if (leftLength != rightLength) {
61 if (leftType == ROW) {
62 throw DbException.get(ErrorCode.COLUMN_COUNT_DOES_NOT_MATCH);
63 }
64 if (forEquality) {
65 return 1;
66 }
67 }
68 if (forEquality) {
69 boolean hasNull = false;
70 for (int i = 0; i < leftLength; i++) {
71 Value v1 = leftArray[i];
72 Value v2 = rightArray[i];
73 int comp = v1.compareWithNull(v2, forEquality, provider, compareMode);
74 if (comp != 0) {
75 if (comp != Integer.MIN_VALUE) {
76 return comp;
77 }
78 hasNull = true;
79 }
80 }
81 return hasNull ? Integer.MIN_VALUE : 0;
82 }
83 int len = Math.min(leftLength, rightLength);
84 for (int i = 0; i < len; i++) {
85 Value v1 = leftArray[i];
86 Value v2 = rightArray[i];
87 int comp = v1.compareWithNull(v2, forEquality, provider, compareMode);
88 if (comp != 0) {
89 return comp;
90 }
91 }
92 return Integer.compare(leftLength, rightLength);
93 }
94
95 @Override
96 public boolean containsNull() {
97 for (Value v : values) {
98 if (v.containsNull()) {
99 return true;
100 }
101 }
102 return false;
103 }
104
105 @Override
106 public int getMemory() {
107 int memory = 72 + values.length * Constants.MEMORY_POINTER;
108 for (Value v : values) {
109 memory += v.getMemory();
110 }
111 return memory;
112 }
113
114 }