java.text.CollationElementIterator

Here are the examples of the java api class java.text.CollationElementIterator taken from open source projects.

1. WorkHorseForCollatorDatatypes#hasSingleCollationElement()

Project: derby
File: WorkHorseForCollatorDatatypes.java
/**
     * Check if the string consists of a single collation element.
     * @return {@code true} iff it's a single collation element
     * @see CollationElementsInterface#hasSingleCollationElement()
     */
boolean hasSingleCollationElement() throws StandardException {
    if (stringData.isNull()) {
        return false;
    }
    CollationElementIterator cei = collatorForCharacterDatatypes.getCollationElementIterator(stringData.getString());
    // then call next() to see that there is no more than one element.
    return cei.next() != CollationElementIterator.NULLORDER && cei.next() == CollationElementIterator.NULLORDER;
}

2. APITest#TestElemIter()

Project: openjdk
File: APITest.java
//----------------------------------------------------------------------------
// ctor -- Tests the constructor methods
//
public final void TestElemIter() {
    logln("testing sortkey begins...");
    Collator col = null;
    try {
        col = Collator.getInstance();
    } catch (Exception foo) {
        errln("Error : " + foo.getMessage());
        errln("Default collation creation failed.");
    }
    RuleBasedCollator rbCol;
    if (col instanceof RuleBasedCollator) {
        rbCol = (RuleBasedCollator) col;
    } else {
        return;
    }
    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 = rbCol.getCollationElementIterator(testString1);
    CollationElementIterator iterator2 = rbCol.getCollationElementIterator(testString1);
    CollationElementIterator iterator3 = rbCol.getCollationElementIterator(testString2);
    int order1, order2, order3;
    order1 = iterator1.next();
    order2 = iterator2.next();
    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();
    doAssert(CollationElementIterator.secondaryOrder(order1) != CollationElementIterator.secondaryOrder(order3), "The secondary orders should be different");
    doAssert(order1 != CollationElementIterator.NULLORDER, "Unexpected end of iterator reached");
    iterator1.reset();
    iterator2.reset();
    iterator3.reset();
    order1 = iterator1.next();
    order2 = iterator2.next();
    doAssert(order1 == order2, "The order result should be the same");
    order3 = iterator3.next();
    doAssert(CollationElementIterator.primaryOrder(order1) == CollationElementIterator.primaryOrder(order3), "The orders should be the same");
    doAssert(CollationElementIterator.secondaryOrder(order1) == CollationElementIterator.secondaryOrder(order3), "The orders should be the same");
    doAssert(CollationElementIterator.tertiaryOrder(order1) == CollationElementIterator.tertiaryOrder(order3), "The 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();
    doAssert(CollationElementIterator.secondaryOrder(order1) != CollationElementIterator.secondaryOrder(order3), "The secondary orders should be different");
    doAssert(order1 != CollationElementIterator.NULLORDER, "Unexpected end of iterator reached");
    logln("testing CollationElementIterator ends...");
}

3. StringComparable#getFirstCaseDiff()

Project: openjdk
File: StringComparable.java
private final int[] getFirstCaseDiff(final String text, final String pattern, final Locale locale) {
    final CollationElementIterator targIter = m_collator.getCollationElementIterator(text);
    final CollationElementIterator patIter = m_collator.getCollationElementIterator(pattern);
    int startTarg = -1;
    int endTarg = -1;
    int startPatt = -1;
    int endPatt = -1;
    final int done = getElement(CollationElementIterator.NULLORDER);
    int patternElement = 0, targetElement = 0;
    boolean getPattern = true, getTarget = true;
    while (true) {
        if (getPattern) {
            startPatt = patIter.getOffset();
            patternElement = getElement(patIter.next());
            endPatt = patIter.getOffset();
        }
        if ((getTarget)) {
            startTarg = targIter.getOffset();
            targetElement = getElement(targIter.next());
            endTarg = targIter.getOffset();
        }
        getTarget = getPattern = true;
        if ((patternElement == done) || (targetElement == done)) {
            return null;
        } else if (targetElement == 0) {
            getPattern = false;
        } else if (patternElement == 0) {
            getTarget = false;
        } else if (targetElement != patternElement) {
            // mismatch
            if ((startPatt < endPatt) && (startTarg < endTarg)) {
                final String subText = text.substring(startTarg, endTarg);
                final String subPatt = pattern.substring(startPatt, endPatt);
                final String subTextUp = subText.toUpperCase(locale);
                final String subPattUp = subPatt.toUpperCase(locale);
                if (m_collator.compare(subTextUp, subPattUp) != 0) {
                    // not case diffference
                    continue;
                }
                int diff[] = { UNKNOWN_CASE, UNKNOWN_CASE };
                if (m_collator.compare(subText, subTextUp) == 0) {
                    diff[0] = UPPER_CASE;
                } else if (m_collator.compare(subText, subText.toLowerCase(locale)) == 0) {
                    diff[0] = LOWER_CASE;
                }
                if (m_collator.compare(subPatt, subPattUp) == 0) {
                    diff[1] = UPPER_CASE;
                } else if (m_collator.compare(subPatt, subPatt.toLowerCase(locale)) == 0) {
                    diff[1] = LOWER_CASE;
                }
                if (((diff[0] == UPPER_CASE) && (diff[1] == LOWER_CASE)) || ((diff[0] == LOWER_CASE) && (diff[1] == UPPER_CASE))) {
                    return diff;
                } else {
                    // not case diff
                    continue;
                }
            } else {
                continue;
            }
        }
    }
}