Search in sources :

Example 31 with Collator

use of java.text.Collator in project lucene-solr by apache.

the class CollationField method setup.

/**
   * Setup the field according to the provided parameters
   */
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);
}
Also used : CollationKeyAnalyzer(org.apache.lucene.collation.CollationKeyAnalyzer) SolrException(org.apache.solr.common.SolrException) Collator(java.text.Collator) RuleBasedCollator(java.text.RuleBasedCollator)

Example 32 with Collator

use of java.text.Collator in project lucene-solr by apache.

the class TestCollationDocValuesField method testRanges.

public void testRanges() throws Exception {
    Directory dir = newDirectory();
    RandomIndexWriter iw = new RandomIndexWriter(random(), dir);
    Document doc = new Document();
    Field field = newField("field", "", StringField.TYPE_STORED);
    // uses -Dtests.locale
    Collator collator = Collator.getInstance(Locale.getDefault());
    if (random().nextBoolean()) {
        collator.setStrength(Collator.PRIMARY);
    }
    CollationDocValuesField collationField = new CollationDocValuesField("collated", collator);
    doc.add(field);
    doc.add(collationField);
    int numDocs = atLeast(500);
    for (int i = 0; i < numDocs; i++) {
        String value = TestUtil.randomSimpleString(random());
        field.setStringValue(value);
        collationField.setStringValue(value);
        iw.addDocument(doc);
    }
    IndexReader ir = iw.getReader();
    iw.close();
    IndexSearcher is = newSearcher(ir);
    int numChecks = atLeast(100);
    try {
        for (int i = 0; i < numChecks; i++) {
            String start = TestUtil.randomSimpleString(random());
            String end = TestUtil.randomSimpleString(random());
            BytesRef lowerVal = new BytesRef(collator.getCollationKey(start).toByteArray());
            BytesRef upperVal = new BytesRef(collator.getCollationKey(end).toByteArray());
            doTestRanges(is, start, end, lowerVal, upperVal, collator);
        }
    } finally {
        ir.close();
        dir.close();
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) StringField(org.apache.lucene.document.StringField) Field(org.apache.lucene.document.Field) SortField(org.apache.lucene.search.SortField) IndexReader(org.apache.lucene.index.IndexReader) Document(org.apache.lucene.document.Document) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) BytesRef(org.apache.lucene.util.BytesRef) Directory(org.apache.lucene.store.Directory) Collator(java.text.Collator)

Example 33 with Collator

use of java.text.Collator in project lucene-solr by apache.

the class TestCollationKeyAnalyzer method testThreadSafe.

public void testThreadSafe() throws Exception {
    int iters = 20 * RANDOM_MULTIPLIER;
    for (int i = 0; i < iters; i++) {
        Collator collator = Collator.getInstance(Locale.GERMAN);
        collator.setStrength(Collator.PRIMARY);
        Analyzer analyzer = new CollationKeyAnalyzer(collator);
        assertThreadSafe(analyzer);
        analyzer.close();
    }
}
Also used : Analyzer(org.apache.lucene.analysis.Analyzer) Collator(java.text.Collator)

Example 34 with Collator

use of java.text.Collator in project MonjaDB by Kanatoko.

the class MCollectionList method onTableColumnSelect.

//--------------------------------------------------------------------------------
private void onTableColumnSelect(TableColumn column) {
    final String columnName = column.getText();
    //sort order
    if (sortOrder == 1) {
        sortOrder = -1;
    } else {
        sortOrder = 1;
    }
    final int _sortOrder = sortOrder;
    Comparator c = new Comparator() {

        /**************/
        public int compare(Object o1, Object o2) {
            Map map1 = (Map) o1;
            Map map2 = (Map) o2;
            Object value1 = map1.get(columnName);
            Object value2 = map2.get(columnName);
            if (value1 == null || value2 == null) {
                return 0;
            }
            if ((value1 instanceof Integer || value1 instanceof Double) && (value2 instanceof Integer || value2 instanceof Double)) {
                double double1 = toDouble(value1);
                double double2 = toDouble(value2);
                if (double1 > double2) {
                    return 1 * _sortOrder;
                } else if (double1 == double2) {
                    return 0;
                } else {
                    return -1 * _sortOrder;
                }
            }
            String str1 = value1.toString();
            String str2 = value2.toString();
            Collator collator = Collator.getInstance(Locale.getDefault());
            return collator.compare(str1, str2) * _sortOrder;
        }

        public boolean equals(Object o1, Object o2) {
            return o1.equals(o2);
        }
    };
    /***************/
    Collections.sort(statsList, c);
    drawTable(statsList);
}
Also used : DBObject(com.mongodb.DBObject) Collator(java.text.Collator)

Example 35 with Collator

use of java.text.Collator in project MonjaDB by Kanatoko.

the class MDBList method onTableColumnSelect.

//--------------------------------------------------------------------------------
private void onTableColumnSelect(TableColumn column) {
    final String columnName = column.getText();
    //sort order
    if (sortOrder == 1) {
        sortOrder = -1;
    } else {
        sortOrder = 1;
    }
    final int _sortOrder = sortOrder;
    Comparator c = new Comparator() {

        /**************/
        public int compare(Object o1, Object o2) {
            Map map1 = (Map) o1;
            Map map2 = (Map) o2;
            Object value1 = map1.get(columnName);
            Object value2 = map2.get(columnName);
            debug(value1.getClass());
            debug(value2.getClass());
            if (value1 == null || value2 == null) {
                return 0;
            }
            if ((value1 instanceof Integer || value1 instanceof Double) && (value2 instanceof Integer || value2 instanceof Double)) {
                double double1 = toDouble(value1);
                double double2 = toDouble(value2);
                if (double1 > double2) {
                    return 1 * _sortOrder;
                } else if (double1 == double2) {
                    return 0;
                } else {
                    return -1 * _sortOrder;
                }
            }
            String str1 = value1.toString();
            String str2 = value2.toString();
            Collator collator = Collator.getInstance(Locale.getDefault());
            return collator.compare(str1, str2) * _sortOrder;
        }

        public boolean equals(Object o1, Object o2) {
            return o1.equals(o2);
        }
    };
    /***************/
    Collections.sort(statsList, c);
    drawTable(statsList);
}
Also used : DBObject(com.mongodb.DBObject) Collator(java.text.Collator)

Aggregations

Collator (java.text.Collator)70 ArrayList (java.util.ArrayList)14 RuleBasedCollator (java.text.RuleBasedCollator)8 HashMap (java.util.HashMap)7 Comparator (java.util.Comparator)5 List (java.util.List)5 GraphObject (com.facebook.model.GraphObject)4 Context (android.content.Context)3 InputMethodInfo (android.view.inputmethod.InputMethodInfo)3 HashSet (java.util.HashSet)3 LinkedHashMap (java.util.LinkedHashMap)3 Locale (java.util.Locale)3 Entry (java.util.Map.Entry)3 SharedPreferences (android.content.SharedPreferences)2 ApplicationInfo (android.content.pm.ApplicationInfo)2 PackageInfo (android.content.pm.PackageInfo)2 DBObject (com.mongodb.DBObject)2 SMSException (com.sun.identity.sm.SMSException)2 ServiceSchema (com.sun.identity.sm.ServiceSchema)2 AutoPilot (com.ximpleware.AutoPilot)2