下面列出了java.text.Collator#getInstance ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Order projects by display name.
*/
public static Comparator<Project> projectDisplayNameComparator() {
return new Comparator<Project>() {
private final Collator LOC_COLLATOR = Collator.getInstance();
public int compare(Project o1, Project o2) {
ProjectInformation i1 = ProjectUtils.getInformation(o1);
ProjectInformation i2 = ProjectUtils.getInformation(o2);
int result = LOC_COLLATOR.compare(i1.getDisplayName(), i2.getDisplayName());
if (result != 0) {
return result;
} else {
result = i1.getName().compareTo(i2.getName());
if (result != 0) {
return result;
} else {
return System.identityHashCode(o1) - System.identityHashCode(o2);
}
}
}
};
}
/**
* Finds the names of callable targets in an Ant script.
* @param script Ant script to inspect
* @return list of target names, sorted (by locale)
* @throws IOException if the script cannot be inspected
*/
public static List<String> getCallableTargetNames(FileObject script) throws IOException {
AntProjectCookie apc = antProjectCookieFor(script);
Set<TargetLister.Target> allTargets = TargetLister.getTargets(apc);
SortedSet<String> targetNames = new TreeSet<String>(Collator.getInstance());
for (TargetLister.Target target : allTargets) {
if (target.isOverridden()) {
// Cannot call it directly.
continue;
}
if (target.isInternal()) {
// Should not be called from outside.
continue;
}
targetNames.add(target.getName());
}
return new ArrayList<String>(targetNames);
}
public AnonymousSubmissionComparator() {
try {
collator = new RuleBasedCollator(((RuleBasedCollator) Collator.getInstance()).getRules().replaceAll("<'\u005f'", "<' '<'\u005f'"));
} catch (ParseException e) {
// error with init RuleBasedCollator with rules
// use the default Collator
collator = Collator.getInstance();
log.warn("{} AssignmentComparator cannot init RuleBasedCollator. Will use the default Collator instead. {}", this, e);
}
}
/**
* ECMA 15.5.4.9 String.prototype.localeCompare (that)
* @param self self reference
* @param that comparison object
* @return result of locale sensitive comparison operation between {@code self} and {@code that}
*/
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static Object localeCompare(final Object self, final Object that) {
final String str = checkObjectToString(self);
final Collator collator = Collator.getInstance(Global.getEnv()._locale);
collator.setStrength(Collator.IDENTICAL);
collator.setDecomposition(Collator.CANONICAL_DECOMPOSITION);
return (double)collator.compare(str, JSType.toString(that));
}
private DocCollator(Locale locale, int strength) {
instance = Collator.getInstance(locale);
instance.setStrength(strength);
keys = new LinkedHashMap<String, CollationKey>(MAX_SIZE + 1, 0.75f, true) {
private static final long serialVersionUID = 1L;
@Override
protected boolean removeEldestEntry(Entry<String, CollationKey> eldest) {
return size() > MAX_SIZE;
}
};
}
public AbstractPortletRunController(WindowControl wControl, UserRequest ureq, Translator trans, String portletName) {
super(ureq, wControl, trans);
collator = Collator.getInstance();
this.portletName = portletName;
this.guiPreferences = ureq.getUserSession().getGuiPreferences();
this.identity = ureq.getIdentity();
this.locale = ureq.getLocale();
}
@Override
public int compareTo( DocumentGroupImpl o ) {
Collator collator = Collator.getInstance();
int res = collator.compare( displayName, o.displayName );
if( 0 == res )
res = collator.compare( name, o.name );
return res;
}
@Override
protected Comparator<AdditionalInfoAndIInfo> getItemsComparator() {
return new Comparator<AdditionalInfoAndIInfo>() {
/*
* (non-Javadoc)
*
* @see java.util.Comparator#compare(java.lang.Object,
* java.lang.Object)
*/
@Override
public int compare(AdditionalInfoAndIInfo resource1, AdditionalInfoAndIInfo resource2) {
Collator collator = Collator.getInstance();
String s1 = resource1.info.getName();
String s2 = resource2.info.getName();
int comparability = collator.compare(s1, s2);
//same name
if (comparability == 0) {
String p1 = resource1.info.getDeclaringModuleName();
String p2 = resource2.info.getDeclaringModuleName();
if (p1 == null && p2 == null) {
return 0;
}
if (p1 != null && p2 == null) {
return -1;
}
if (p1 == null && p2 != null) {
return 1;
}
return p1.compareTo(p2);
}
return comparability;
}
};
}
private Comparator getComparator0(int column) {
Comparator comparator = getComparator(column);
if (comparator != null) {
return comparator;
}
// This should be ok as useToString(column) should have returned
// true in this case.
return Collator.getInstance();
}
RegionComparator() {
collator = Collator.getInstance();
collator.setStrength(Collator.PRIMARY);
}
public int comparerLocalSensitive(String s1, String s2) {
Collator c = Collator.getInstance();
c.setStrength(Collator.PRIMARY);
return c.compare(s1, s2);
}
public void run() {
/** debug: printout the test data
for (int i=0; i<sourceData_ja.length; i++){
System.out.println(i+": "+sourceData_ja[i]);
}
**/
/*
* 1. Test the backward compatibility
* note: targetData_ja.length is equal to sourceData_ja.length
*/
Collator myCollator = Collator.getInstance(Locale.JAPAN);
CollationKey[] keys = new CollationKey[sourceData_ja.length];
CollationKey[] target_keys = new CollationKey[targetData_ja.length];
for (int i=0; i<sourceData_ja.length; i++){
keys[i] = myCollator.getCollationKey(sourceData_ja[i]);
target_keys[i] = myCollator.getCollationKey(targetData_ja[i]); //used later
}
/* Sort the string using CollationKey */
InsertionSort(keys);
/** debug: printout the result after sort
System.out.println("--- After Sorting ---");
for (int i=0; i<sourceData_ja.length; i++){
System.out.println(i+" :"+keys[i].getSourceString());
}
**/
/*
* Compare the result using equals method and getSourceString method.
*/
boolean pass = true;
for (int i=0; i<sourceData_ja.length; i++){
/* Comparing using String.equals: in order to use getStringSource() */
if (! targetData_ja[i].equals(keys[i].getSourceString())){
throw new RuntimeException("FAILED: CollationKeyTest backward compatibility "
+"while comparing" +targetData_ja[i]+" vs "
+keys[i].getSourceString());
}
/* Comparing using CollaionKey.equals: in order to use equals() */
if (! target_keys[i].equals(keys[i])){
throw new RuntimeException("FAILED: CollationKeyTest backward compatibility."
+" Using CollationKey.equals " +targetData_ja[i]
+" vs " +keys[i].getSourceString());
}
/* Comparing using CollaionKey.hashCode(): in order to use hashCode() */
if (target_keys[i].hashCode() != keys[i].hashCode()){
throw new RuntimeException("FAILED: CollationKeyTest backward compatibility."
+" Using CollationKey.hashCode " +targetData_ja[i]
+" vs " +keys[i].getSourceString());
}
/* Comparing using CollaionKey.toByteArray(): in order to use toByteArray() */
byte[] target_bytes = target_keys[i].toByteArray();
byte[] source_bytes = keys[i].toByteArray();
for (int j=0; j<target_bytes.length; j++){
Byte targetByte = new Byte(target_bytes[j]);
Byte sourceByte = new Byte(source_bytes[j]);
if (targetByte.compareTo(sourceByte)!=0){
throw new RuntimeException("FAILED: CollationKeyTest backward "
+"compatibility. Using Byte.compareTo from CollationKey.toByteArray "
+targetData_ja[i]
+" vs " +keys[i].getSourceString());
}
}
}
testSubclassMethods();
testConstructor();
}
/********************************************************
*********************************************************/
public static void main (String[] args) {
Locale reservedLocale = Locale.getDefault();
try{
int errors=0;
Locale loc = new Locale ("th", "TH"); // Thai
Locale.setDefault (loc);
Collator col = Collator.getInstance ();
/*
* The original data "data" are the data to be sorted provided by the submitter of the CR.
* It's in correct order in accord with thai collation in CLDR 1.9. If we use old Java without this fix,
* the output order will be incorrect. Correct order will be turned into incorrect order.
* If fix is there, "data" after sorting will be unchanged, same as "sortedData". If fix is lost (regression),
* "data" after sorting will be changed, not as "sortedData".(not correct anymore)
* The submitter of the CR also gives a expected "sortedData" in the CR, but it's in accord with collation in CLDR 1.4.
* His data to be sorted are actually well sorted in accord with CLDR 1.9.
*/
String[] data = {"\u0e01", "\u0e01\u0e2f", "\u0e01\u0e46", "\u0e01\u0e4f", "\u0e01\u0e5a", "\u0e01\u0e5b", "\u0e01\u0e4e", "\u0e01\u0e4c", "\u0e01\u0e48", "\u0e01\u0e01", "\u0e01\u0e4b\u0e01", "\u0e01\u0e4d", "\u0e01\u0e30", "\u0e01\u0e31\u0e01", "\u0e01\u0e32", "\u0e01\u0e33", "\u0e01\u0e34", "\u0e01\u0e35", "\u0e01\u0e36", "\u0e01\u0e37", "\u0e01\u0e38", "\u0e01\u0e39", "\u0e40\u0e01", "\u0e40\u0e01\u0e48", "\u0e40\u0e01\u0e49", "\u0e40\u0e01\u0e4b", "\u0e41\u0e01", "\u0e42\u0e01", "\u0e43\u0e01", "\u0e44\u0e01", "\u0e01\u0e3a", "\u0e24\u0e32", "\u0e24\u0e45", "\u0e40\u0e25", "\u0e44\u0e26"};
String[] sortedData = {"\u0e01", "\u0e01\u0e2f", "\u0e01\u0e46", "\u0e01\u0e4f", "\u0e01\u0e5a", "\u0e01\u0e5b", "\u0e01\u0e4e", "\u0e01\u0e4c", "\u0e01\u0e48", "\u0e01\u0e01", "\u0e01\u0e4b\u0e01", "\u0e01\u0e4d", "\u0e01\u0e30", "\u0e01\u0e31\u0e01", "\u0e01\u0e32", "\u0e01\u0e33", "\u0e01\u0e34", "\u0e01\u0e35", "\u0e01\u0e36", "\u0e01\u0e37", "\u0e01\u0e38", "\u0e01\u0e39", "\u0e40\u0e01", "\u0e40\u0e01\u0e48", "\u0e40\u0e01\u0e49", "\u0e40\u0e01\u0e4b", "\u0e41\u0e01", "\u0e42\u0e01", "\u0e43\u0e01", "\u0e44\u0e01", "\u0e01\u0e3a", "\u0e24\u0e32", "\u0e24\u0e45", "\u0e40\u0e25", "\u0e44\u0e26"};
Arrays.sort (data, col);
System.out.println ("Using " + loc.getDisplayName());
for (int i = 0; i < data.length; i++) {
System.out.println(data[i] + " : " + sortedData[i]);
if (sortedData[i].compareTo(data[i]) != 0) {
errors++;
}
}//end for
if (errors > 0){
StringBuffer expected = new StringBuffer(), actual = new StringBuffer();
expected.append(sortedData[0]);
actual.append(data[0]);
for (int i=1; i<data.length; i++) {
expected.append(",");
expected.append(sortedData[i]);
actual.append(",");
actual.append(data[i]);
}
String errmsg = "Error is found in collation testing in Thai\n" + "exepected order is: " + expected.toString() + "\n" + "actual order is: " + actual.toString() + "\n";
throw new RuntimeException(errmsg);
}
}finally{
// restore the reserved locale
Locale.setDefault(reservedLocale);
}
}
public MesquiteCollator (){
collator = Collator.getInstance();
pA = new Parser();
pB = new Parser();
}
/**
* @tests java.text.Collator#getInstance()
*/
public void test_getInstance() {
Collator c1 = Collator.getInstance();
Collator c2 = Collator.getInstance(Locale.getDefault());
assertTrue("Wrong locale", c1.equals(c2));
}
IssuerComparator(){
collator = Collator.getInstance();
collator.setStrength(Collator.PRIMARY);
}
@Override
public int compare(RemoteFile lhs, RemoteFile rhs) {
Collator collator = Collator.getInstance();
return collator.compare(lhs.name, rhs.name);
}
public BundleValueComparator(Locale locale) {
collator = Collator.getInstance(locale);
}
/**
* Returns the <code>Comparator</code> for the specified
* column. If a <code>Comparator</code> has not been specified using
* the <code>setComparator</code> method a <code>Comparator</code>
* will be returned based on the column class
* (<code>TableModel.getColumnClass</code>) of the specified column.
* If the column class is <code>String</code>,
* <code>Collator.getInstance</code> is returned. If the
* column class implements <code>Comparable</code> a private
* <code>Comparator</code> is returned that invokes the
* <code>compareTo</code> method. Otherwise
* <code>Collator.getInstance</code> is returned.
*
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
public Comparator<?> getComparator(int column) {
Comparator comparator = super.getComparator(column);
if (comparator != null) {
return comparator;
}
Class columnClass = getModel().getColumnClass(column);
if (columnClass == String.class) {
return Collator.getInstance();
}
if (Comparable.class.isAssignableFrom(columnClass)) {
return COMPARABLE_COMPARATOR;
}
return Collator.getInstance();
}
/**
* Convenience override of getDisplayNames(ULocale, Comparator, String) that
* uses the default collator for the locale as the comparator to
* sort the display names.
*/
public SortedMap getDisplayNames(ICUService service, ULocale locale, String matchID) {
Collator col = Collator.getInstance(locale.toLocale());
return service.getDisplayNames(locale, col, matchID);
}