Example usage for java.text Collator SECONDARY

List of usage examples for java.text Collator SECONDARY

Introduction

In this page you can find the example usage for java.text Collator SECONDARY.

Prototype

int SECONDARY

To view the source code for java.text Collator SECONDARY.

Click Source Link

Document

Collator strength value.

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  www . j a v a  2s . c  om*/
    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:org.nuxeo.ecm.platform.ui.web.directory.DirectorySelectItemComparator.java

public DirectorySelectItemComparator(String ordering, Boolean caseSensitive, Locale locale) {
    this.ordering = StringUtils.split(ordering, ",");
    this.caseSensitive = caseSensitive;
    if (locale == null) {
        FacesContext context = FacesContext.getCurrentInstance();
        this.locale = context.getViewRoot().getLocale();
    } else {/*from w w  w.ja v a  2s  . c o  m*/
        this.locale = locale;
    }
    collator = Collator.getInstance(this.locale);
    if (Boolean.TRUE.equals(this.caseSensitive)) {
        collator.setStrength(Collator.TERTIARY); // TERTIARY will make a
                                                 // difference between 'a'
                                                 // and 'A'
    } else {
        collator.setStrength(Collator.SECONDARY);
    }
}

From source file:org.nuxeo.ecm.platform.ui.web.directory.SelectItemComparator.java

public SelectItemComparator(String ordering, Boolean caseSentitive, Locale locale) {
    this.ordering = StringUtils.split(ordering, ",");
    this.caseSensitive = caseSentitive;
    if (locale == null) {
        FacesContext context = FacesContext.getCurrentInstance();
        this.locale = context.getViewRoot().getLocale();
    } else {/* w w  w .java  2 s  .  c om*/
        this.locale = locale;
    }
    collator = Collator.getInstance(this.locale);
    if (Boolean.TRUE.equals(this.caseSensitive)) {
        collator.setStrength(Collator.TERTIARY); // TERTIARY will make a
                                                 // difference between 'a'
                                                 // and 'A'
    } else {
        collator.setStrength(Collator.SECONDARY);
    }
}

From source file:org.nuxeo.ecm.platform.ui.web.component.SelectItemComparator.java

public SelectItemComparator(String ordering, Boolean caseSentitive, Locale locale) {
    this.ordering = StringUtils.split(ordering, ",");
    caseSensitive = caseSentitive;// www .  j av a 2s .co m
    if (locale == null) {
        FacesContext context = FacesContext.getCurrentInstance();
        this.locale = context.getViewRoot().getLocale();
    } else {
        this.locale = locale;
    }
    collator = Collator.getInstance(this.locale);
    if (Boolean.TRUE.equals(caseSensitive)) {
        collator.setStrength(Collator.TERTIARY); // TERTIARY will make a
                                                 // difference between 'a'
                                                 // and 'A'
    } else {
        collator.setStrength(Collator.SECONDARY);
    }
}

From source file:StringComparable.java

public int compareTo(Object o) {
    final String pattern = ((StringComparable) o).toString();
    if (m_text.equals(pattern)) {//Code-point equals 
        return 0;
    }//from  w  w w  .j  a  v  a 2  s  .  c o m
    final int savedStrength = m_collator.getStrength();
    int comp = 0;
    // Is there difference more significant than case-order?     
    if (((savedStrength == Collator.PRIMARY) || (savedStrength == Collator.SECONDARY))) {
        comp = m_collator.compare(m_text, pattern);
    } else {// more than SECONDARY
        m_collator.setStrength(Collator.SECONDARY);
        comp = m_collator.compare(m_text, pattern);
        m_collator.setStrength(savedStrength);
    }
    if (comp != 0) {//Difference more significant than case-order 
        return comp;
    }

    // No difference more significant than case-order.     
    // Find case difference
    comp = getCaseDiff(m_text, pattern);
    if (comp != 0) {
        return comp;
    } else {// No case differences. Less significant difference could exist 
        return m_collator.compare(m_text, pattern);
    }
}

From source file:org.alfresco.web.data.Sort.java

/**
 * Constructor//  w  w w. j ava2  s  . c o m
 * 
 * @param data             a the List of String[] data to sort
 * @param column           the column getter method to use on the row to sort
 * @param bForward         true for a forward sort, false for a reverse sort
 * @param mode             sort mode to use (see IDataContainer constants)
 */
public Sort(List data, String column, boolean bForward, String mode) {
    this.data = data;
    this.column = column;
    this.bForward = bForward;
    this.sortMode = mode;

    if (this.data.size() != 0) {
        // setup the Collator for our Locale
        Collator collator = Collator.getInstance(Locale.getDefault());

        // set the strength according to the sort mode
        if (mode.equals(IDataContainer.SORT_CASEINSENSITIVE)) {
            collator.setStrength(Collator.SECONDARY);
        } else {
            collator.setStrength(Collator.IDENTICAL);
        }

        this.keys = buildCollationKeys(collator);
    }
}

From source file:nz.ac.otago.psyanlab.common.designer.util.LongSparseArrayAdapter.java

private Long[] sortKeys(Context context, LongSparseArray<T> items) {
    Locale locale = context.getResources().getConfiguration().locale;
    final Collator collator = Collator.getInstance(locale);
    collator.setStrength(Collator.SECONDARY);

    SortedSet<Long> sortedKeys = new TreeSet<Long>(new Comparator<Long>() {
        @Override//from ww  w.  ja v  a2s . c  o m
        public int compare(Long lhs, Long rhs) {
            return collator.compare(mItems.get(lhs).toString(), mItems.get(rhs).toString());
        }
    });

    for (int i = 0; i < items.size(); i++) {
        sortedKeys.add(items.keyAt(i));
    }

    return sortedKeys.toArray(new Long[sortedKeys.size()]);
}

From source file:org.apache.solr.analysis.CollationKeyFilterFactory.java

public void inform(ResourceLoader loader) {
    String custom = args.get("custom");
    String language = args.get("language");
    String country = args.get("country");
    String variant = args.get("variant");
    String strength = args.get("strength");
    String decomposition = args.get("decomposition");

    if (custom == null && language == null)
        throw new SolrException(ErrorCode.SERVER_ERROR, "Either custom or language is required.");

    if (custom != null && (language != null || country != null || variant != null))
        throw new SolrException(ErrorCode.SERVER_ERROR, "Cannot specify both language and custom. "
                + "To tailor rules for a built-in language, see the javadocs for RuleBasedCollator. "
                + "Then save the entire customized ruleset to a file, and use with the custom parameter");

    if (language != null) {
        // create from a system collator, based on Locale.
        collator = createFromLocale(language, country, variant);
    } else {/*from   w  w  w  . ja  va2 s.  co m*/
        // create from a custom ruleset
        collator = createFromRules(custom, loader);
    }

    // set the strength flag, otherwise it will be the default.
    if (strength != null) {
        if (strength.equalsIgnoreCase("primary"))
            collator.setStrength(Collator.PRIMARY);
        else if (strength.equalsIgnoreCase("secondary"))
            collator.setStrength(Collator.SECONDARY);
        else if (strength.equalsIgnoreCase("tertiary"))
            collator.setStrength(Collator.TERTIARY);
        else if (strength.equalsIgnoreCase("identical"))
            collator.setStrength(Collator.IDENTICAL);
        else
            throw new SolrException(ErrorCode.SERVER_ERROR, "Invalid strength: " + strength);
    }

    // set the decomposition flag, otherwise it will be the default.
    if (decomposition != null) {
        if (decomposition.equalsIgnoreCase("no"))
            collator.setDecomposition(Collator.NO_DECOMPOSITION);
        else if (decomposition.equalsIgnoreCase("canonical"))
            collator.setDecomposition(Collator.CANONICAL_DECOMPOSITION);
        else if (decomposition.equalsIgnoreCase("full"))
            collator.setDecomposition(Collator.FULL_DECOMPOSITION);
        else
            throw new SolrException(ErrorCode.SERVER_ERROR, "Invalid decomposition: " + decomposition);
    }
}

From source file:org.apache.solr.schema.CollationField.java

/**
 * Setup the field according to the provided parameters
 *///from w ww . j a v a2 s  .  com
private void setup(ResourceLoader loader, Map<String, String> args) {
    String custom = args.remove("custom");
    String language = args.remove("language");
    String country = args.remove("country");
    String variant = args.remove("variant");
    String strength = args.remove("strength");
    String decomposition = args.remove("decomposition");

    final Collator collator;

    if (custom == null && language == null)
        throw new SolrException(ErrorCode.SERVER_ERROR, "Either custom or language is required.");

    if (custom != null && (language != null || country != null || variant != null))
        throw new SolrException(ErrorCode.SERVER_ERROR, "Cannot specify both language and custom. "
                + "To tailor rules for a built-in language, see the javadocs for RuleBasedCollator. "
                + "Then save the entire customized ruleset to a file, and use with the custom parameter");

    if (language != null) {
        // create from a system collator, based on Locale.
        collator = createFromLocale(language, country, variant);
    } else {
        // create from a custom ruleset
        collator = createFromRules(custom, loader);
    }

    // set the strength flag, otherwise it will be the default.
    if (strength != null) {
        if (strength.equalsIgnoreCase("primary"))
            collator.setStrength(Collator.PRIMARY);
        else if (strength.equalsIgnoreCase("secondary"))
            collator.setStrength(Collator.SECONDARY);
        else if (strength.equalsIgnoreCase("tertiary"))
            collator.setStrength(Collator.TERTIARY);
        else if (strength.equalsIgnoreCase("identical"))
            collator.setStrength(Collator.IDENTICAL);
        else
            throw new SolrException(ErrorCode.SERVER_ERROR, "Invalid strength: " + strength);
    }

    // set the decomposition flag, otherwise it will be the default.
    if (decomposition != null) {
        if (decomposition.equalsIgnoreCase("no"))
            collator.setDecomposition(Collator.NO_DECOMPOSITION);
        else if (decomposition.equalsIgnoreCase("canonical"))
            collator.setDecomposition(Collator.CANONICAL_DECOMPOSITION);
        else if (decomposition.equalsIgnoreCase("full"))
            collator.setDecomposition(Collator.FULL_DECOMPOSITION);
        else
            throw new SolrException(ErrorCode.SERVER_ERROR, "Invalid decomposition: " + decomposition);
    }
    analyzer = new CollationKeyAnalyzer(collator);
}

From source file:org.apache.phoenix.expression.function.CollationKeyFunctionTest.java

@Test
public void testCollationKeyBytesForSecondaryStrength() throws Exception {
    // "a" and "A" are considered equivalent but not ""
    testCollationKeysEqual(new String[] { "a", "A" }, "en", Boolean.FALSE, Collator.SECONDARY, null);
    testSortOrderNoEquals(new String[] { "b", "a", "" }, "en", Boolean.FALSE, Collator.SECONDARY, null,
            new Integer[] { 1, 2, 0 });
}