Java Code Examples for com.ibm.icu.text.Collator#setStrength()

The following examples show how to use com.ibm.icu.text.Collator#setStrength() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ResultSetWrapper.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
private Collator createCollator(SeriesDefinition sd )
{
	// If sort strength is ASCII(-1), then just use default compare of
	// String class to do collator, so here just return null;
	if ( sd.isSetSortStrength( ) && sd.getSortStrength( ) < 0 )
	{
		return null;
	}
	Collator c = null;
	if ( sd.getSortLocale( ) != null )
	{
		c = Collator.getInstance( new ULocale( sd.getSortLocale( ) ) );
	}
	else {
		c = Collator.getInstance( );	
	}
	
	if ( sd.isSetSortStrength( ) )
	{
		c.setStrength( sd.getSortStrength( ) );
	}
	
	return c;
}
 
Example 2
Source File: TestICUCollationKeyAnalyzer.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testThreadSafe() throws Exception {
  int iters = 20 * RANDOM_MULTIPLIER;
  for (int i = 0; i < iters; i++) {
    Locale locale = Locale.GERMAN;
    Collator collator = Collator.getInstance(locale);
    collator.setStrength(Collator.IDENTICAL);
    Analyzer a = new ICUCollationKeyAnalyzer(collator);
    assertThreadSafe(a);
    a.close();
  }
}
 
Example 3
Source File: TestICUCollationDocValuesField.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
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);
  Collator collator = Collator.getInstance(); // uses -Dtests.locale
  if (random().nextBoolean()) {
    collator.setStrength(Collator.PRIMARY);
  }
  ICUCollationDocValuesField collationField = new ICUCollationDocValuesField("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);
  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);
  }
  
  ir.close();
  dir.close();
}
 
Example 4
Source File: IcuCollationKeyAnalyzerTests.java    From elasticsearch-plugin-bundle with GNU Affero General Public License v3.0 5 votes vote down vote up
public void testThreadSafe() throws Exception {
    int iters = 20;
    for (int i = 0; i < iters; i++) {
        Locale locale = Locale.GERMAN;
        Collator collator = Collator.getInstance(locale);
        collator.setStrength(Collator.IDENTICAL);
        assertThreadSafe(Randomness.get(), new IcuCollationKeyAnalyzer(collator));
    }
}
 
Example 5
Source File: QueryExecutor.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
private Collator createCollator( ISortDefinition sd )
{
	if ( sd.getSortStrength( ) != -1 )
	{
		Collator c = Collator.getInstance( sd.getSortLocale( ) == null
						? session.getEngineContext( ).getLocale( )
								: sd.getSortLocale( ) );
		c.setStrength( sd.getSortStrength( ) );
		return c;
	}
	return null;
}
 
Example 6
Source File: ICUCollationField.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
/**
 * Setup the field according to the provided parameters
 */
private void setup(ResourceLoader loader, Map<String,String> args) {
  String custom = args.remove("custom");
  String localeID = args.remove("locale");
  String strength = args.remove("strength");
  String decomposition = args.remove("decomposition");
  
  String alternate = args.remove("alternate");
  String caseLevel = args.remove("caseLevel");
  String caseFirst = args.remove("caseFirst");
  String numeric = args.remove("numeric");
  String variableTop = args.remove("variableTop");

  if (custom == null && localeID == null)
    throw new SolrException(ErrorCode.SERVER_ERROR, "Either custom or locale is required.");
  
  if (custom != null && localeID != null)
    throw new SolrException(ErrorCode.SERVER_ERROR, "Cannot specify both locale 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");
  
  final Collator collator;
  
  if (localeID != null) { 
    // create from a system collator, based on Locale.
    collator = createFromLocale(localeID);
  } 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("quaternary"))
      collator.setStrength(Collator.QUATERNARY);
    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
      throw new SolrException(ErrorCode.SERVER_ERROR, "Invalid decomposition: " + decomposition);
  }
  
  // expert options: concrete subclasses are always a RuleBasedCollator
  RuleBasedCollator rbc = (RuleBasedCollator) collator;
  if (alternate != null) {
    if (alternate.equalsIgnoreCase("shifted")) {
      rbc.setAlternateHandlingShifted(true);
    } else if (alternate.equalsIgnoreCase("non-ignorable")) {
      rbc.setAlternateHandlingShifted(false);
    } else {
      throw new SolrException(ErrorCode.SERVER_ERROR, "Invalid alternate: " + alternate);
    }
  }
  if (caseLevel != null) {
    rbc.setCaseLevel(Boolean.parseBoolean(caseLevel));
  }
  if (caseFirst != null) {
    if (caseFirst.equalsIgnoreCase("lower")) {
      rbc.setLowerCaseFirst(true);
    } else if (caseFirst.equalsIgnoreCase("upper")) {
      rbc.setUpperCaseFirst(true);
    } else {
      throw new SolrException(ErrorCode.SERVER_ERROR, "Invalid caseFirst: " + caseFirst);
    }
  }
  if (numeric != null) {
    rbc.setNumericCollation(Boolean.parseBoolean(numeric));
  }
  if (variableTop != null) {
    rbc.setVariableTop(variableTop);
  }

  analyzer = new ICUCollationKeyAnalyzer(collator);
}
 
Example 7
Source File: ModelUtil.java    From birt with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * 
 * Performs property name sorting on a list of properties. Properties
 * returned are sorted by their (locale-specific) display name. The name for
 * sorting is assumed to be "groupName.displayName" in which "groupName" is
 * the localized name of the property group, if any; and "displayName" is
 * the localized name of the property. That is, properties without groups
 * sort by their property display names. Properties with groups sort first
 * by group name within the overall list, then by property name within the
 * group. Sorting in English ignores case.
 * <p>
 * For example, if we have the groups "G" and "R", and the properties
 * "alpha", "G.beta", "G.sigma", "iota", "R.delta", "R.epsilon" and "theta",
 * the Properties returned is assumed to be sorted into that order.
 * 
 * Sorts a list of <code>PropertyDefn</code> s by there localized name. Uses
 * <code>Collator</code> to do the comparison, sorting in English ignores
 * case.
 * 
 * @param propDefns
 *            a list that contains PropertyDefns.
 * @return the list of <code>PropertyDefn</code> s that is sorted by their
 *         display name.
 */

public static List<IPropertyDefn> sortPropertiesByLocalizedName(
		List<IPropertyDefn> propDefns )
{
	// Use the static factory method, getInstance, to obtain the appropriate
	// Collator object for the current
	// locale.

	// The Collator instance that performs locale-sensitive String
	// comparison.

	ULocale locale = ThreadResources.getLocale( );
	Collator collator = Collator.getInstance( locale );

	// Sorting in English should ignore case.
	if ( ULocale.ENGLISH.equals( locale ) )
	{

		// Set Collator strength value as PRIMARY, only PRIMARY differences
		// are considered significant during comparison. The assignment of
		// strengths to language features is locale defendant. A common
		// example is for different base letters ("a" vs "b") to be
		// considered a PRIMARY difference.

		collator.setStrength( Collator.PRIMARY );
	}

	final Map<PropertyDefn, CollationKey> keysMap = new HashMap<PropertyDefn, CollationKey>( );
	for ( int i = 0; i < propDefns.size( ); i++ )
	{
		PropertyDefn propDefn = (PropertyDefn) propDefns.get( i );

		// Transforms the String into a series of bits that can be compared
		// bitwise to other CollationKeys.
		// CollationKeys provide better performance than Collator.

		CollationKey key = collator.getCollationKey( propDefn
				.getDisplayName( ) );
		keysMap.put( propDefn, key );
	}

	Collections.sort( propDefns, new Comparator<IPropertyDefn>( ) {

		public int compare( IPropertyDefn o1, IPropertyDefn o2 )
		{
			PropertyDefn p1 = (PropertyDefn) o1;
			PropertyDefn p2 = (PropertyDefn) o2;

			CollationKey key1 = keysMap.get( p1 );
			CollationKey key2 = keysMap.get( p2 );

			// Comparing two CollationKeys returns the relative order of the
			// Strings they represent. Using CollationKeys to compare
			// Strings is generally faster than using Collator.compare.

			return key1.compareTo( key2 );
		}
	} );

	return propDefns;
}