File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\value\ValueString.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 java.sql.PreparedStatement;
10 import java.sql.SQLException;
11 import org.h2.constant.SysProperties;
12 import org.h2.util.MathUtils;
13 import org.h2.util.StringUtils;
14
15 /**
16 * Implementation of the VARCHAR data type.
17 * It is also the base class for other ValueString* classes.
18 */
19 public class ValueString extends Value {
20
21 private static final ValueString EMPTY = new ValueString("");
22
23 /**
24 * The string data.
25 */
26 protected final String value;
27
28 protected ValueString(String value) {
29 this.value = value;
30 }
31
32 public String getSQL() {
33 return StringUtils.quoteStringSQL(value);
34 }
35
36 public boolean equals(Object other) {
37 return other instanceof ValueString && value.equals(((ValueString) other).value);
38 }
39
40 protected int compareSecure(Value o, CompareMode mode) {
41 // compatibility: the other object could be another type
42 ValueString v = (ValueString) o;
43 return mode.compareString(value, v.value, false);
44 }
45
46 public String getString() {
47 return value;
48 }
49
50 public long getPrecision() {
51 return value.length();
52 }
53
54 public Object getObject() {
55 return value;
56 }
57
58 public void set(PreparedStatement prep, int parameterIndex) throws SQLException {
59 prep.setString(parameterIndex, value);
60 }
61
62 public int getDisplaySize() {
63 return value.length();
64 }
65
66 public int getMemory() {
67 return value.length() * 2 + 48;
68 }
69
70 public Value convertPrecision(long precision, boolean force) {
71 if (precision == 0 || value.length() <= precision) {
72 return this;
73 }
74 int p = MathUtils.convertLongToInt(precision);
75 return getNew(value.substring(0, p));
76 }
77
78 public int hashCode() {
79 // TODO hash performance: could build a quicker hash
80 // by hashing the size and a few characters
81 return value.hashCode();
82
83 // proposed code:
84 // private int hash = 0;
85 //
86 // public int hashCode() {
87 // int h = hash;
88 // if (h == 0) {
89 // String s = value;
90 // int l = s.length();
91 // if (l > 0) {
92 // if (l < 16)
93 // h = s.hashCode();
94 // else {
95 // h = l;
96 // for (int i = 1; i <= l; i <<= 1)
97 // h = 31 *
98 // (31 * h + s.charAt(i - 1)) +
99 // s.charAt(l - i);
100 // }
101 // hash = h;
102 // }
103 // }
104 // return h;
105 // }
106
107 }
108
109 public int getType() {
110 return Value.STRING;
111 }
112
113 /**
114 * Get or create a string value for the given string.
115 *
116 * @param s the string
117 * @return the value
118 */
119 public static ValueString get(String s) {
120 if (s.length() == 0) {
121 return EMPTY;
122 }
123 ValueString obj = new ValueString(StringUtils.cache(s));
124 if (s.length() > SysProperties.OBJECT_CACHE_MAX_PER_ELEMENT_SIZE) {
125 return obj;
126 }
127 return (ValueString) Value.cache(obj);
128 // this saves memory, but is really slow
129 // return new ValueString(s.intern());
130 }
131
132 /**
133 * Create a new String value of the current class.
134 * This method is meant to be overridden by subclasses.
135 *
136 * @param s the string
137 * @return the value
138 */
139 protected ValueString getNew(String s) {
140 return ValueString.get(s);
141 }
142
143 }
144