下面列出了怎么用java.text.CollationKey的API类实例代码及写法,或者点击链接到github查看源代码。
private void InsertionSort(CollationKey[] keys){
int f, i;
CollationKey tmp;
for (f=1; f < keys.length; f++){
if(keys[f].compareTo( keys[f-1]) > 0){
continue;
}
tmp = keys[f];
i = f-1;
while ( (i>=0) && (keys[i].compareTo(tmp) > 0) ) {
keys[i+1] = keys[i];
i--;
}
keys[i+1]=tmp;
}
}
private void InsertionSort(CollationKey[] keys){
int f, i;
CollationKey tmp;
for (f=1; f < keys.length; f++){
if(keys[f].compareTo( keys[f-1]) > 0){
continue;
}
tmp = keys[f];
i = f-1;
while ( (i>=0) && (keys[i].compareTo(tmp) > 0) ) {
keys[i+1] = keys[i];
i--;
}
keys[i+1]=tmp;
}
}
/** @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);
}
@SuppressFBWarnings(value = "EI_EXPOSE_REP2", justification = "DB-9406")
public void copyState
(
String otherValue,
char[] otherRawData,
int otherRawLength,
CollationKey otherCKey,
InputStream otherStream,
Clob otherClobValue,
LocaleFinder otherLocaleFinder
)
{
value = otherValue;
rawData = otherRawData;
rawLength = otherRawLength;
cKey = otherCKey;
stream = otherStream;
_clobValue = otherClobValue;
localeFinder = otherLocaleFinder;
isNull = evaluateNull();
}
private void InsertionSort(CollationKey[] keys){
int f, i;
CollationKey tmp;
for (f=1; f < keys.length; f++){
if(keys[f].compareTo( keys[f-1]) > 0){
continue;
}
tmp = keys[f];
i = f-1;
while ( (i>=0) && (keys[i].compareTo(tmp) > 0) ) {
keys[i+1] = keys[i];
i--;
}
keys[i+1]=tmp;
}
}
@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);
}
private void InsertionSort(CollationKey[] keys){
int f, i;
CollationKey tmp;
for (f=1; f < keys.length; f++){
if(keys[f].compareTo( keys[f-1]) > 0){
continue;
}
tmp = keys[f];
i = f-1;
while ( (i>=0) && (keys[i].compareTo(tmp) > 0) ) {
keys[i+1] = keys[i];
i--;
}
keys[i+1]=tmp;
}
}
/** @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);
}
private void InsertionSort(CollationKey[] keys){
int f, i;
CollationKey tmp;
for (f=1; f < keys.length; f++){
if(keys[f].compareTo( keys[f-1]) > 0){
continue;
}
tmp = keys[f];
i = f-1;
while ( (i>=0) && (keys[i].compareTo(tmp) > 0) ) {
keys[i+1] = keys[i];
i--;
}
keys[i+1]=tmp;
}
}
/** @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);
}
@Override
public int compare(FileNode o1, FileNode o2) {
// 判断两个对比对象是否是开头包含数字,如果包含数字则获取数字并按数字真正大小进行排序
BigDecimal num1,num2;
if (null != (num1 = isStartNumber(o1))
&& null != (num2 = isStartNumber(o2))) {
return num1.subtract(num2).intValue();
}
CollationKey c1 = cmp.getCollationKey(o1.getOriginName());
CollationKey c2 = cmp.getCollationKey(o2.getOriginName());
return cmp.compare(c1.getSourceString(), c2.getSourceString());
}
public final void TestCollationKey( )
{
logln("testing CollationKey begins...");
Collator col = null;
try {
col = Collator.getInstance(Locale.ROOT);
} catch (Exception foo) {
errln("Error : " + foo.getMessage());
errln("Default collation creation failed.");
}
if (col == null) {
return;
}
String test1 = "Abcda", test2 = "abcda";
logln("Use tertiary comparison level testing ....");
CollationKey sortk1 = col.getCollationKey(test1);
CollationKey sortk2 = col.getCollationKey(test2);
doAssert(sortk1.compareTo(sortk2) > 0,
"Result should be \"Abcda\" >>> \"abcda\"");
CollationKey sortk3 = sortk2;
CollationKey sortkNew = sortk1;
doAssert(sortk1 != sortk2, "The sort keys should be different");
doAssert(sortk1.hashCode() != sortk2.hashCode(), "sort key hashCode() failed");
doAssert(sortk2.compareTo(sortk3) == 0, "The sort keys should be the same");
doAssert(sortk1 == sortkNew, "The sort keys assignment failed");
doAssert(sortk1.hashCode() == sortkNew.hashCode(), "sort key hashCode() failed");
doAssert(sortkNew != sortk3, "The sort keys should be different");
doAssert(sortk1.compareTo(sortk3) > 0, "Result should be \"Abcda\" >>> \"abcda\"");
doAssert(sortk2.compareTo(sortk3) == 0, "Result should be \"abcda\" == \"abcda\"");
long cnt1, cnt2;
byte byteArray1[] = sortk1.toByteArray();
byte byteArray2[] = sortk2.toByteArray();
doAssert(byteArray1 != null && byteArray2 != null, "CollationKey.toByteArray failed.");
logln("testing sortkey ends...");
}
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.");
}
@Test
public void testICU(){
String str = "hello";
java.text.Collator col = java.text.Collator.getInstance(Locale.US);;
CollationKey ck = col.getCollationKey(str);
byte[] bytes = ck.toByteArray();
System.out.println("collation key bytes length: " + bytes.length);
System.out.println("collation key bytes: " + Arrays.toString(bytes));
System.out.println("collation key as string:" + new String(bytes));
System.out.println("original string: " + Arrays.toString(str.getBytes()));
}
/**
* Generate a key for sorting.
*/
@Override
CollationKey generateKey() {
String k = name() + flatSignature() + typeParametersString();
// ',' and '&' are between '$' and 'a': normalize to spaces.
k = k.replace(',', ' ').replace('&', ' ');
// System.out.println("COLLATION KEY FOR " + this + " is \"" + k + "\"");
return env.doclocale.collator.getCollationKey(k);
}
/**
* return a key for sorting.
*/
CollationKey key() {
if (collationkey == null) {
collationkey = generateKey();
}
return collationkey;
}
public final void TestCollationKey( )
{
logln("testing CollationKey begins...");
Collator col = null;
try {
col = Collator.getInstance(Locale.ROOT);
} catch (Exception foo) {
errln("Error : " + foo.getMessage());
errln("Default collation creation failed.");
}
if (col == null) {
return;
}
String test1 = "Abcda", test2 = "abcda";
logln("Use tertiary comparison level testing ....");
CollationKey sortk1 = col.getCollationKey(test1);
CollationKey sortk2 = col.getCollationKey(test2);
doAssert(sortk1.compareTo(sortk2) > 0,
"Result should be \"Abcda\" >>> \"abcda\"");
CollationKey sortk3 = sortk2;
CollationKey sortkNew = sortk1;
doAssert(sortk1 != sortk2, "The sort keys should be different");
doAssert(sortk1.hashCode() != sortk2.hashCode(), "sort key hashCode() failed");
doAssert(sortk2.compareTo(sortk3) == 0, "The sort keys should be the same");
doAssert(sortk1 == sortkNew, "The sort keys assignment failed");
doAssert(sortk1.hashCode() == sortkNew.hashCode(), "sort key hashCode() failed");
doAssert(sortkNew != sortk3, "The sort keys should be different");
doAssert(sortk1.compareTo(sortk3) > 0, "Result should be \"Abcda\" >>> \"abcda\"");
doAssert(sortk2.compareTo(sortk3) == 0, "Result should be \"abcda\" == \"abcda\"");
long cnt1, cnt2;
byte byteArray1[] = sortk1.toByteArray();
byte byteArray2[] = sortk2.toByteArray();
doAssert(byteArray1 != null && byteArray2 != null, "CollationKey.toByteArray failed.");
logln("testing sortkey ends...");
}
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.");
}
/**
* Generate a key for sorting.
*/
@Override
CollationKey generateKey() {
String k = name() + flatSignature() + typeParametersString();
// ',' and '&' are between '$' and 'a': normalize to spaces.
k = k.replace(',', ' ').replace('&', ' ');
// System.out.println("COLLATION KEY FOR " + this + " is \"" + k + "\"");
return env.doclocale.collator.getCollationKey(k);
}
/**
* Hash code implementation for collator sensitive subclasses.
*/
int hashCodeForCollation() {
CollationKey key = null;
try {
key = getCollationKey();
} catch (StandardException se) {
// ignore exceptions, like we do in hashCode()
if (SanityManager.DEBUG) {
SanityManager.THROWASSERT("Unexpected exception", se);
}
}
return key == null ? 0 : key.hashCode();
}
/**
* Generate a key for sorting.
*/
@Override
CollationKey generateKey() {
String k = name() + flatSignature() + typeParametersString();
// ',' and '&' are between '$' and 'a': normalize to spaces.
k = k.replace(',', ' ').replace('&', ' ');
// System.out.println("COLLATION KEY FOR " + this + " is \"" + k + "\"");
return env.doclocale.collator.getCollationKey(k);
}
/**
* return a key for sorting.
*/
CollationKey key() {
if (collationkey == null) {
collationkey = generateKey();
}
return collationkey;
}
public final void TestCollationKey( )
{
logln("testing CollationKey begins...");
Collator col = null;
try {
col = Collator.getInstance(Locale.ROOT);
} catch (Exception foo) {
errln("Error : " + foo.getMessage());
errln("Default collation creation failed.");
}
if (col == null) {
return;
}
String test1 = "Abcda", test2 = "abcda";
logln("Use tertiary comparison level testing ....");
CollationKey sortk1 = col.getCollationKey(test1);
CollationKey sortk2 = col.getCollationKey(test2);
doAssert(sortk1.compareTo(sortk2) > 0,
"Result should be \"Abcda\" >>> \"abcda\"");
CollationKey sortk3 = sortk2;
CollationKey sortkNew = sortk1;
doAssert(sortk1 != sortk2, "The sort keys should be different");
doAssert(sortk1.hashCode() != sortk2.hashCode(), "sort key hashCode() failed");
doAssert(sortk2.compareTo(sortk3) == 0, "The sort keys should be the same");
doAssert(sortk1 == sortkNew, "The sort keys assignment failed");
doAssert(sortk1.hashCode() == sortkNew.hashCode(), "sort key hashCode() failed");
doAssert(sortkNew != sortk3, "The sort keys should be different");
doAssert(sortk1.compareTo(sortk3) > 0, "Result should be \"Abcda\" >>> \"abcda\"");
doAssert(sortk2.compareTo(sortk3) == 0, "Result should be \"abcda\" == \"abcda\"");
long cnt1, cnt2;
byte byteArray1[] = sortk1.toByteArray();
byte byteArray2[] = sortk2.toByteArray();
doAssert(byteArray1 != null && byteArray2 != null, "CollationKey.toByteArray failed.");
logln("testing sortkey ends...");
}
@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);
}
/**
* Generate a key for sorting.
*/
@Override
CollationKey generateKey() {
String k = name() + flatSignature() + typeParametersString();
// ',' and '&' are between '$' and 'a': normalize to spaces.
k = k.replace(',', ' ').replace('&', ' ');
// System.out.println("COLLATION KEY FOR " + this + " is \"" + k + "\"");
return env.doclocale.collator.getCollationKey(k);
}
/**
* return a key for sorting.
*/
CollationKey key() {
if (collationkey == null) {
collationkey = generateKey();
}
return collationkey;
}
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;
}
};
}
/**
* Generate a key for sorting.
*/
@Override
CollationKey generateKey() {
String k = name() + flatSignature() + typeParametersString();
// ',' and '&' are between '$' and 'a': normalize to spaces.
k = k.replace(',', ' ').replace('&', ' ');
// System.out.println("COLLATION KEY FOR " + this + " is \"" + k + "\"");
return env.doclocale.collator.getCollationKey(k);
}