Example usage for java.text CollationElementIterator next

List of usage examples for java.text CollationElementIterator next

Introduction

In this page you can find the example usage for java.text CollationElementIterator next.

Prototype

public int next() 

Source Link

Document

Get the next collation element in the string.

Usage

From source file:Search.java

public static int indexOf(String source, String pattern) {
    RuleBasedCollator rbc = (RuleBasedCollator) Collator.getInstance();
    rbc.setStrength(Collator.SECONDARY);

    CollationElementIterator textCEI;
    CollationElementIterator patCEI;
    textCEI = rbc.getCollationElementIterator(source);
    patCEI = rbc.getCollationElementIterator(pattern);

    // e1 will contain the collation element for the source
    // e2 will contain the collation element for the pattern
    int e1, e2;//from  ww  w .j a v a2s  .  c o m
    int startMatch = -1;

    // initialize e2 with the first collation element in the pattern
    e2 = patCEI.next();

    while ((e1 = textCEI.next()) != CollationElementIterator.NULLORDER) {
        if (e1 == e2) { // if the elements match
            if (startMatch == -1)
                startMatch = textCEI.getOffset();
            e2 = patCEI.next(); // increment to the next element
            if (e2 == CollationElementIterator.NULLORDER)
                break;
        } else { // elements do not match
            if (startMatch != -1) {
                patCEI.reset();
                e2 = patCEI.next();
                startMatch = -1;
            }
        }
    }
    return startMatch;
}

From source 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();
        }/*from   ww w . j  av  a  2s  . com*/
        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;
            }

        }
    }

}