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 com.sun.j2me.global;
027:
028: import javax.microedition.global.StringComparator;
029: import javax.microedition.global.ResourceManager;
030: import javax.microedition.global.ResourceException;
031: import javax.microedition.global.UnsupportedLocaleException;
032:
033: /**
034: * This class realizes string comparison methods of
035: * {@link javax.microedition.global.StringComparator}. Specifically, these are:
036: * <ul>
037: * <li> {@link #compare(String, String)}
038: * <li> {@link #equals(String, String)}
039: * </ul>
040: * <p>
041: * This realization of <code>StringComparator</code> is intended to be based on
042: * native string comparison functions. Regarding this,
043: * <code>StringComparatorImpl</code> uses locale index that points to its locale
044: * instead of working with explicit locale code.
045: */
046: public class StringComparatorImpl implements CommonStringComparator {
047:
048: /**
049: * Current <code>StringComparatorImpl</code> level of comparison.
050: * Possible values are:
051: * <ul>
052: * <li> javax.microedition.global.StringComparator#LEVEL1
053: * <li> javax.microedition.global.StringComparator#LEVEL2
054: * <li> javax.microedition.global.StringComparator#LEVEL3
055: * <li> javax.microedition.global.StringComparator#IDENTICAL
056: * </ul>
057: */
058: private int level;
059:
060: /**
061: * Current <code>StringComparatorImpl</code> locale index in the list of
062: * supported locales.
063: */
064: private int locale_index;
065:
066: /**
067: * Constructs an instance using the specified locale and collation level.
068: * <p>
069: * If the specified locale is <code>null</code> or the empty string, this
070: * class uses the generic collation algorithm.
071: *
072: * @param locale the locale to use, or <code>null</code> to use the generic
073: * collation algorithm
074: * @param level the collation level to use
075: * @throws UnsupportedLocaleException if the specified locale is not
076: * supported by the StringComparator implementation
077: * @throws IllegalArgumentException if the specified locale is
078: * non-<code>null</code> but is not valid according to the MIDP
079: * specification, or if <code>level</code> is not one of the constants
080: * defined in this class
081: * @see javax.microedition.global.StringComparator#LEVEL1
082: * @see javax.microedition.global.StringComparator#LEVEL2
083: * @see javax.microedition.global.StringComparator#LEVEL3
084: * @see javax.microedition.global.StringComparator#IDENTICAL
085: */
086: public StringComparatorImpl(String locale, int level) {
087: this .level = level;
088:
089: locale_index = CollationAbstractionLayerImpl
090: .getCollationLocaleIndex(locale);
091: if (locale_index == -1) {
092: throw new UnsupportedLocaleException("Locale \"" + locale
093: + "\" unsupported.");
094: }
095: }
096:
097: /**
098: * Compares the two strings using the rules specific to the associated
099: * locale of this instance. Use the <code>equals</code> method to more
100: * conveniently test for equality.
101: *
102: * @param s1 first string to compare
103: * @param s2 second string to compare
104: * @return negative if <code>s1</code> belongs before <code>s2</code>,
105: * zero if the strings are equal, positive if <code>s1</code> belongs
106: * after <code>s2</code>
107: * @throws NullPointerException if either <code>s1</code> or <code>s2</code>
108: * is <code>null</code>
109: * @see #equals(String, String)
110: */
111: public int compare(String s1, String s2) {
112: if (s1 == null || s2 == null) {
113: throw new NullPointerException(
114: "Cannot compare null strings");
115: }
116: return compare0(locale_index, s1, s2, level);
117: }
118:
119: /**
120: * Tests if the two strings are equal according to the rules specific to
121: * the associated locale of this instance.
122: *
123: * @param s1 first string to compare
124: * @param s2 second string to compare
125: * @return <code>true</code> if the strings are equal, <code>false</code>
126: * if not
127: * @throws NullPointerException if either s1 or s2 is <code>null</code>
128: */
129: public boolean equals(String s1, String s2) {
130: return compare(s1, s2) == 0;
131: }
132:
133: /**
134: * Compare two strings using locale- and level-specific rules.
135: *
136: * @param s1 first string to compare
137: * @param s2 second string to compare
138: * @param locale_index the locale index in supported locales list
139: * @param level the collation level to use
140: * @return negative if <code>s1</code> belongs before <code>s2</code>,
141: * zero if the strings are equal, positive if <code>s1</code> belongs
142: * after <code>s2</code>
143: */
144: private static native int compare0(int locale_index, String s1,
145: String s2, int level);
146: }
|