File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\value\ValueStringIgnoreCase.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 org.h2.constant.SysProperties;
10 import org.h2.util.StringUtils;
11
12 /**
13 * Implementation of the VARCHAR_IGNORECASE data type.
14 */
15 public class ValueStringIgnoreCase extends ValueString {
16
17 private static final ValueStringIgnoreCase EMPTY = new ValueStringIgnoreCase("");
18 private int hash;
19
20 protected ValueStringIgnoreCase(String value) {
21 super(value);
22 }
23
24 public int getType() {
25 return Value.STRING_IGNORECASE;
26 }
27
28 protected int compareSecure(Value o, CompareMode mode) {
29 ValueStringIgnoreCase v = (ValueStringIgnoreCase) o;
30 return mode.compareString(value, v.value, true);
31 }
32
33 public boolean equals(Object other) {
34 return other instanceof ValueString && value.equalsIgnoreCase(((ValueString) other).value);
35 }
36
37 public int hashCode() {
38 if (hash == 0) {
39 // this is locale sensitive
40 hash = value.toUpperCase().hashCode();
41 }
42 return hash;
43 }
44
45 public String getSQL() {
46 return "CAST(" + StringUtils.quoteStringSQL(value) + " AS VARCHAR_IGNORECASE)";
47 }
48
49 /**
50 * Get or create a case insensitive string value for the given string.
51 * The value will have the same case as the passed string.
52 *
53 * @param s the string
54 * @return the value
55 */
56 public static ValueStringIgnoreCase get(String s) {
57 if (s.length() == 0) {
58 return EMPTY;
59 }
60 ValueStringIgnoreCase obj = new ValueStringIgnoreCase(StringUtils.cache(s));
61 if (s.length() > SysProperties.OBJECT_CACHE_MAX_PER_ELEMENT_SIZE) {
62 return obj;
63 }
64 ValueStringIgnoreCase cache = (ValueStringIgnoreCase) Value.cache(obj);
65 // the cached object could have the wrong case
66 // (it would still be 'equal', but we don't like to store it)
67 if (cache.value.equals(s)) {
68 return cache;
69 }
70 return obj;
71 }
72
73 protected ValueString getNew(String s) {
74 return ValueStringIgnoreCase.get(s);
75 }
76
77 }
78