下面列出了java.text.Collator#compare ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private static int compareGraphObjects(GraphObject a, GraphObject b, Collection<String> sortFields,
Collator collator) {
for (String sortField : sortFields) {
String sa = (String) a.getProperty(sortField);
String sb = (String) b.getProperty(sortField);
if (sa != null && sb != null) {
int result = collator.compare(sa, sb);
if (result != 0) {
return result;
}
} else if (!(sa == null && sb == null)) {
return (sa == null) ? -1 : 1;
}
}
return 0;
}
@Override
public int compareTo(TempBonusFacadeImpl o)
{
final Collator collator = Collator.getInstance();
// Check sort keys first
String key1 = this.getOriginObj().get(StringKey.SORT_KEY);
if (key1 == null)
{
key1 = this.getOriginObj().getDisplayName();
}
String key2 = o.getOriginObj().get(StringKey.SORT_KEY);
if (key2 == null)
{
key2 = o.getOriginObj().getDisplayName();
}
if (!key1.equals(key2))
{
return collator.compare(key1, key2);
}
return collator.compare(this.getOriginObj().getDisplayName(), o.getOriginObj().getDisplayName());
}
private static int compareGraphObjects(GraphObject a, GraphObject b, Collection<String> sortFields,
Collator collator) {
for (String sortField : sortFields) {
String sa = (String) a.getProperty(sortField);
String sb = (String) b.getProperty(sortField);
if (sa != null && sb != null) {
int result = collator.compare(sa, sb);
if (result != 0) {
return result;
}
} else if (!(sa == null && sb == null)) {
return (sa == null) ? -1 : 1;
}
}
return 0;
}
private static int compareGraphObjects(GraphObject a, GraphObject b, Collection<String> sortFields,
Collator collator) {
for (String sortField : sortFields) {
String sa = (String) a.getProperty(sortField);
String sb = (String) b.getProperty(sortField);
if (sa != null && sb != null) {
int result = collator.compare(sa, sb);
if (result != 0) {
return result;
}
} else if (!(sa == null && sb == null)) {
return (sa == null) ? -1 : 1;
}
}
return 0;
}
public static final Comparator<AppInfo> getAppNameComparator() {
final Collator collator = Collator.getInstance();
return new Comparator<AppInfo>() {
public final int compare(AppInfo a, AppInfo b) {
if (a.user.equals(b.user)) {
int result = collator.compare(a.title.toString().trim(),
b.title.toString().trim());
if (result == 0) {
result = a.componentName.compareTo(b.componentName);
}
return result;
} else {
// TODO Need to figure out rules for sorting
// profiles, this puts work second.
return a.user.toString().compareTo(b.user.toString());
}
}
};
}
/**
* 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));
}
/**
* 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 double 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 collator.compare(str, JSType.toString(that));
}
/**
* Check whether the JVM suffers from this bug:
* http://bugs.sun.com/view_bug.do?bug_id=4804273
* If it does, the tests that use Swedish locale will fail.
*
* @return true if the bug is present, false otherwise
*/
private static boolean hasBuggySwedishLocale() {
Collator c = Collator.getInstance(new Locale("sv"));
if (c.compare("aa", "ab") < 0) {
// OK, aa should be less than ab with Swedish collation
return false;
} else {
// this is a bug
return true;
}
}
@Override
public int compareTo(ConditionDTO that) {
// including the id in the comparison so that the TreeSet that this
// is stored in does not discard any entries just because their expressions
// are equal
final Collator collator = Collator.getInstance(Locale.US);
final String thisCmpStr = getExpression() + "_" + getId();
final String thatCmpStr = that.getExpression() + "_" + that.getId();
return collator.compare(thisCmpStr, thatCmpStr);
}
@Override
public int compare(Object o1, Object o2)
{
String key1 = getSortKey(o1);
String key2 = getSortKey(o2);
final Collator collator = Collator.getInstance();
if (!key1.equals(key2))
{
return collator.compare(key1, key2);
}
return collator.compare(String.valueOf(o1), String.valueOf(o2));
}
/**
* Gets a comparator for comparing state entry keys.
*
* @return comparator for comparing state entry keys
*/
public static Comparator<String> getKeyComparator() {
final Collator collator = Collator.getInstance(Locale.US);
return new Comparator<String>() {
@Override
public int compare(String s1, String s2) {
return collator.compare(s1, s2);
}
};
}
/**
* 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 double 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 collator.compare(str, JSType.toString(that));
}
/**
* 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));
}
@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;
}
public int comparerLocalSensitive(String s1, String s2) {
Collator c = Collator.getInstance();
c.setStrength(Collator.PRIMARY);
return c.compare(s1, s2);
}
private static int compareStrings(boolean caseSensitive, String s1, String s2) {
Collator collator = Collator.getInstance();
collator.setStrength(caseSensitive ? Collator.TERTIARY : Collator.SECONDARY);
return collator.compare(s1, s2);
}
@Override
public int compareTo(@NotNull TwigPath twigPath) {
Collator collator = Collator.getInstance();
collator.setStrength(Collator.SECONDARY);
return collator.compare(this.getNamespace(), twigPath.getNamespace());
}
private PagingResults<SiteInfo> getFavouriteSites(String userName, PagingRequest pagingRequest)
{
final Collator collator = Collator.getInstance();
final Set<SiteInfo> sortedFavouriteSites = new TreeSet<SiteInfo>(new Comparator<SiteInfo>()
{
@Override
public int compare(SiteInfo o1, SiteInfo o2)
{
return collator.compare(o1.getTitle(), o2.getTitle());
}
});
Map<String, Serializable> prefs = preferenceService.getPreferences(userName, FAVOURITE_SITES_PREFIX);
for(Entry<String, Serializable> entry : prefs.entrySet())
{
boolean isFavourite = false;
Serializable s = entry.getValue();
if(s instanceof Boolean)
{
isFavourite = (Boolean)s;
}
if(isFavourite)
{
String siteShortName = entry.getKey().substring(FAVOURITE_SITES_PREFIX_LENGTH).replace(".favourited", "");
SiteInfo siteInfo = siteService.getSite(siteShortName);
if(siteInfo != null)
{
sortedFavouriteSites.add(siteInfo);
}
}
}
int totalSize = sortedFavouriteSites.size();
final PageDetails pageDetails = PageDetails.getPageDetails(pagingRequest, totalSize);
final List<SiteInfo> page = new ArrayList<SiteInfo>(pageDetails.getPageSize());
Iterator<SiteInfo> it = sortedFavouriteSites.iterator();
for(int counter = 0; counter < pageDetails.getEnd() && it.hasNext(); counter++)
{
SiteInfo favouriteSite = it.next();
if(counter < pageDetails.getSkipCount())
{
continue;
}
if(counter > pageDetails.getEnd() - 1)
{
break;
}
page.add(favouriteSite);
}
return new PagingResults<SiteInfo>()
{
@Override
public List<SiteInfo> getPage()
{
return page;
}
@Override
public boolean hasMoreItems()
{
return pageDetails.hasMoreItems();
}
@Override
public Pair<Integer, Integer> getTotalResultCount()
{
Integer total = Integer.valueOf(sortedFavouriteSites.size());
return new Pair<Integer, Integer>(total, total);
}
@Override
public String getQueryExecutionId()
{
return null;
}
};
}
public int comparerLocalSensitive(String s1, String s2) {
Collator c = Collator.getInstance();
c.setStrength(Collator.PRIMARY);
return c.compare(s1, s2);
}
@Override
public int compare(Track o1, Track o2) {
Collator comp = java.text.Collator.getInstance();
return comp.compare(o1.toString(), o2.toString());
}