PageRenderTime 60ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/saxonB/net/sf/saxon/sort/UppercaseFirstCollator.java

https://bitbucket.org/dmwelch/phdxnat_pipeline
Java | 104 lines | 44 code | 13 blank | 47 comment | 17 complexity | 8f653804fdaa2fb768d3e0c15e8d91a4 MD5 | raw file
  1. package net.sf.saxon.sort;
  2. /**
  3. * A Collator used for comparing strings, with upper case collated before lower case
  4. * if the strings are otherwise equal. This is implemented as a wrapper around a collator
  5. * that compares the strings ignoring case.
  6. *
  7. * @author Michael H. Kay
  8. *
  9. */
  10. public class UppercaseFirstCollator implements StringCollator, java.io.Serializable {
  11. private StringCollator baseCollator;
  12. /**
  13. * Create an UppercaseFirstCollator
  14. * @param base the base collator used to compare strings irrespective of case
  15. */
  16. public UppercaseFirstCollator(StringCollator base) {
  17. baseCollator = base;
  18. }
  19. /**
  20. * Compare two string objects: case is irrelevant, unless the strings are equal ignoring
  21. * case, in which case uppercase comes first.
  22. * @return <0 if a<b, 0 if a=b, >0 if a>b
  23. * @throws ClassCastException if the objects do not implement the CharSequence interface
  24. */
  25. public int compareStrings(String a, String b) {
  26. int diff = baseCollator.compareStrings(a, b);
  27. if (diff != 0) {
  28. return diff;
  29. }
  30. // This is doing a character-by-character comparison, which isn't really right.
  31. // There might be a sequence of letters constituting a single collation unit.
  32. int i = 0;
  33. int j = 0;
  34. while (true) {
  35. // Skip characters that are equal in the two strings
  36. while (i < a.length() && j < b.length() && a.charAt(i) == b.charAt(j)) {
  37. i++;
  38. j++;
  39. }
  40. // Skip non-letters in the first string
  41. while (i < a.length() && !Character.isLetter(a.charAt(i))) {
  42. i++;
  43. }
  44. // Skip non-letters in the second string
  45. while (j < b.length() && !Character.isLetter(b.charAt(j))) {
  46. j++;
  47. }
  48. // if either string is exhausted, treat the strings as equal
  49. if (i >= a.length()) {
  50. return 0;
  51. }
  52. if (j >= b.length()) {
  53. return 0;
  54. }
  55. // if one character is upper case and the other isn't, the issue is decided
  56. boolean aUpper = Character.isUpperCase(a.charAt(i++));
  57. boolean bUpper = Character.isUpperCase(b.charAt(j++));
  58. if (aUpper && !bUpper) {
  59. return -1;
  60. }
  61. if (bUpper && !aUpper) {
  62. return +1;
  63. }
  64. }
  65. }
  66. /**
  67. * Get a collation key for two Strings. The essential property of collation keys
  68. * is that if two values are equal under the collation, then the collation keys are
  69. * compare correctly under the equals() method.
  70. */
  71. public Object getCollationKey(String s) {
  72. return baseCollator.getCollationKey(s);
  73. }
  74. }
  75. //
  76. // The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
  77. // you may not use this file except in compliance with the License. You may obtain a copy of the
  78. // License at http://www.mozilla.org/MPL/
  79. //
  80. // Software distributed under the License is distributed on an "AS IS" basis,
  81. // WITHOUT WARRANTY OF ANY KIND, either express or implied.
  82. // See the License for the specific language governing rights and limitations under the License.
  83. //
  84. // The Original Code is: all this file.
  85. //
  86. // The Initial Developer of this module is Michael H. Kay.
  87. //
  88. // Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
  89. //
  90. // Contributor(s): none.
  91. //