001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026: package javax.microedition.global;
027:
028: import com.sun.j2me.global.CollationAbstractionLayer;
029: import com.sun.j2me.global.CommonStringComparator;
030: import com.sun.j2me.global.LocaleHelpers;
031:
032: // JAVADOC COMMENT ELIDED
033: public final class StringComparator {
034:
035: // JAVADOC COMMENT ELIDED
036: public static final int LEVEL1 = 1;
037:
038: // JAVADOC COMMENT ELIDED
039: public static final int LEVEL2 = 2;
040:
041: // JAVADOC COMMENT ELIDED
042: public static final int LEVEL3 = 3;
043:
044: // JAVADOC COMMENT ELIDED
045: public static final int IDENTICAL = 15;
046:
047: /**
048: * Collation level
049: */
050: private int level;
051: /**
052: * Locale
053: */
054: private String locale;
055:
056: /**
057: * Instance of the CollationAbstractionLayer
058: */
059: private static CollationAbstractionLayer collationAbstractionLayer = CollationAbstractionLayer
060: .getInstance();
061:
062: /**
063: * String comparator
064: */
065: private CommonStringComparator stringComparatorImpl;
066:
067: // JAVADOC COMMENT ELIDED
068: public StringComparator() {
069: this (System.getProperty("microedition.locale"), LEVEL1);
070: }
071:
072: // JAVADOC COMMENT ELIDED
073: public StringComparator(String locale) {
074: this (locale, LEVEL1);
075: }
076:
077: // JAVADOC COMMENT ELIDED
078: public StringComparator(String locale, int level)
079: throws UnsupportedLocaleException, IllegalArgumentException {
080:
081: // check the level
082: if ((level < LEVEL1) || (level > LEVEL3)
083: && (level != IDENTICAL)) {
084: throw new IllegalArgumentException("Invalid level");
085: }
086:
087: if (!LocaleHelpers.isValidLocale(locale)
088: && !("".equals(locale))) {
089: throw new IllegalArgumentException("Invalid locale format");
090: }
091:
092: locale = LocaleHelpers.normalizeLocale(locale);
093:
094: if ("".equals(locale)) {
095: this .locale = null;
096: } else {
097: this .locale = locale;
098: }
099: this .level = level;
100:
101: if (locale == null) {
102: locale = "";
103: }
104:
105: stringComparatorImpl = collationAbstractionLayer
106: .getStringComparator(locale, level);
107: }
108:
109: // JAVADOC COMMENT ELIDED
110: public static String[] getSupportedLocales() {
111: return collationAbstractionLayer.getSupportedLocales();
112: }
113:
114: // JAVADOC COMMENT ELIDED
115: public int compare(String s1, String s2) {
116: return stringComparatorImpl.compare(s1, s2);
117: }
118:
119: // JAVADOC COMMENT ELIDED
120: public boolean equals(String s1, String s2) {
121: return stringComparatorImpl.equals(s1, s2);
122: }
123:
124: // JAVADOC COMMENT ELIDED
125: public int getLevel() {
126: return level;
127: }
128:
129: // JAVADOC COMMENT ELIDED
130: public String getLocale() {
131: return locale;
132: }
133: }
|