Java Code Examples for java.text.CollationKey#compareTo()

The following examples show how to use java.text.CollationKey#compareTo() . 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: FriendCompara.java    From Android with MIT License 6 votes vote down vote up
@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);
}
 
Example 2
Source File: GroupComPara.java    From Android with MIT License 6 votes vote down vote up
@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);
}
 
Example 3
Source File: WorkHorseForCollatorDatatypes.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/** @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);
}
 
Example 4
Source File: WorkHorseForCollatorDatatypes.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/** @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);
}
 
Example 5
Source File: WorkHorseForCollatorDatatypes.java    From spliceengine with GNU Affero General Public License v3.0 6 votes vote down vote up
/** @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);
}
 
Example 6
Source File: MonkeyTest.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
public void TestCollationKey()
{
    String source = "-abcdefghijklmnopqrstuvwxyz#&^$@";
    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.");
}
 
Example 7
Source File: MonkeyTest.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public void TestCollationKey()
{
    String source = "-abcdefghijklmnopqrstuvwxyz#&^$@";
    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.");
}
 
Example 8
Source File: MonkeyTest.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public void TestCollationKey()
{
    String source = "-abcdefghijklmnopqrstuvwxyz#&^$@";
    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.");
}
 
Example 9
Source File: BaseListComparatorSort.java    From Android with MIT License 5 votes vote down vote up
@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);
}
 
Example 10
Source File: GroupMemberCompara.java    From Android with MIT License 5 votes vote down vote up
@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);
}
 
Example 11
Source File: MonkeyTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public void TestCollationKey()
{
    String source = "-abcdefghijklmnopqrstuvwxyz#&^$@";
    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.");
}
 
Example 12
Source File: MonkeyTest.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
public void TestCollationKey()
{
    String source = "-abcdefghijklmnopqrstuvwxyz#&^$@";
    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.");
}