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 java.sql.SQLException;
09:
10: import org.h2.constant.SysProperties;
11: import org.h2.util.MathUtils;
12: import org.h2.util.StringCache;
13:
14: /**
15: * Implementation of the CHAR data type.
16: */
17: public class ValueStringFixed extends ValueStringBase {
18:
19: private static final ValueStringFixed EMPTY = new ValueStringFixed(
20: "");
21:
22: protected ValueStringFixed(String value) {
23: super (value);
24: }
25:
26: protected int compareSecure(Value o, CompareMode mode)
27: throws SQLException {
28: // compatibility: the other object could be ValueString
29: ValueStringBase v = (ValueStringBase) o;
30: return mode.compareString(value, v.value, false);
31: }
32:
33: private static String trimRight(String s) {
34: int endIndex = s.length() - 1;
35: int i = endIndex;
36: while (i >= 0 && s.charAt(i) == ' ') {
37: i--;
38: }
39: s = i == endIndex ? s : s.substring(0, i + 1);
40: return s;
41: }
42:
43: public boolean equals(Object other) {
44: return other instanceof ValueStringBase
45: && value.equals(((ValueStringBase) other).value);
46: }
47:
48: public int hashCode() {
49: // TODO hash performance: could build a quicker hash
50: // by hashing the size and a few characters
51: return value.hashCode();
52: }
53:
54: public int getType() {
55: return Value.STRING_FIXED;
56: }
57:
58: public static ValueStringFixed get(String s) {
59: if (s.length() == 0) {
60: return EMPTY;
61: }
62: s = trimRight(s);
63: ValueStringFixed obj = new ValueStringFixed(StringCache.get(s));
64: if (s.length() > SysProperties.OBJECT_CACHE_MAX_PER_ELEMENT_SIZE) {
65: return obj;
66: }
67: return (ValueStringFixed) Value.cache(obj);
68: }
69:
70: public Value convertPrecision(long precision) {
71: if (precision == 0 || value.length() <= precision) {
72: return this ;
73: }
74: int p = MathUtils.convertLongToInt(precision);
75: return ValueStringFixed.get(value.substring(0, p));
76: }
77:
78: }
|