Debian Codegraph @

src/main/org/h2/value/ValueStringBase.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 java.math.BigDecimal;
9 import java.nio.charset.StandardCharsets;
10
11 import org.h2.api.ErrorCode;
12 import org.h2.engine.CastDataProvider;
13 import org.h2.engine.Constants;
14 import org.h2.message.DbException;
15
16 /**
17 * Base implementation of String based data types.
18 */
19 abstract class ValueStringBase extends Value {
20
21 /**
22 * The value.
23 */
24 String value;
25
26 private TypeInfo type;
27
28 ValueStringBase(String v) {
29 int length = v.length();
30 if (length > Constants.MAX_STRING_LENGTH) {
31 throw DbException.getValueTooLongException(getTypeName(getValueType()), v, length);
32 }
33 this.value = v;
34 }
35
36 @Override
37 public final TypeInfo getType() {
38 TypeInfo type = this.type;
39 if (type == null) {
40 int length = value.length();
41 this.type = type = new TypeInfo(getValueType(), length, 0, null);
42 }
43 return type;
44 }
45
46 @Override
47 public int compareTypeSafe(Value v, CompareMode mode, CastDataProvider provider) {
48 return mode.compareString(value, ((ValueStringBase) v).value, false);
49 }
50
51 @Override
52 public int hashCode() {
53 // TODO hash performance: could build a quicker hash
54 // by hashing the size and a few characters
55 return getClass().hashCode() ^ value.hashCode();
56
57 // proposed code:
58 // private int hash = 0;
59 //
60 // public int hashCode() {
61 // int h = hash;
62 // if (h == 0) {
63 // String s = value;
64 // int l = s.length();
65 // if (l > 0) {
66 // if (l < 16)
67 // h = s.hashCode();
68 // else {
69 // h = l;
70 // for (int i = 1; i <= l; i <<= 1)
71 // h = 31 *
72 // (31 * h + s.charAt(i - 1)) +
73 // s.charAt(l - i);
74 // }
75 // hash = h;
76 // }
77 // }
78 // return h;
79 // }
80 }
81
82 @Override
83 public final String getString() {
84 return value;
85 }
86
87 @Override
88 public final byte[] getBytes() {
89 return value.getBytes(StandardCharsets.UTF_8);
90 }
91
92 @Override
93 public final boolean getBoolean() {
94 String s = value.trim();
95 if (s.equalsIgnoreCase("true") || s.equalsIgnoreCase("t") || s.equalsIgnoreCase("yes")
96 || s.equalsIgnoreCase("y")) {
97 return true;
98 } else if (s.equalsIgnoreCase("false") || s.equalsIgnoreCase("f") || s.equalsIgnoreCase("no")
99 || s.equalsIgnoreCase("n")) {
100 return false;
101 }
102 try {
103 // convert to a number, and if it is not 0 then it is true
104 return new BigDecimal(s).signum() != 0;
105 } catch (NumberFormatException e) {
106 throw getDataConversionError(BOOLEAN);
107 }
108 }
109
110 @Override
111 public final byte getByte() {
112 try {
113 return Byte.parseByte(value.trim());
114 } catch (NumberFormatException e) {
115 throw DbException.get(ErrorCode.DATA_CONVERSION_ERROR_1, e, value);
116 }
117 }
118
119 @Override
120 public final short getShort() {
121 try {
122 return Short.parseShort(value.trim());
123 } catch (NumberFormatException e) {
124 throw DbException.get(ErrorCode.DATA_CONVERSION_ERROR_1, e, value);
125 }
126 }
127
128 @Override
129 public final int getInt() {
130 try {
131 return Integer.parseInt(value.trim());
132 } catch (NumberFormatException e) {
133 throw DbException.get(ErrorCode.DATA_CONVERSION_ERROR_1, e, value);
134 }
135 }
136
137 @Override
138 public final long getLong() {
139 try {
140 return Long.parseLong(value.trim());
141 } catch (NumberFormatException e) {
142 throw DbException.get(ErrorCode.DATA_CONVERSION_ERROR_1, e, value);
143 }
144 }
145
146 @Override
147 public final BigDecimal getBigDecimal() {
148 try {
149 return new BigDecimal(value.trim());
150 } catch (NumberFormatException e) {
151 throw DbException.get(ErrorCode.DATA_CONVERSION_ERROR_1, e, value);
152 }
153 }
154
155 @Override
156 public final float getFloat() {
157 try {
158 return Float.parseFloat(value.trim());
159 } catch (NumberFormatException e) {
160 throw DbException.get(ErrorCode.DATA_CONVERSION_ERROR_1, e, value);
161 }
162 }
163
164 @Override
165 public final double getDouble() {
166 try {
167 return Double.parseDouble(value.trim());
168 } catch (NumberFormatException e) {
169 throw DbException.get(ErrorCode.DATA_CONVERSION_ERROR_1, e, value);
170 }
171 }
172
173 @Override
174 public final int getMemory() {
175 /*
176 * Java 11 with -XX:-UseCompressedOops
177 * Empty string: 88 bytes
178 * 1 to 4 UTF-16 chars: 96 bytes
179 */
180 return value.length() * 2 + 94;
181 }
182
183 @Override
184 public boolean equals(Object other) {
185 return other != null && getClass() == other.getClass() && value.equals(((ValueStringBase) other).value);
186 }
187
188 }