Debian Codegraph @

src/main/org/h2/value/CompareMode.java

raw
1 /*
2 * Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0,
3 * and the EPL 1.0 (https://h2database.com/html/license.html).
4 * Initial Developer: H2 Group
5 */
6 package org.h2.value;
7
8 import java.nio.charset.Charset;
9 import java.text.Collator;
10 import java.util.Comparator;
11 import java.util.Locale;
12 import java.util.Objects;
13
14 import org.h2.util.StringUtils;
15
16 /**
17 * Instances of this class can compare strings. Case sensitive and case
18 * insensitive comparison is supported, and comparison using a collator.
19 */
20 public class CompareMode implements Comparator<Value> {
21
22 /**
23 * This constant means there is no collator set, and the default string
24 * comparison is to be used.
25 */
26 public static final String OFF = "OFF";
27
28 /**
29 * This constant means the default collator should be used, even if ICU4J is
30 * in the classpath.
31 */
32 public static final String DEFAULT = "DEFAULT_";
33
34 /**
35 * This constant means ICU4J should be used (this will fail if it is not in
36 * the classpath).
37 */
38 public static final String ICU4J = "ICU4J_";
39
40 /**
41 * This constant means the charset specified should be used.
42 * This will fail if the specified charset does not exist.
43 */
44 public static final String CHARSET = "CHARSET_";
45
46 private static Locale[] LOCALES;
47
48 private static volatile CompareMode lastUsed;
49
50 private static final boolean CAN_USE_ICU4J;
51
52 static {
53 boolean b = false;
54 try {
55 Class.forName("com.ibm.icu.text.Collator");
56 b = true;
57 } catch (Exception e) {
58 // ignore
59 }
60 CAN_USE_ICU4J = b;
61 }
62
63 private final String name;
64 private final int strength;
65
66 protected CompareMode(String name, int strength) {
67 this.name = name;
68 this.strength = strength;
69 }
70
71 /**
72 * Create a new compare mode with the given collator and strength. If
73 * required, a new CompareMode is created, or if possible the last one is
74 * returned. A cache is used to speed up comparison when using a collator;
75 * CollationKey objects are cached.
76 *
77 * @param name the collation name or null
78 * @param strength the collation strength
79 * @return the compare mode
80 */
81 public static CompareMode getInstance(String name, int strength) {
82 CompareMode last = lastUsed;
83 if (last != null && Objects.equals(last.name, name) && last.strength == strength) {
84 return last;
85 }
86 if (name == null || name.equals(OFF)) {
87 last = new CompareMode(name, strength);
88 } else {
89 boolean useICU4J;
90 if (name.startsWith(ICU4J)) {
91 useICU4J = true;
92 name = name.substring(ICU4J.length());
93 } else if (name.startsWith(DEFAULT)) {
94 useICU4J = false;
95 name = name.substring(DEFAULT.length());
96 } else if (name.startsWith(CHARSET)) {
97 useICU4J = false;
98 } else {
99 useICU4J = CAN_USE_ICU4J;
100 }
101 if (useICU4J) {
102 last = new CompareModeIcu4J(name, strength);
103 } else {
104 last = new CompareModeDefault(name, strength);
105 }
106 }
107 lastUsed = last;
108 return last;
109 }
110
111 /**
112 * Returns available locales for collations.
113 *
114 * @param onlyIfInitialized
115 * if {@code true}, returns {@code null} when locales are not yet
116 * initialized
117 * @return available locales for collations.
118 */
119 public static Locale[] getCollationLocales(boolean onlyIfInitialized) {
120 Locale[] locales = LOCALES;
121 if (locales == null && !onlyIfInitialized) {
122 LOCALES = locales = Collator.getAvailableLocales();
123 }
124 return locales;
125 }
126
127 /**
128 * Compare two characters in a string.
129 *
130 * @param a the first string
131 * @param ai the character index in the first string
132 * @param b the second string
133 * @param bi the character index in the second string
134 * @param ignoreCase true if a case-insensitive comparison should be made
135 * @return true if the characters are equals
136 */
137 public boolean equalsChars(String a, int ai, String b, int bi, boolean ignoreCase) {
138 char ca = a.charAt(ai);
139 char cb = b.charAt(bi);
140 if (ca == cb) {
141 return true;
142 }
143 if (ignoreCase) {
144 if (Character.toUpperCase(ca) == Character.toUpperCase(cb)
145 || Character.toLowerCase(ca) == Character.toLowerCase(cb)) {
146 return true;
147 }
148 }
149 return false;
150 }
151
152 /**
153 * Compare two strings.
154 *
155 * @param a the first string
156 * @param b the second string
157 * @param ignoreCase true if a case-insensitive comparison should be made
158 * @return -1 if the first string is 'smaller', 1 if the second string is
159 * smaller, and 0 if they are equal
160 */
161 public int compareString(String a, String b, boolean ignoreCase) {
162 if (ignoreCase) {
163 return a.compareToIgnoreCase(b);
164 }
165 return a.compareTo(b);
166 }
167
168 /**
169 * Get the collation name.
170 *
171 * @param l the locale
172 * @return the name of the collation
173 */
174 public static String getName(Locale l) {
175 Locale english = Locale.ENGLISH;
176 String name = l.getDisplayLanguage(english) + ' ' +
177 l.getDisplayCountry(english) + ' ' + l.getVariant();
178 name = StringUtils.toUpperEnglish(name.trim().replace(' ', '_'));
179 return name;
180 }
181
182 /**
183 * Compare name of the locale with the given name. The case of the name
184 * is ignored.
185 *
186 * @param locale the locale
187 * @param name the name
188 * @return true if they match
189 */
190 static boolean compareLocaleNames(Locale locale, String name) {
191 return name.equalsIgnoreCase(locale.toString()) || name.equalsIgnoreCase(locale.toLanguageTag()) ||
192 name.equalsIgnoreCase(getName(locale));
193 }
194
195 /**
196 * Get the collator object for the given language name or language / country
197 * combination.
198 *
199 * @param name the language name
200 * @return the collator
201 */
202 public static Collator getCollator(String name) {
203 Collator result = null;
204 if (name.startsWith(ICU4J)) {
205 name = name.substring(ICU4J.length());
206 } else if (name.startsWith(DEFAULT)) {
207 name = name.substring(DEFAULT.length());
208 } else if (name.startsWith(CHARSET)) {
209 return new CharsetCollator(Charset.forName(name.substring(CHARSET.length())));
210 }
211 int length = name.length();
212 if (length == 2) {
213 Locale locale = new Locale(StringUtils.toLowerEnglish(name), "");
214 if (compareLocaleNames(locale, name)) {
215 result = Collator.getInstance(locale);
216 }
217 } else if (length == 5) {
218 // LL_CC (language_country)
219 int idx = name.indexOf('_');
220 if (idx >= 0) {
221 String language = StringUtils.toLowerEnglish(name.substring(0, idx));
222 String country = name.substring(idx + 1);
223 Locale locale = new Locale(language, country);
224 if (compareLocaleNames(locale, name)) {
225 result = Collator.getInstance(locale);
226 }
227 }
228 } else if (name.indexOf('-') > 0) {
229 Locale locale = Locale.forLanguageTag(name);
230 if (!locale.getLanguage().isEmpty()) {
231 return Collator.getInstance(locale);
232 }
233 }
234 if (result == null) {
235 for (Locale locale : getCollationLocales(false)) {
236 if (compareLocaleNames(locale, name)) {
237 result = Collator.getInstance(locale);
238 break;
239 }
240 }
241 }
242 return result;
243 }
244
245 public String getName() {
246 return name == null ? OFF : name;
247 }
248
249 public int getStrength() {
250 return strength;
251 }
252
253 @Override
254 public boolean equals(Object obj) {
255 if (obj == this) {
256 return true;
257 } else if (!(obj instanceof CompareMode)) {
258 return false;
259 }
260 CompareMode o = (CompareMode) obj;
261 if (!getName().equals(o.getName())) {
262 return false;
263 }
264 if (strength != o.strength) {
265 return false;
266 }
267 return true;
268 }
269
270 @Override
271 public int hashCode() {
272 int result = 1;
273 result = 31 * result + getName().hashCode();
274 result = 31 * result + strength;
275 return result;
276 }
277
278 @Override
279 public int compare(Value o1, Value o2) {
280 return o1.compareTo(o2, null, this);
281 }
282
283 }