1 | /* |
2 | * Copyright 2004-2014 H2 Group. Multiple-Licensed under the MPL 2.0, |
3 | * and the EPL 1.0 (http://h2database.com/html/license.html). |
4 | * Initial Developer: H2 Group |
5 | */ |
6 | package org.h2.value; |
7 | |
8 | import org.h2.engine.SysProperties; |
9 | import org.h2.util.StringUtils; |
10 | |
11 | /** |
12 | * Implementation of the CHAR data type. |
13 | */ |
14 | public class ValueStringFixed extends ValueString { |
15 | |
16 | private static final ValueStringFixed EMPTY = new ValueStringFixed(""); |
17 | |
18 | protected ValueStringFixed(String value) { |
19 | super(value); |
20 | } |
21 | |
22 | private static String trimRight(String s) { |
23 | int endIndex = s.length() - 1; |
24 | int i = endIndex; |
25 | while (i >= 0 && s.charAt(i) == ' ') { |
26 | i--; |
27 | } |
28 | s = i == endIndex ? s : s.substring(0, i + 1); |
29 | return s; |
30 | } |
31 | |
32 | @Override |
33 | public int getType() { |
34 | return Value.STRING_FIXED; |
35 | } |
36 | |
37 | /** |
38 | * Get or create a fixed length string value for the given string. |
39 | * Spaces at the end of the string will be removed. |
40 | * |
41 | * @param s the string |
42 | * @return the value |
43 | */ |
44 | public static ValueStringFixed get(String s) { |
45 | s = trimRight(s); |
46 | if (s.length() == 0) { |
47 | return EMPTY; |
48 | } |
49 | ValueStringFixed obj = new ValueStringFixed(StringUtils.cache(s)); |
50 | if (s.length() > SysProperties.OBJECT_CACHE_MAX_PER_ELEMENT_SIZE) { |
51 | return obj; |
52 | } |
53 | return (ValueStringFixed) Value.cache(obj); |
54 | } |
55 | |
56 | @Override |
57 | protected ValueString getNew(String s) { |
58 | return ValueStringFixed.get(s); |
59 | } |
60 | |
61 | } |