File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\value\CompareModeDefault.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.text.CollationKey;
10 import java.text.Collator;
11 import org.h2.constant.SysProperties;
12 import org.h2.message.DbException;
13 import org.h2.util.SmallLRUCache;
14
15 /**
16 * The default implementation of CompareMode. It uses java.text.Collator.
17 */
18 public class CompareModeDefault extends CompareMode {
19
20 private final Collator collator;
21 private final SmallLRUCache<String, CollationKey> collationKeys;
22
23 protected CompareModeDefault(String name, int strength) {
24 super(name, strength);
25 collator = CompareMode.getCollator(name);
26 if (collator == null) {
27 throw DbException.throwInternalError(name);
28 }
29 collator.setStrength(strength);
30 int cacheSize = SysProperties.COLLATOR_CACHE_SIZE;
31 if (cacheSize != 0) {
32 collationKeys = SmallLRUCache.newInstance(cacheSize);
33 } else {
34 collationKeys = null;
35 }
36 }
37
38 public int compareString(String a, String b, boolean ignoreCase) {
39 if (ignoreCase) {
40 // this is locale sensitive
41 a = a.toUpperCase();
42 b = b.toUpperCase();
43 }
44 int comp;
45 if (collationKeys != null) {
46 CollationKey aKey = getKey(a);
47 CollationKey bKey = getKey(b);
48 comp = aKey.compareTo(bKey);
49 } else {
50 comp = collator.compare(a, b);
51 }
52 return comp;
53 }
54
55 public boolean equalsChars(String a, int ai, String b, int bi, boolean ignoreCase) {
56 return compareString(a.substring(ai, ai + 1), b.substring(bi, bi + 1), ignoreCase) == 0;
57 }
58
59 private CollationKey getKey(String a) {
60 synchronized (collationKeys) {
61 CollationKey key = collationKeys.get(a);
62 if (key == null) {
63 key = collator.getCollationKey(a);
64 collationKeys.put(a, key);
65 }
66 return key;
67 }
68 }
69
70 }
71