下面列出了java.text.CollationKey#compareTo ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public int compare(ContactEntity lhs, ContactEntity rhs) {
String string1 = TextUtils.isEmpty(lhs.getRemark()) ? lhs.getUsername() : lhs.getRemark();
String string2 = TextUtils.isEmpty(rhs.getRemark()) ? rhs.getUsername() : rhs.getRemark();
char char1 = TextUtils.isEmpty(string1) ? '#' : string1.charAt(0);
char char2 = TextUtils.isEmpty(string2) ? '#' : string2.charAt(0);
CollationKey key1 = collator.getCollationKey(PinyinUtil.chatToPinyin(char1));
CollationKey key2 = collator.getCollationKey(PinyinUtil.chatToPinyin(char2));
// Comparison method violates its general contract
if (key1.getSourceString().equals(key2.getSourceString())) {
return 0;
}
if ("#".equals(key1.getSourceString())
|| "#".equals(key2.getSourceString())) {
if ("#".equals(key1.getSourceString())) {
return 1;
} else if ("#".equals(key2.getSourceString())) {
return -1;
}
}
return key1.compareTo(key2);
}
@Override
public int compare(GroupMemberEntity lhs, GroupMemberEntity rhs) {
String string1 = TextUtils.isEmpty(lhs.getNick()) ? lhs.getUsername() : lhs.getNick();
String string2 = TextUtils.isEmpty(rhs.getNick()) ? rhs.getUsername() : rhs.getNick();
char char1 = TextUtils.isEmpty(string1) ? '#' : string1.charAt(0);
char char2 = TextUtils.isEmpty(string2) ? '#' : string2.charAt(0);
CollationKey key1 = collator.getCollationKey(PinyinUtil.chatToPinyin(char1));
CollationKey key2 = collator.getCollationKey(PinyinUtil.chatToPinyin(char2));
// Comparison method violates its general contract
if (key1.getSourceString().equals(key2.getSourceString())) {
return 0;
}
if ("#".equals(key1.getSourceString())
|| "#".equals(key2.getSourceString())) {
if ("#".equals(key1.getSourceString())) {
return 1;
} else if ("#".equals(key2.getSourceString())) {
return -1;
}
}
return key1.compareTo(key2);
}
/** @see SQLChar#stringCompare(SQLChar, SQLChar) */
int stringCompare(SQLChar str1, SQLChar str2)
throws StandardException
{
CollationKey ckey1 = str1.getCollationKey();
CollationKey ckey2 = str2.getCollationKey();
/*
** By convention, nulls sort High, and null == null
*/
if (ckey1 == null || ckey2 == null)
{
if (ckey1 != null) // str2 == null
return -1;
if (ckey2 != null) // this == null
return 1;
return 0; // both == null
}
return ckey1.compareTo(ckey2);
}
/** @see SQLChar#stringCompare(SQLChar, SQLChar) */
int stringCompare(SQLChar str1, SQLChar str2)
throws StandardException
{
CollationKey ckey1 = str1.getCollationKey();
CollationKey ckey2 = str2.getCollationKey();
/*
** By convention, nulls sort High, and null == null
*/
if (ckey1 == null || ckey2 == null)
{
if (ckey1 != null) // str2 == null
return -1;
if (ckey2 != null) // this == null
return 1;
return 0; // both == null
}
return ckey1.compareTo(ckey2);
}
/** @see SQLChar#stringCompare(SQLChar, SQLChar) */
int stringCompare(SQLChar str1, SQLChar str2)
throws StandardException
{
CollationKey ckey1 = str1.getCollationKey();
CollationKey ckey2 = str2.getCollationKey();
/*
** By convention, nulls sort High, and null == null
*/
if (ckey1 == null || ckey2 == null)
{
if (ckey1 != null) // str2 == null
return -1;
if (ckey2 != null) // this == null
return 1;
return 0; // both == null
}
return ckey1.compareTo(ckey2);
}
public void TestCollationKey()
{
String source = "-abcdefghijklmnopqrstuvwxyz#&^[email protected]";
Random r = new Random(3);
int s = checkValue(r.nextInt() % source.length());
int t = checkValue(r.nextInt() % source.length());
int slen = checkValue((r.nextInt() - source.length()) % source.length());
int tlen = checkValue((r.nextInt() - source.length()) % source.length());
String subs = source.substring((s > slen ? slen : s), (s >= slen ? s : slen));
String subt = source.substring((t > tlen ? tlen : t), (t >= tlen ? t : tlen));
myCollator.setStrength(Collator.TERTIARY);
CollationKey CollationKey1 = myCollator.getCollationKey(subs);
CollationKey CollationKey2 = myCollator.getCollationKey(subt);
int result = CollationKey1.compareTo(CollationKey2); // Tertiary
int revResult = CollationKey2.compareTo(CollationKey1); // Tertiary
report(("CollationKey(" + subs + ")"), ("CollationKey(" + subt + ")"), result, revResult);
myCollator.setStrength(Collator.SECONDARY);
CollationKey1 = myCollator.getCollationKey(subs);
CollationKey2 = myCollator.getCollationKey(subt);
result = CollationKey1.compareTo(CollationKey2); // Secondary
revResult = CollationKey2.compareTo(CollationKey1); // Secondary
report(("CollationKey(" + subs + ")") , ("CollationKey(" + subt + ")"), result, revResult);
myCollator.setStrength(Collator.PRIMARY);
CollationKey1 = myCollator.getCollationKey(subs);
CollationKey2 = myCollator.getCollationKey(subt);
result = CollationKey1.compareTo(CollationKey2); // Primary
revResult = CollationKey2.compareTo(CollationKey1); // Primary
report(("CollationKey(" + subs + ")"), ("CollationKey(" + subt + ")"), result, revResult);
String addOne = subs + "\uE000";
CollationKey1 = myCollator.getCollationKey(subs);
CollationKey2 = myCollator.getCollationKey(addOne);
result = CollationKey1.compareTo(CollationKey2);
if (result != -1)
errln("CollationKey(" + subs + ")" + ".LT." + "CollationKey(" + addOne + ") Failed.");
result = CollationKey2.compareTo(CollationKey1);
if (result != 1)
errln("CollationKey(" + addOne + ")" + ".GT." + "CollationKey(" + subs + ") Failed.");
}
public void TestCollationKey()
{
String source = "-abcdefghijklmnopqrstuvwxyz#&^[email protected]";
Random r = new Random(3);
int s = checkValue(r.nextInt() % source.length());
int t = checkValue(r.nextInt() % source.length());
int slen = checkValue((r.nextInt() - source.length()) % source.length());
int tlen = checkValue((r.nextInt() - source.length()) % source.length());
String subs = source.substring((s > slen ? slen : s), (s >= slen ? s : slen));
String subt = source.substring((t > tlen ? tlen : t), (t >= tlen ? t : tlen));
myCollator.setStrength(Collator.TERTIARY);
CollationKey CollationKey1 = myCollator.getCollationKey(subs);
CollationKey CollationKey2 = myCollator.getCollationKey(subt);
int result = CollationKey1.compareTo(CollationKey2); // Tertiary
int revResult = CollationKey2.compareTo(CollationKey1); // Tertiary
report(("CollationKey(" + subs + ")"), ("CollationKey(" + subt + ")"), result, revResult);
myCollator.setStrength(Collator.SECONDARY);
CollationKey1 = myCollator.getCollationKey(subs);
CollationKey2 = myCollator.getCollationKey(subt);
result = CollationKey1.compareTo(CollationKey2); // Secondary
revResult = CollationKey2.compareTo(CollationKey1); // Secondary
report(("CollationKey(" + subs + ")") , ("CollationKey(" + subt + ")"), result, revResult);
myCollator.setStrength(Collator.PRIMARY);
CollationKey1 = myCollator.getCollationKey(subs);
CollationKey2 = myCollator.getCollationKey(subt);
result = CollationKey1.compareTo(CollationKey2); // Primary
revResult = CollationKey2.compareTo(CollationKey1); // Primary
report(("CollationKey(" + subs + ")"), ("CollationKey(" + subt + ")"), result, revResult);
String addOne = subs + "\uE000";
CollationKey1 = myCollator.getCollationKey(subs);
CollationKey2 = myCollator.getCollationKey(addOne);
result = CollationKey1.compareTo(CollationKey2);
if (result != -1)
errln("CollationKey(" + subs + ")" + ".LT." + "CollationKey(" + addOne + ") Failed.");
result = CollationKey2.compareTo(CollationKey1);
if (result != 1)
errln("CollationKey(" + addOne + ")" + ".GT." + "CollationKey(" + subs + ") Failed.");
}
public void TestCollationKey()
{
String source = "-abcdefghijklmnopqrstuvwxyz#&^[email protected]";
Random r = new Random(3);
int s = checkValue(r.nextInt() % source.length());
int t = checkValue(r.nextInt() % source.length());
int slen = checkValue((r.nextInt() - source.length()) % source.length());
int tlen = checkValue((r.nextInt() - source.length()) % source.length());
String subs = source.substring((s > slen ? slen : s), (s >= slen ? s : slen));
String subt = source.substring((t > tlen ? tlen : t), (t >= tlen ? t : tlen));
myCollator.setStrength(Collator.TERTIARY);
CollationKey CollationKey1 = myCollator.getCollationKey(subs);
CollationKey CollationKey2 = myCollator.getCollationKey(subt);
int result = CollationKey1.compareTo(CollationKey2); // Tertiary
int revResult = CollationKey2.compareTo(CollationKey1); // Tertiary
report(("CollationKey(" + subs + ")"), ("CollationKey(" + subt + ")"), result, revResult);
myCollator.setStrength(Collator.SECONDARY);
CollationKey1 = myCollator.getCollationKey(subs);
CollationKey2 = myCollator.getCollationKey(subt);
result = CollationKey1.compareTo(CollationKey2); // Secondary
revResult = CollationKey2.compareTo(CollationKey1); // Secondary
report(("CollationKey(" + subs + ")") , ("CollationKey(" + subt + ")"), result, revResult);
myCollator.setStrength(Collator.PRIMARY);
CollationKey1 = myCollator.getCollationKey(subs);
CollationKey2 = myCollator.getCollationKey(subt);
result = CollationKey1.compareTo(CollationKey2); // Primary
revResult = CollationKey2.compareTo(CollationKey1); // Primary
report(("CollationKey(" + subs + ")"), ("CollationKey(" + subt + ")"), result, revResult);
String addOne = subs + "\uE000";
CollationKey1 = myCollator.getCollationKey(subs);
CollationKey2 = myCollator.getCollationKey(addOne);
result = CollationKey1.compareTo(CollationKey2);
if (result != -1)
errln("CollationKey(" + subs + ")" + ".LT." + "CollationKey(" + addOne + ") Failed.");
result = CollationKey2.compareTo(CollationKey1);
if (result != 1)
errln("CollationKey(" + addOne + ")" + ".GT." + "CollationKey(" + subs + ") Failed.");
}
@Override
public int compare(T lhs, T rhs) {
String ostr1;
String ostr2;
String lhsName = getName(lhs);
String rhskName = getName(rhs);
if (TextUtils.isEmpty(lhsName)) {
ostr1 = PinyinUtil.getFirstChar(TextUtils.isEmpty(lhsName) ? "#" : lhsName);
} else {
ostr1 = PinyinUtil.getFirstChar(TextUtils.isEmpty(lhsName) ? "#" : lhsName);
}
if (TextUtils.isEmpty(rhskName)) {
ostr2 = PinyinUtil.getFirstChar(TextUtils.isEmpty(rhskName) ? "#" : rhskName);
} else {
ostr2 = PinyinUtil.getFirstChar(TextUtils.isEmpty(rhskName) ? "#" : rhskName);
}
CollationKey key1 = collator.getCollationKey(pinyin(ostr1.equals("") ? '#': ostr1.charAt(0)));
CollationKey key2 = collator.getCollationKey(pinyin(ostr2.equals("") ? '#': ostr2.charAt(0)));
// Comparison method violates its general contract
if(key1.getSourceString().equals(key2.getSourceString())){
return 0;
}
if ("#".equals(key1.getSourceString())
|| "#".equals(key2.getSourceString())) {
if ("#".equals(key1.getSourceString())) {
return 1;
} else if ("#".equals(key2.getSourceString())) {
return -1;
}
}
return key1.compareTo(key2);
}
@Override
public int compare(GroupMemberEntity lhs, GroupMemberEntity rhs) {
String string1 = TextUtils.isEmpty(lhs.getNick()) ? lhs.getUsername() : lhs.getNick();
String string2 = TextUtils.isEmpty(rhs.getNick()) ? rhs.getUsername() : rhs.getNick();
if (TextUtils.isEmpty(string1)) {
string1 = "#";
}
if (TextUtils.isEmpty(string2)) {
string2 = "#";
}
char char1 = string1.charAt(0);
char char2 = string2.charAt(0);
CollationKey key1 = collator.getCollationKey(PinyinUtil.chatToPinyin(char1));
CollationKey key2 = collator.getCollationKey(PinyinUtil.chatToPinyin(char2));
// Comparison method violates its general contract
if (key1.getSourceString().equals(key2.getSourceString())) {
return 0;
}
if ("#".equals(key1.getSourceString())
|| "#".equals(key2.getSourceString())) {
if ("#".equals(key1.getSourceString())) {
return 1;
} else if ("#".equals(key2.getSourceString())) {
return -1;
}
}
return key1.compareTo(key2);
}
public void TestCollationKey()
{
String source = "-abcdefghijklmnopqrstuvwxyz#&^[email protected]";
Random r = new Random(3);
int s = checkValue(r.nextInt() % source.length());
int t = checkValue(r.nextInt() % source.length());
int slen = checkValue((r.nextInt() - source.length()) % source.length());
int tlen = checkValue((r.nextInt() - source.length()) % source.length());
String subs = source.substring((s > slen ? slen : s), (s >= slen ? s : slen));
String subt = source.substring((t > tlen ? tlen : t), (t >= tlen ? t : tlen));
myCollator.setStrength(Collator.TERTIARY);
CollationKey CollationKey1 = myCollator.getCollationKey(subs);
CollationKey CollationKey2 = myCollator.getCollationKey(subt);
int result = CollationKey1.compareTo(CollationKey2); // Tertiary
int revResult = CollationKey2.compareTo(CollationKey1); // Tertiary
report(("CollationKey(" + subs + ")"), ("CollationKey(" + subt + ")"), result, revResult);
myCollator.setStrength(Collator.SECONDARY);
CollationKey1 = myCollator.getCollationKey(subs);
CollationKey2 = myCollator.getCollationKey(subt);
result = CollationKey1.compareTo(CollationKey2); // Secondary
revResult = CollationKey2.compareTo(CollationKey1); // Secondary
report(("CollationKey(" + subs + ")") , ("CollationKey(" + subt + ")"), result, revResult);
myCollator.setStrength(Collator.PRIMARY);
CollationKey1 = myCollator.getCollationKey(subs);
CollationKey2 = myCollator.getCollationKey(subt);
result = CollationKey1.compareTo(CollationKey2); // Primary
revResult = CollationKey2.compareTo(CollationKey1); // Primary
report(("CollationKey(" + subs + ")"), ("CollationKey(" + subt + ")"), result, revResult);
String addOne = subs + "\uE000";
CollationKey1 = myCollator.getCollationKey(subs);
CollationKey2 = myCollator.getCollationKey(addOne);
result = CollationKey1.compareTo(CollationKey2);
if (result != -1)
errln("CollationKey(" + subs + ")" + ".LT." + "CollationKey(" + addOne + ") Failed.");
result = CollationKey2.compareTo(CollationKey1);
if (result != 1)
errln("CollationKey(" + addOne + ")" + ".GT." + "CollationKey(" + subs + ") Failed.");
}
public void TestCollationKey()
{
String source = "-abcdefghijklmnopqrstuvwxyz#&^[email protected]";
Random r = new Random(3);
int s = checkValue(r.nextInt() % source.length());
int t = checkValue(r.nextInt() % source.length());
int slen = checkValue((r.nextInt() - source.length()) % source.length());
int tlen = checkValue((r.nextInt() - source.length()) % source.length());
String subs = source.substring((s > slen ? slen : s), (s >= slen ? s : slen));
String subt = source.substring((t > tlen ? tlen : t), (t >= tlen ? t : tlen));
myCollator.setStrength(Collator.TERTIARY);
CollationKey CollationKey1 = myCollator.getCollationKey(subs);
CollationKey CollationKey2 = myCollator.getCollationKey(subt);
int result = CollationKey1.compareTo(CollationKey2); // Tertiary
int revResult = CollationKey2.compareTo(CollationKey1); // Tertiary
report(("CollationKey(" + subs + ")"), ("CollationKey(" + subt + ")"), result, revResult);
myCollator.setStrength(Collator.SECONDARY);
CollationKey1 = myCollator.getCollationKey(subs);
CollationKey2 = myCollator.getCollationKey(subt);
result = CollationKey1.compareTo(CollationKey2); // Secondary
revResult = CollationKey2.compareTo(CollationKey1); // Secondary
report(("CollationKey(" + subs + ")") , ("CollationKey(" + subt + ")"), result, revResult);
myCollator.setStrength(Collator.PRIMARY);
CollationKey1 = myCollator.getCollationKey(subs);
CollationKey2 = myCollator.getCollationKey(subt);
result = CollationKey1.compareTo(CollationKey2); // Primary
revResult = CollationKey2.compareTo(CollationKey1); // Primary
report(("CollationKey(" + subs + ")"), ("CollationKey(" + subt + ")"), result, revResult);
String addOne = subs + "\uE000";
CollationKey1 = myCollator.getCollationKey(subs);
CollationKey2 = myCollator.getCollationKey(addOne);
result = CollationKey1.compareTo(CollationKey2);
if (result != -1)
errln("CollationKey(" + subs + ")" + ".LT." + "CollationKey(" + addOne + ") Failed.");
result = CollationKey2.compareTo(CollationKey1);
if (result != 1)
errln("CollationKey(" + addOne + ")" + ".GT." + "CollationKey(" + subs + ") Failed.");
}