PageRenderTime 40ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

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

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