Here are the examples of the java api class java.text.CollationKey taken from open source projects.
1. WorkHorseForCollatorDatatypes#stringCompare()
Project: derby
File: WorkHorseForCollatorDatatypes.java
File: WorkHorseForCollatorDatatypes.java
/** @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 (// str2 == null ckey1 != null) return -1; if (// this == null ckey2 != null) return 1; // both == null return 0; } return ckey1.compareTo(ckey2); }
2. CollationKeyTestImpl#InsertionSort()
Project: openjdk
File: CollationKeyTestImpl.java
File: CollationKeyTestImpl.java
/* * Sort the array of CollationKey using compareTo method in insertion sort. */ 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; } }
3. SQLChar#hashCodeForCollation()
Project: derby
File: SQLChar.java
File: SQLChar.java
/** * Hash code implementation for collator sensitive subclasses. */ int hashCodeForCollation() { CollationKey key = null; try { key = getCollationKey(); } catch (StandardException se) { if (SanityManager.DEBUG) { SanityManager.THROWASSERT("Unexpected exception", se); } } return key == null ? 0 : key.hashCode(); }
4. APITest#TestCollationKey()
Project: openjdk
File: APITest.java
File: APITest.java
//---------------------------------------------------------------------------- // ctor -- Tests the constructor methods // 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..."); }
5. MonkeyTest#TestCollationKey()
Project: openjdk
File: MonkeyTest.java
File: MonkeyTest.java
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); // Tertiary int result = CollationKey1.compareTo(CollationKey2); // Tertiary int revResult = CollationKey2.compareTo(CollationKey1); report(("CollationKey(" + subs + ")"), ("CollationKey(" + subt + ")"), result, revResult); myCollator.setStrength(Collator.SECONDARY); CollationKey1 = myCollator.getCollationKey(subs); CollationKey2 = myCollator.getCollationKey(subt); // Secondary result = CollationKey1.compareTo(CollationKey2); // Secondary revResult = CollationKey2.compareTo(CollationKey1); report(("CollationKey(" + subs + ")"), ("CollationKey(" + subt + ")"), result, revResult); myCollator.setStrength(Collator.PRIMARY); CollationKey1 = myCollator.getCollationKey(subs); CollationKey2 = myCollator.getCollationKey(subt); // Primary result = CollationKey1.compareTo(CollationKey2); // Primary revResult = CollationKey2.compareTo(CollationKey1); report(("CollationKey(" + subs + ")"), ("CollationKey(" + subt + ")"), result, revResult); String addOne = subs + "?"; 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."); }
6. CollationKeyTestImpl#run()
Project: openjdk
File: CollationKeyTestImpl.java
File: CollationKeyTestImpl.java
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]); //used later target_keys[i] = myCollator.getCollationKey(targetData_ja[i]); } /* 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(); }