Debian Codegraph @

src/main/org/h2/value/CompareModeIcu4J.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.lang.reflect.Method;
9 import java.text.Collator;
10 import java.util.Comparator;
11 import java.util.Locale;
12
13 import org.h2.message.DbException;
14 import org.h2.util.JdbcUtils;
15 import org.h2.util.StringUtils;
16
17 /**
18 * An implementation of CompareMode that uses the ICU4J Collator.
19 */
20 public class CompareModeIcu4J extends CompareMode {
21
22 private final Comparator<String> collator;
23
24 private volatile CompareModeIcu4J caseInsensitive;
25
26 protected CompareModeIcu4J(String name, int strength) {
27 super(name, strength);
28 collator = getIcu4jCollator(name, strength);
29 }
30
31 @Override
32 public int compareString(String a, String b, boolean ignoreCase) {
33 if (ignoreCase && getStrength() > Collator.SECONDARY) {
34 CompareModeIcu4J i = caseInsensitive;
35 if (i == null) {
36 caseInsensitive = i = new CompareModeIcu4J(getName(), Collator.SECONDARY);
37 }
38 return i.compareString(a, b, false);
39 }
40 return collator.compare(a, b);
41 }
42
43 @Override
44 public boolean equalsChars(String a, int ai, String b, int bi,
45 boolean ignoreCase) {
46 return compareString(a.substring(ai, ai + 1), b.substring(bi, bi + 1),
47 ignoreCase) == 0;
48 }
49
50 @SuppressWarnings("unchecked")
51 private static Comparator<String> getIcu4jCollator(String name, int strength) {
52 try {
53 Comparator<String> result = null;
54 Class<?> collatorClass = JdbcUtils.loadUserClass(
55 "com.ibm.icu.text.Collator");
56 Method getInstanceMethod = collatorClass.getMethod(
57 "getInstance", Locale.class);
58 int length = name.length();
59 if (length == 2) {
60 Locale locale = new Locale(StringUtils.toLowerEnglish(name), "");
61 if (compareLocaleNames(locale, name)) {
62 result = (Comparator<String>) getInstanceMethod.invoke(null, locale);
63 }
64 } else if (length == 5) {
65 // LL_CC (language_country)
66 int idx = name.indexOf('_');
67 if (idx >= 0) {
68 String language = StringUtils.toLowerEnglish(name.substring(0, idx));
69 String country = name.substring(idx + 1);
70 Locale locale = new Locale(language, country);
71 if (compareLocaleNames(locale, name)) {
72 result = (Comparator<String>) getInstanceMethod.invoke(null, locale);
73 }
74 }
75 }
76 if (result == null) {
77 for (Locale locale : (Locale[]) collatorClass.getMethod(
78 "getAvailableLocales").invoke(null)) {
79 if (compareLocaleNames(locale, name)) {
80 result = (Comparator<String>) getInstanceMethod.invoke(null, locale);
81 break;
82 }
83 }
84 }
85 if (result == null) {
86 throw DbException.getInvalidValueException("collator", name);
87 }
88 collatorClass.getMethod("setStrength", int.class).invoke(result, strength);
89 return result;
90 } catch (Exception e) {
91 throw DbException.convert(e);
92 }
93 }
94
95 }