KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > value > CompareMode


1 /*
2  * Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
3  * Initial Developer: H2 Group
4  */

5 package org.h2.value;
6
7 import java.text.Collator JavaDoc;
8 import java.util.Locale JavaDoc;
9
10 import org.h2.util.StringUtils;
11
12 public class CompareMode {
13     public static final String JavaDoc OFF = "OFF";
14     private String JavaDoc name = OFF;
15     private Collator JavaDoc collator;
16     
17     public CompareMode(Collator JavaDoc collator, String JavaDoc name) {
18         this.collator = collator;
19         if(name != null) {
20             this.name = name;
21         }
22     }
23     
24     public int compareString(String JavaDoc a, String JavaDoc b, boolean ignoreCase) {
25         if(collator == null) {
26             if(ignoreCase) {
27                 return a.compareToIgnoreCase(b);
28             }
29             return a.compareTo(b);
30         }
31         if(ignoreCase) {
32             // this is locale sensitive
33
a = a.toUpperCase();
34             b = b.toUpperCase();
35         }
36         int comp = collator.compare(a, b);
37         return comp;
38     }
39     
40     public static String JavaDoc getName(Locale JavaDoc l) {
41         Locale JavaDoc english = Locale.ENGLISH;
42         String JavaDoc name = l.getDisplayLanguage(english) + ' ' + l.getDisplayCountry(english) + ' ' + l.getVariant();
43         name = StringUtils.toUpperEnglish(name.trim().replace(' ', '_'));
44         return name;
45     }
46     
47     private static boolean compareLocaleNames(Locale JavaDoc locale, String JavaDoc name) {
48         return name.equalsIgnoreCase(locale.toString()) || name.equalsIgnoreCase(getName(locale));
49     }
50
51     public static Collator JavaDoc getCollator(String JavaDoc name) {
52         Collator JavaDoc result = null;
53         if(name.length() == 2) {
54             Locale JavaDoc locale = new Locale JavaDoc(name.toLowerCase());
55             if(compareLocaleNames(locale, name)) {
56                 result = Collator.getInstance(locale);
57             }
58         } else if(name.length() == 5) {
59             int idx = name.indexOf('_');
60             if(idx >= 0) {
61                 String JavaDoc language = name.substring(0, idx).toLowerCase();
62                 String JavaDoc country = name.substring(idx + 1);
63                 Locale JavaDoc locale = new Locale JavaDoc(language, country);
64                 if(compareLocaleNames(locale, name)) {
65                     result = Collator.getInstance(locale);
66                 }
67             }
68         }
69         if(result == null) {
70             Locale JavaDoc[] locales = Collator.getAvailableLocales();
71             for(int i=0; i<locales.length; i++) {
72                 Locale JavaDoc locale = locales[i];
73                 if(compareLocaleNames(locale, name)) {
74                     result = Collator.getInstance(locale);
75                     break;
76                 }
77             }
78         }
79         return result;
80     }
81
82     public String JavaDoc getName() {
83         return name;
84     }
85     
86 }
87
Popular Tags