Debian Codegraph @

src/main/org/h2/value/ValueVarcharIgnoreCase.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.engine.CastDataProvider;
9 import org.h2.engine.SysProperties;
10 import org.h2.util.StringUtils;
11
12 /**
13 * Implementation of the VARCHAR_IGNORECASE data type.
14 */
15 public final class ValueVarcharIgnoreCase extends ValueStringBase {
16
17 private static final ValueVarcharIgnoreCase EMPTY = new ValueVarcharIgnoreCase("");
18
19 /**
20 * The hash code.
21 */
22 private int hash;
23
24 private ValueVarcharIgnoreCase(String value) {
25 super(value);
26 }
27
28 @Override
29 public int getValueType() {
30 return VARCHAR_IGNORECASE;
31 }
32
33 @Override
34 public int compareTypeSafe(Value v, CompareMode mode, CastDataProvider provider) {
35 return mode.compareString(value, ((ValueStringBase) v).value, true);
36 }
37
38 @Override
39 public boolean equals(Object other) {
40 return other instanceof ValueVarcharIgnoreCase
41 && value.equalsIgnoreCase(((ValueVarcharIgnoreCase) other).value);
42 }
43
44 @Override
45 public int hashCode() {
46 if (hash == 0) {
47 // this is locale sensitive
48 hash = value.toUpperCase().hashCode();
49 }
50 return hash;
51 }
52
53 @Override
54 public StringBuilder getSQL(StringBuilder builder, int sqlFlags) {
55 if ((sqlFlags & NO_CASTS) == 0) {
56 return StringUtils.quoteStringSQL(builder.append("CAST("), value).append(" AS VARCHAR_IGNORECASE(")
57 .append(value.length()).append("))");
58 }
59 return StringUtils.quoteStringSQL(builder, value);
60 }
61
62 /**
63 * Get or create a VARCHAR_IGNORECASE value for the given string.
64 * The value will have the same case as the passed string.
65 *
66 * @param s the string
67 * @return the value
68 */
69 public static ValueVarcharIgnoreCase get(String s) {
70 int length = s.length();
71 if (length == 0) {
72 return EMPTY;
73 }
74 ValueVarcharIgnoreCase obj = new ValueVarcharIgnoreCase(StringUtils.cache(s));
75 if (length > SysProperties.OBJECT_CACHE_MAX_PER_ELEMENT_SIZE) {
76 return obj;
77 }
78 ValueVarcharIgnoreCase cache = (ValueVarcharIgnoreCase) Value.cache(obj);
79 // the cached object could have the wrong case
80 // (it would still be 'equal', but we don't like to store it)
81 if (cache.value.equals(s)) {
82 return cache;
83 }
84 return obj;
85 }
86
87 }