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);
}
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();
}
}
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();
}
}
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);
}
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);
}
Aggregations