01: /*
02: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
03: * (http://h2database.com/html/license.html).
04: * Initial Developer: H2 Group
05: */
06: package org.h2.value;
07:
08: import org.h2.constant.SysProperties;
09: import org.h2.util.MathUtils;
10: import org.h2.util.StringCache;
11: import org.h2.util.StringUtils;
12:
13: /**
14: * Implementation of the VARCHAR_IGNORECASE data type.
15: */
16: public class ValueStringIgnoreCase extends ValueStringBase {
17:
18: private static final ValueStringIgnoreCase EMPTY = new ValueStringIgnoreCase(
19: "");
20: private int hash;
21:
22: protected ValueStringIgnoreCase(String value) {
23: super (value);
24: }
25:
26: public int getType() {
27: return Value.STRING_IGNORECASE;
28: }
29:
30: protected int compareSecure(Value o, CompareMode mode) {
31: ValueStringIgnoreCase v = (ValueStringIgnoreCase) o;
32: return mode.compareString(value, v.value, true);
33: }
34:
35: public boolean equals(Object other) {
36: return other instanceof ValueStringBase
37: && value
38: .equalsIgnoreCase(((ValueStringBase) other).value);
39: }
40:
41: public int hashCode() {
42: if (hash == 0) {
43: // this is locale sensitive
44: hash = value.toUpperCase().hashCode();
45: }
46: return hash;
47: }
48:
49: public String getSQL() {
50: return "CAST(" + StringUtils.quoteStringSQL(value)
51: + " AS VARCHAR_IGNORECASE)";
52: }
53:
54: public static ValueStringIgnoreCase get(String s) {
55: if (s.length() == 0) {
56: return EMPTY;
57: }
58: ValueStringIgnoreCase obj = new ValueStringIgnoreCase(
59: StringCache.get(s));
60: if (s.length() > SysProperties.OBJECT_CACHE_MAX_PER_ELEMENT_SIZE) {
61: return obj;
62: }
63: ValueStringIgnoreCase cache = (ValueStringIgnoreCase) Value
64: .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: } else {
70: return obj;
71: }
72: }
73:
74: public Value convertPrecision(long precision) {
75: if (precision == 0 || value.length() <= precision) {
76: return this ;
77: }
78: int p = MathUtils.convertLongToInt(precision);
79: return ValueStringIgnoreCase.get(value.substring(0, p));
80: }
81:
82: }
|