KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > value > ValueString


1 /*
2  * Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
3  * Initial Developer: H2 Group
4  */

5 package org.h2.value;
6
7 import org.h2.engine.Constants;
8 import org.h2.util.StringCache;
9
10 /**
11  * @author Thomas
12  */

13 public class ValueString extends ValueStringBase {
14
15     private static final ValueString EMPTY = new ValueString("");
16
17     protected ValueString(String JavaDoc value) {
18         super(value);
19     }
20
21     public int getType() {
22         return Value.STRING;
23     }
24
25     protected int compareSecure(Value o, CompareMode mode) {
26         ValueString v = (ValueString) o;
27         return mode.compareString(value, v.value, false);
28     }
29
30     public int hashCode() {
31         // TODO hash performance: could build a quicker hash by hashing the size and a few characters
32
return value.hashCode();
33         
34         // proposed code:
35
// private int hash = 0;
36
//
37
// public int hashCode() {
38
// int h = hash;
39
// if (h == 0) {
40
// String s = value;
41
// int l = s.length();
42
// if (l > 0) {
43
// if (l < 16)
44
// h = s.hashCode();
45
// else {
46
// h = l;
47
// for (int i = 1; i <= l; i <<= 1)
48
// h = 31 * (31 * h + s.charAt(i - 1)) + s.charAt(l - i);
49
// }
50
// hash = h;
51
// }
52
// }
53
// return h;
54
// }
55

56     }
57
58     public static ValueString get(String JavaDoc s) {
59         if (s.length() == 0) {
60             return EMPTY;
61         }
62         ValueString obj = new ValueString(StringCache.get(s));
63         if (s.length() > Constants.OBJECT_CACHE_MAX_PER_ELEMENT_SIZE) {
64             return obj;
65         }
66         return (ValueString) Value.cache(obj);
67         // this saves memory, but is really slow
68
// return new ValueString(s.intern());
69
}
70
71 }
72
Popular Tags