FileDocCategorySizeDatePackage
StringComparatorImpl.javaAPI DocphoneME MR2 API (J2ME)5797Wed May 02 18:00:46 BST 2007com.sun.j2me.global

StringComparatorImpl

public class StringComparatorImpl extends Object implements CommonStringComparator
This class realizes string comparison methods of {@link javax.microedition.global.StringComparator}. Specifically, these are:
  • {@link #compare(String, String)}
  • {@link #equals(String, String)}

This realization of StringComparator is intended to be based on native string comparison functions. Regarding this, StringComparatorImpl uses locale index that points to its locale instead of working with explicit locale code.

Fields Summary
private int
level
Current StringComparatorImpl level of comparison. Possible values are:
  • javax.microedition.global.StringComparator#LEVEL1
  • javax.microedition.global.StringComparator#LEVEL2
  • javax.microedition.global.StringComparator#LEVEL3
  • javax.microedition.global.StringComparator#IDENTICAL
private int
locale_index
Current StringComparatorImpl locale index in the list of supported locales.
Constructors Summary
public StringComparatorImpl(String locale, int level)
Constructs an instance using the specified locale and collation level.

If the specified locale is null or the empty string, this class uses the generic collation algorithm.

param
locale the locale to use, or null to use the generic collation algorithm
param
level the collation level to use
throws
UnsupportedLocaleException if the specified locale is not supported by the StringComparator implementation
throws
IllegalArgumentException if the specified locale is non-null but is not valid according to the MIDP specification, or if level is not one of the constants defined in this class
see
javax.microedition.global.StringComparator#LEVEL1
see
javax.microedition.global.StringComparator#LEVEL2
see
javax.microedition.global.StringComparator#LEVEL3
see
javax.microedition.global.StringComparator#IDENTICAL

        this.level = level;

		locale_index = CollationAbstractionLayerImpl.getCollationLocaleIndex(locale);
		if (locale_index == -1) {
			throw new UnsupportedLocaleException("Locale \""
												 + locale +
												 "\" unsupported.");
		}
    
Methods Summary
public intcompare(java.lang.String s1, java.lang.String s2)
Compares the two strings using the rules specific to the associated locale of this instance. Use the equals method to more conveniently test for equality.

param
s1 first string to compare
param
s2 second string to compare
return
negative if s1 belongs before s2, zero if the strings are equal, positive if s1 belongs after s2
throws
NullPointerException if either s1 or s2 is null
see
#equals(String, String)

        if (s1 == null || s2 == null) {
            throw new NullPointerException("Cannot compare null strings");
        }
        return compare0(locale_index, s1, s2, level);
    
private static native intcompare0(int locale_index, java.lang.String s1, java.lang.String s2, int level)
Compare two strings using locale- and level-specific rules.

param
s1 first string to compare
param
s2 second string to compare
param
locale_index the locale index in supported locales list
param
level the collation level to use
return
negative if s1 belongs before s2, zero if the strings are equal, positive if s1 belongs after s2

public booleanequals(java.lang.String s1, java.lang.String s2)
Tests if the two strings are equal according to the rules specific to the associated locale of this instance.

param
s1 first string to compare
param
s2 second string to compare
return
true if the strings are equal, false if not
throws
NullPointerException if either s1 or s2 is null

        return compare(s1, s2) == 0;