com.ibm.icu.text.Collator类的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(10.4k)|赞(0)|评价(0)|浏览(218)

本文整理了Java中com.ibm.icu.text.Collator类的一些代码示例,展示了Collator类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Collator类的具体详情如下:
包路径:com.ibm.icu.text.Collator
类名称:Collator

Collator介绍

[英]java.text.Collator. usage

Collator performs locale-sensitive string comparison. A concrete subclass, RuleBasedCollator, allows customization of the collation ordering by the use of rule sets.

A Collator is thread-safe only when frozen. See #isFrozen() and Freezable.

Following the Unicode Consortium's specifications for the Unicode Collation Algorithm (UCA), there are 5 different levels of strength used in comparisons:

  • PRIMARY strength: Typically, this is used to denote differences between base characters (for example, "a" < "b"). It is the strongest difference. For example, dictionaries are divided into different sections by base character.
  • SECONDARY strength: Accents in the characters are considered secondary differences (for example, "as" < "às" < "at"). Other differences between letters can also be considered secondary differences, depending on the language. A secondary difference is ignored when there is a primary difference anywhere in the strings.
  • TERTIARY strength: Upper and lower case differences in characters are distinguished at tertiary strength (for example, "ao" < "Ao" < "aò"). In addition, a variant of a letter differs from the base form on the tertiary strength (such as "A" and "Ⓐ"). Another example is the difference between large and small Kana. A tertiary difference is ignored when there is a primary or secondary difference anywhere in the strings.
  • QUATERNARY strength: When punctuation is ignored (see Ignoring Punctuations in the User Guide) at PRIMARY to TERTIARY strength, an additional strength level can be used to distinguish words with and without punctuation (for example, "ab" < "a-b" < "aB"). This difference is ignored when there is a PRIMARY, SECONDARY or TERTIARY difference. The QUATERNARY strength should only be used if ignoring punctuation is required.
  • IDENTICAL strength: When all other strengths are equal, the IDENTICAL strength is used as a tiebreaker. The Unicode code point values of the NFD form of each string are compared, just in case there is no difference. For example, Hebrew cantellation marks are only distinguished at this strength. This strength should be used sparingly, as only code point value differences between two strings is an extremely rare occurrence. Using this strength substantially decreases the performance for both comparison and collation key generation APIs. This strength also increases the size of the collation key.
    Unlike the JDK, ICU4J's Collator deals only with 2 decomposition modes, the canonical decomposition mode and one that does not use any decomposition. The compatibility decomposition mode, java.text.Collator.FULL_DECOMPOSITION is not supported here. If the canonical decomposition mode is set, the Collator handles un-normalized text properly, producing the same results as if the text were normalized in NFD. If canonical decomposition is turned off, it is the user's responsibility to ensure that all text is already in the appropriate form before performing a comparison or before getting a CollationKey.

For more information about the collation service see the User Guide.

Examples of use

// Get the Collator for US English and set its strength to PRIMARY 
Collator usCollator = Collator.getInstance(Locale.US); 
usCollator.setStrength(Collator.PRIMARY); 
if (usCollator.compare("abc", "ABC") == 0) { 
System.out.println("Strings are equivalent"); 
} 
The following example shows how to compare two strings using the 
Collator for the default locale. 
// Compare two strings in the default locale 
Collator myCollator = Collator.getInstance(); 
myCollator.setDecomposition(NO_DECOMPOSITION); 
if (myCollator.compare("à\u0325", "a\u0325̀") != 0) { 
System.out.println("à\u0325 is not equals to a\u0325̀ without decomposition"); 
myCollator.setDecomposition(CANONICAL_DECOMPOSITION); 
if (myCollator.compare("à\u0325", "a\u0325̀") != 0) { 
System.out.println("Error: à\u0325 should be equals to a\u0325̀ with decomposition"); 
} 
else { 
System.out.println("à\u0325 is equals to a\u0325̀ with decomposition"); 
} 
} 
else { 
System.out.println("Error: à\u0325 should be not equals to a\u0325̀ without decomposition"); 
}

[中]

代码示例

代码示例来源:origin: org.eclipse/org.eclipse.ui.editors

public int compare(Object o1, Object o2) {
    if (!(o2 instanceof ListItem))
      return -1;
    if (!(o1 instanceof ListItem))
      return 1;
    String label1= ((ListItem)o1).label;
    String label2= ((ListItem)o2).label;
    return Collator.getInstance().compare(label1, label2);
  }
};

代码示例来源:origin: org.apache.lucene/lucene-analyzers-icu

/**
 * Create a new ICUCollatedTermAttributeImpl
 * @param collator Collation key generator
 */
public ICUCollatedTermAttributeImpl(Collator collator) {
 // clone the collator: see http://userguide.icu-project.org/collation/architecture
 try {
  this.collator = (Collator) collator.clone();
 } catch (CloneNotSupportedException e) {
  throw new RuntimeException(e);
 }
}

代码示例来源:origin: com.ibm.icu/icu4j-localespi

@Override
public void setDecomposition(int decompositionMode) {
  switch (decompositionMode) {
  case java.text.Collator.CANONICAL_DECOMPOSITION:
    fIcuCollator.setDecomposition(Collator.CANONICAL_DECOMPOSITION);
    break;
  case java.text.Collator.NO_DECOMPOSITION:
    fIcuCollator.setDecomposition(Collator.NO_DECOMPOSITION);
    break;
  case java.text.Collator.FULL_DECOMPOSITION:
    // Not supported by ICU.
    // This option is interpreted as IDENTICAL strength.
    fIcuCollator.setStrength(Collator.IDENTICAL);
    break;
  default:
    throw new IllegalArgumentException("Invalid decomposition mode.");
  }
}

代码示例来源:origin: org.eclipse.scout.sdk.deps/org.eclipse.ui.ide

public CollationKey getDescriptionKey() {
  if (descriptionKey == null) {
    descriptionKey = Collator.getInstance()
        .getCollationKey(description);
  }
  return descriptionKey;
}

代码示例来源:origin: org.eclipse.jdt/org.eclipse.jdt.ui

@Override
  public int compare(String[] o1, String[] o2) {
    return fCollator.compare(o1[1], o2[1]);
  }
});

代码示例来源:origin: org.eclipse.platform/org.eclipse.ui.navigator.resources

/**
 * Construct a sorter that uses the name of the resource as its sorting
 * criteria.
 *
 */
public ResourceExtensionSorter() {
  super(ResourceSorter.NAME);
  icuCollator = Collator.getInstance();
}

代码示例来源:origin: com.yahoo.vespa/container-search

public void setLocale(String locale, Strength strength) {
  this.locale = locale;
  this.strength = strength;
  ULocale uloc;
  try {
    uloc = new ULocale(locale);
  } catch (Throwable e) {
    throw new RuntimeException("ULocale("+locale+") failed with exception " + e.toString());
  }
  try {
    collator = Collator.getInstance(uloc);
    if (collator == null) {
      throw new RuntimeException("No collator available for: " + locale);
    }
  } catch (Throwable e) {
    throw new RuntimeException("Collator.getInstance(ULocale("+locale+")) failed with exception " + e.toString());
  }
  collator.setStrength(strength2Collator(strength));
  // collator.setDecomposition(Collator.CANONICAL_DECOMPOSITION);
}
public String getLocale() { return locale; }

代码示例来源:origin: io.virtdata/virtdata-lib-realer

/**
 * @return this, for chaining
 * @internal Used in UnicodeTools
 * @deprecated This API is ICU internal only.
 */
@Deprecated
public Collator setStrength2(int newStrength)
{
  setStrength(newStrength);
  return this;
}

代码示例来源:origin: org.eclipse.scout.sdk.deps/org.eclipse.ui.workbench.texteditor

@Override
  public int compare(String[] o1, String[] o2) {
    return fCollator.compare(o1[1], o2[1]);
  }
});

代码示例来源:origin: dita-ot/dita-ot

public IndexCollator(final Locale theLocale) {
  this.defaultCollator = java.text.Collator.getInstance(theLocale);
  try {
    this.icu4jCollator = com.ibm.icu.text.Collator.getInstance(theLocale);
  } catch (final NoClassDefFoundError ex) {
    System.out.println("[INFO] IBM ICU4J Collator is not found. Default Java Collator will be used");
    icuCollator = false;
  }
}

代码示例来源:origin: org.eclipse.scout.sdk.deps/org.eclipse.ui.ide

public CollationKey getResourceNameKey() {
  if (resourceNameKey == null) {
    resourceNameKey = Collator.getInstance().getCollationKey(
        resourceName);
  }
  return resourceNameKey;
}

代码示例来源:origin: com.ibm.icu/icu4j-localespi

@Override
public void setStrength(int newStrength) {
  switch (newStrength) {
  case java.text.Collator.IDENTICAL:
    fIcuCollator.setStrength(Collator.IDENTICAL);
    break;
  case java.text.Collator.PRIMARY:
    fIcuCollator.setStrength(Collator.PRIMARY);
    break;
  case java.text.Collator.SECONDARY:
    fIcuCollator.setStrength(Collator.SECONDARY);
    break;
  case java.text.Collator.TERTIARY:
    fIcuCollator.setStrength(Collator.TERTIARY);
    break;
  default:
    throw new IllegalArgumentException("Invalid strength.");
  }
}

代码示例来源:origin: org.eclipse/org.eclipse.ui.editors

public int compareTo(Object o) {
    if (!(o instanceof NavigationEnablementAction))
      return -1;
    String otherName= ((NavigationEnablementAction)o).fName;
    return Collator.getInstance().compare(fName, otherName);
  }
}

代码示例来源:origin: org.eclipse.scout.sdk.deps/org.eclipse.jdt.ui

@Override
  public int compare(String[] o1, String[] o2) {
    return fCollator.compare(o1[1], o2[1]);
  }
});

代码示例来源:origin: org.eclipse.jdt/org.eclipse.jdt.ui

public void sort() {
  Comparator<IBinding> comparator= new Comparator<IBinding>() {
    Collator collator= Collator.getInstance();
    @Override
    public int compare(IBinding b1, IBinding b2) {
      return collator.compare(b1.getName(), b2.getName());
    }
  };
  Arrays.sort(fFields, comparator);
  Arrays.sort(fMethods, comparator);
  Arrays.sort(fInheritedFields, comparator);
  Arrays.sort(fInheritedMethods, comparator);
}

代码示例来源:origin: org.elasticsearch.plugin/analysis-icu

/**
 * 
 * @param input Source token stream
 * @param collator CollationKey generator
 */
public ICUCollationKeyFilter(TokenStream input, Collator collator) {
 super(input);
 // clone the collator: see http://userguide.icu-project.org/collation/architecture
 try {
  this.collator = (Collator) collator.clone();
 } catch (CloneNotSupportedException e) {
  throw new RuntimeException(e);
 }
}

代码示例来源:origin: org.eclipse.scout.sdk.deps/org.eclipse.ui.editors

@Override
  public int compare(ListItem o1, ListItem o2) {
    String label1= o1.name;
    String label2= o2.name;
    return Collator.getInstance().compare(label1, label2);
  }
};

代码示例来源:origin: dita-ot/dita-ot

public int compare(final Object o1, final Object o2) {
  if (icuCollator) {
    return this.icu4jCollator.compare(o1, o2);
  } else {
    return this.defaultCollator.compare(o1, o2);
  }
}

代码示例来源:origin: org.eclipse.scout.sdk.deps/org.eclipse.jdt.ui

public void sort() {
  Comparator<IBinding> comparator= new Comparator<IBinding>() {
    Collator collator= Collator.getInstance();
    @Override
    public int compare(IBinding b1, IBinding b2) {
      return collator.compare(b1.getName(), b2.getName());
    }
  };
  Arrays.sort(fFields, comparator);
  Arrays.sort(fMethods, comparator);
  Arrays.sort(fInheritedFields, comparator);
  Arrays.sort(fInheritedMethods, comparator);
}

代码示例来源:origin: org.apache.lucene/lucene-icu

/**
 * 
 * @param input Source token stream
 * @param collator CollationKey generator
 */
public ICUCollationKeyFilter(TokenStream input, Collator collator) {
 super(input);
 // clone the collator: see http://userguide.icu-project.org/collation/architecture
 try {
  this.collator = (Collator) collator.clone();
 } catch (CloneNotSupportedException e) {
  throw new RuntimeException(e);
 }
}

相关文章