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:
12: /**
13: * Implementation of the VARCHAR data type.
14: */
15: public class ValueString extends ValueStringBase {
16:
17: private static final ValueString EMPTY = new ValueString("");
18:
19: protected ValueString(String value) {
20: super (value);
21: }
22:
23: public int getType() {
24: return Value.STRING;
25: }
26:
27: protected int compareSecure(Value o, CompareMode mode) {
28: // compatibility: the other object could be ValueStringFixed
29: ValueStringBase v = (ValueStringBase) o;
30: return mode.compareString(value, v.value, false);
31: }
32:
33: public boolean equals(Object other) {
34: return other instanceof ValueStringBase
35: && value.equals(((ValueStringBase) other).value);
36: }
37:
38: public int hashCode() {
39: // TODO hash performance: could build a quicker hash
40: // by hashing the size and a few characters
41: return value.hashCode();
42:
43: // proposed code:
44: // private int hash = 0;
45: //
46: // public int hashCode() {
47: // int h = hash;
48: // if (h == 0) {
49: // String s = value;
50: // int l = s.length();
51: // if (l > 0) {
52: // if (l < 16)
53: // h = s.hashCode();
54: // else {
55: // h = l;
56: // for (int i = 1; i <= l; i <<= 1)
57: // h = 31 *
58: // (31 * h + s.charAt(i - 1)) +
59: // s.charAt(l - i);
60: // }
61: // hash = h;
62: // }
63: // }
64: // return h;
65: // }
66:
67: }
68:
69: public Value convertPrecision(long precision) {
70: if (precision == 0 || value.length() <= precision) {
71: return this ;
72: }
73: int p = MathUtils.convertLongToInt(precision);
74: return ValueString.get(value.substring(0, p));
75: }
76:
77: public static ValueString get(String s) {
78: if (s.length() == 0) {
79: return EMPTY;
80: }
81: ValueString obj = new ValueString(StringCache.get(s));
82: if (s.length() > SysProperties.OBJECT_CACHE_MAX_PER_ELEMENT_SIZE) {
83: return obj;
84: }
85: return (ValueString) Value.cache(obj);
86: // this saves memory, but is really slow
87: // return new ValueString(s.intern());
88: }
89:
90: }
|