Search in sources :

Example 76 with RuleBasedCollator

use of android.icu.text.RuleBasedCollator in project j2objc by google.

the class RbnfScannerProviderImpl method createScanner.

/**
 * @deprecated This API is ICU internal only.
 * @hide draft / provisional / internal are hidden on Android
 */
@Deprecated
protected RbnfLenientScanner createScanner(ULocale locale, String extras) {
    RuleBasedCollator collator = null;
    try {
        // create a default collator based on the locale,
        // then pull out that collator's rules, append any additional
        // rules specified in the description, and create a _new_
        // collator based on the combination of those rules
        collator = (RuleBasedCollator) Collator.getInstance(locale.toLocale());
        if (extras != null) {
            String rules = collator.getRules() + extras;
            collator = new RuleBasedCollator(rules);
        }
        collator.setDecomposition(Collator.CANONICAL_DECOMPOSITION);
    } catch (Exception e) {
        // /CLOVER:OFF
        if (DEBUG) {
            // debug hook
            e.printStackTrace();
            System.out.println("++++");
        }
        collator = null;
    // /CLOVER:ON
    }
    return new RbnfLenientScannerImpl(collator);
}
Also used : RuleBasedCollator(android.icu.text.RuleBasedCollator)

Example 77 with RuleBasedCollator

use of android.icu.text.RuleBasedCollator in project j2objc by google.

the class AlphabeticIndexTest method TestFirstCharacters.

@Test
public void TestFirstCharacters() {
    AlphabeticIndex alphabeticIndex = new AlphabeticIndex(Locale.ENGLISH);
    RuleBasedCollator collator = alphabeticIndex.getCollator();
    collator.setStrength(Collator.IDENTICAL);
    Collection<String> firsts = alphabeticIndex.getFirstCharactersInScripts();
    // Verify that each script is represented exactly once.
    // Exclude pseudo-scripts like Common (no letters).
    // Exclude scripts like Braille and Sutton SignWriting
    // because they only have symbols, not letters.
    UnicodeSet missingScripts = new UnicodeSet("[^[:inherited:][:unknown:][:common:][:Braille:][:SignWriting:]]");
    String last = "";
    for (String index : firsts) {
        if (collator.compare(last, index) >= 0) {
            errln("Characters not in order: " + last + " !< " + index);
        }
        int script = getFirstRealScript(index);
        if (script == UScript.UNKNOWN) {
            continue;
        }
        UnicodeSet s = new UnicodeSet().applyIntPropertyValue(UProperty.SCRIPT, script);
        if (missingScripts.containsNone(s)) {
            errln("2nd character in script: " + index + "\t" + new UnicodeSet(missingScripts).retainAll(s).toPattern(false));
        }
        missingScripts.removeAll(s);
    }
    if (missingScripts.size() != 0) {
        String missingScriptNames = "";
        UnicodeSet missingChars = new UnicodeSet(missingScripts);
        for (; ; ) {
            int c = missingChars.charAt(0);
            if (c < 0) {
                break;
            }
            int script = UScript.getScript(c);
            missingScriptNames += " " + UCharacter.getPropertyValueName(UProperty.SCRIPT, script, UProperty.NameChoice.SHORT);
            missingChars.removeAll(new UnicodeSet().applyIntPropertyValue(UProperty.SCRIPT, script));
        }
        errln("Missing character from:" + missingScriptNames + " -- " + missingScripts);
    }
}
Also used : RuleBasedCollator(android.icu.text.RuleBasedCollator) AlphabeticIndex(android.icu.text.AlphabeticIndex) UnicodeSet(android.icu.text.UnicodeSet) Test(org.junit.Test)

Example 78 with RuleBasedCollator

use of android.icu.text.RuleBasedCollator in project j2objc by google.

the class AlphabeticIndexTest method TestPinyinFirst.

/**
 * Test AlphabeticIndex vs. Pinyin with script reordering.
 */
@Test
public void TestPinyinFirst() {
    RuleBasedCollator coll = (RuleBasedCollator) Collator.getInstance(ULocale.CHINESE);
    coll.setReorderCodes(UScript.HAN);
    AlphabeticIndex index = new AlphabeticIndex(coll);
    // ... A-Z ...
    assertEquals("getBucketCount()", 28, index.getBucketCount());
    index.addLabels(Locale.CHINESE);
    // ... A-Z ...
    assertEquals("getBucketCount()", 28, index.getBucketCount());
    int bucketIndex = index.getBucketIndex("\u897f");
    assertEquals("getBucketIndex(U+897F)", 'X' - 'A' + 1, bucketIndex);
    bucketIndex = index.getBucketIndex("i");
    assertEquals("getBucketIndex(i)", 9, bucketIndex);
    bucketIndex = index.getBucketIndex("\u03B1");
    assertEquals("getBucketIndex(Greek alpha)", 27, bucketIndex);
    // U+50005 is an unassigned code point which sorts at the end, independent of the Hani group.
    bucketIndex = index.getBucketIndex(UTF16.valueOf(0x50005));
    assertEquals("getBucketIndex(U+50005)", 27, bucketIndex);
    bucketIndex = index.getBucketIndex("\uFFFF");
    assertEquals("getBucketIndex(U+FFFF)", 27, bucketIndex);
}
Also used : RuleBasedCollator(android.icu.text.RuleBasedCollator) AlphabeticIndex(android.icu.text.AlphabeticIndex) Test(org.junit.Test)

Example 79 with RuleBasedCollator

use of android.icu.text.RuleBasedCollator in project j2objc by google.

the class CollationAPITest method TestRules.

/**
 * This tests the RuleBasedCollator
 * - getRules
 */
@Test
public void TestRules() {
    // root
    RuleBasedCollator coll = (RuleBasedCollator) Collator.getInstance(new Locale("", "", ""));
    // logln("PASS: RuleBased Collator creation passed");
    String rules = coll.getRules();
    if (rules != null && rules.length() != 0) {
        errln("Root tailored rules failed");
    }
}
Also used : ULocale(android.icu.util.ULocale) Locale(java.util.Locale) RuleBasedCollator(android.icu.text.RuleBasedCollator) Test(org.junit.Test)

Example 80 with RuleBasedCollator

use of android.icu.text.RuleBasedCollator in project j2objc by google.

the class CollationAPITest method TestElemIter.

/**
 * This tests the CollationElementIterator related APIs.
 * - creation of a CollationElementIterator object
 * - == and != operators
 * - iterating forward
 * - reseting the iterator index
 * - requesting the order properties(primary, secondary or tertiary)
 */
@Test
public void TestElemIter() {
    // logln("testing sortkey begins...");
    Collator col = Collator.getInstance(Locale.ENGLISH);
    String testString1 = "XFILE What subset of all possible test cases has the highest probability of detecting the most errors?";
    String testString2 = "Xf_ile What subset of all possible test cases has the lowest probability of detecting the least errors?";
    // logln("Constructors and comparison testing....");
    CollationElementIterator iterator1 = ((RuleBasedCollator) col).getCollationElementIterator(testString1);
    CharacterIterator chariter = new StringCharacterIterator(testString1);
    // copy ctor
    CollationElementIterator iterator2 = ((RuleBasedCollator) col).getCollationElementIterator(chariter);
    UCharacterIterator uchariter = UCharacterIterator.getInstance(testString2);
    CollationElementIterator iterator3 = ((RuleBasedCollator) col).getCollationElementIterator(uchariter);
    int offset = 0;
    offset = iterator1.getOffset();
    if (offset != 0) {
        errln("Error in getOffset for collation element iterator");
        return;
    }
    iterator1.setOffset(6);
    iterator1.setOffset(0);
    int order1, order2, order3;
    order1 = iterator1.next();
    doAssert(!(iterator1.equals(iterator2)), "The first iterator advance failed");
    order2 = iterator2.next();
    // Code coverage for dummy "not designed" hashCode() which does "assert false".
    try {
        // We don't expect any particular value.
        iterator1.hashCode();
    } catch (AssertionError ignored) {
    // Expected to be thrown if assertions are enabled.
    }
    // In ICU 52 and earlier we had iterator1.equals(iterator2)
    // but in ICU 53 this fails because the iterators differ (String vs. CharacterIterator).
    // doAssert((iterator1.equals(iterator2)), "The second iterator advance failed");
    doAssert(iterator1.getOffset() == iterator2.getOffset(), "The second iterator advance failed");
    doAssert((order1 == order2), "The order result should be the same");
    order3 = iterator3.next();
    doAssert((CollationElementIterator.primaryOrder(order1) == CollationElementIterator.primaryOrder(order3)), "The primary orders should be the same");
    doAssert((CollationElementIterator.secondaryOrder(order1) == CollationElementIterator.secondaryOrder(order3)), "The secondary orders should be the same");
    doAssert((CollationElementIterator.tertiaryOrder(order1) == CollationElementIterator.tertiaryOrder(order3)), "The tertiary orders should be the same");
    order1 = iterator1.next();
    order3 = iterator3.next();
    doAssert((CollationElementIterator.primaryOrder(order1) == CollationElementIterator.primaryOrder(order3)), "The primary orders should be identical");
    doAssert((CollationElementIterator.tertiaryOrder(order1) != CollationElementIterator.tertiaryOrder(order3)), "The tertiary orders should be different");
    order1 = iterator1.next();
    order3 = iterator3.next();
    // invalid test wrong in UCA
    // doAssert((CollationElementIterator.secondaryOrder(order1) !=
    // CollationElementIterator.secondaryOrder(order3)), "The secondary orders should not be the same");
    doAssert((order1 != CollationElementIterator.NULLORDER), "Unexpected end of iterator reached");
    iterator1.reset();
    iterator2.reset();
    iterator3.reset();
    order1 = iterator1.next();
    doAssert(!(iterator1.equals(iterator2)), "The first iterator advance failed");
    order2 = iterator2.next();
    // In ICU 52 and earlier we had iterator1.equals(iterator2)
    // but in ICU 53 this fails because the iterators differ (String vs. CharacterIterator).
    // doAssert((iterator1.equals(iterator2)), "The second iterator advance failed");
    doAssert(iterator1.getOffset() == iterator2.getOffset(), "The second iterator advance failed");
    doAssert((order1 == order2), "The order result should be the same");
    order3 = iterator3.next();
    doAssert((CollationElementIterator.primaryOrder(order1) == CollationElementIterator.primaryOrder(order3)), "The primary orders should be the same");
    doAssert((CollationElementIterator.secondaryOrder(order1) == CollationElementIterator.secondaryOrder(order3)), "The secondary orders should be the same");
    doAssert((CollationElementIterator.tertiaryOrder(order1) == CollationElementIterator.tertiaryOrder(order3)), "The tertiary orders should be the same");
    order1 = iterator1.next();
    order2 = iterator2.next();
    order3 = iterator3.next();
    doAssert((CollationElementIterator.primaryOrder(order1) == CollationElementIterator.primaryOrder(order3)), "The primary orders should be identical");
    doAssert((CollationElementIterator.tertiaryOrder(order1) != CollationElementIterator.tertiaryOrder(order3)), "The tertiary orders should be different");
    order1 = iterator1.next();
    order3 = iterator3.next();
    // obsolete invalid test, removed
    // doAssert((CollationElementIterator.secondaryOrder(order1) !=
    // CollationElementIterator.secondaryOrder(order3)), "The secondary orders should not be the same");
    doAssert((order1 != CollationElementIterator.NULLORDER), "Unexpected end of iterator reached");
    doAssert(!(iterator2.equals(iterator3)), "The iterators should be different");
    logln("testing CollationElementIterator ends...");
}
Also used : RuleBasedCollator(android.icu.text.RuleBasedCollator) StringCharacterIterator(java.text.StringCharacterIterator) UCharacterIterator(android.icu.text.UCharacterIterator) CharacterIterator(java.text.CharacterIterator) StringCharacterIterator(java.text.StringCharacterIterator) UCharacterIterator(android.icu.text.UCharacterIterator) CollationElementIterator(android.icu.text.CollationElementIterator) Collator(android.icu.text.Collator) RuleBasedCollator(android.icu.text.RuleBasedCollator) Test(org.junit.Test)

Aggregations

RuleBasedCollator (android.icu.text.RuleBasedCollator)140 Test (org.junit.Test)124 Collator (android.icu.text.Collator)42 ULocale (android.icu.util.ULocale)26 CollationElementIterator (android.icu.text.CollationElementIterator)25 Locale (java.util.Locale)22 CollationKey (android.icu.text.CollationKey)17 StringCharacterIterator (java.text.StringCharacterIterator)16 StringSearch (android.icu.text.StringSearch)14 RawCollationKey (android.icu.text.RawCollationKey)11 ParseException (java.text.ParseException)10 UnicodeSet (android.icu.text.UnicodeSet)8 AlphabeticIndex (android.icu.text.AlphabeticIndex)6 BreakIterator (android.icu.text.BreakIterator)6 MissingResourceException (java.util.MissingResourceException)5 IOException (java.io.IOException)4 UnicodeSetIterator (android.icu.text.UnicodeSetIterator)3 UCharacterIterator (android.icu.text.UCharacterIterator)2 CharacterIterator (java.text.CharacterIterator)2 ArrayList (java.util.ArrayList)2