/src/main/java/com/atlassian/crowd/model/group/GroupComparator.java

https://bitbucket.org/atlassian/crowd-rest-client · Java · 130 lines · 69 code · 16 blank · 45 comment · 19 complexity · 5dd81d22f0a4de2a9fef766c0a4dfe89 MD5 · raw file

  1. /*
  2. * Copyright © 2010 - 2015 Atlassian Corporation Pty Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * https://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.atlassian.crowd.model.group;
  17. import static com.atlassian.crowd.embedded.impl.IdentifierUtils.compareToInLowerCase;
  18. import static com.atlassian.crowd.embedded.impl.IdentifierUtils.equalsInLowerCase;
  19. import static com.atlassian.crowd.embedded.impl.IdentifierUtils.toLowerCase;
  20. import java.util.Comparator;
  21. /**
  22. * Supplies re-useable methods for equals, hashcode and compareTo that can be shared with different implementations of
  23. * {@link Group} in order to be compatible.
  24. *
  25. * You can also instantiate this class to get a Comparator of Group.
  26. *
  27. * Note: the GroupComparator is distinct from the EmbeddedGroupComparator as
  28. * model groups take the directoryId into consideration when performing equals/hashCode/compareTo.
  29. */
  30. public class GroupComparator implements Comparator<Group>
  31. {
  32. /** Singleton instance of Comparator<Group> */
  33. public static final Comparator<Group> GROUP_COMPARATOR = new GroupComparator();
  34. private GroupComparator()
  35. {
  36. // Singleton
  37. }
  38. /**
  39. * Checks whether the two Group objects are equal according to the contract of the {@link Group} interface.
  40. * <p>
  41. * If you are implementing {@link Group#equals(Object)} then just write code like this:
  42. * <pre>
  43. * public boolean equals(Object o)
  44. * {
  45. * return (o instanceof Group) &amp;&amp; GroupComparator.equal(this, (Group) o);
  46. * }
  47. * </pre>
  48. *
  49. * @param group1 First Group
  50. * @param group2 Second Group
  51. * @return true if these are two equal Groups.
  52. */
  53. public static boolean equal(Group group1, Group group2)
  54. {
  55. if (group1 == group2)
  56. {
  57. return true;
  58. }
  59. if (group1 == null || group2 == null)
  60. {
  61. return false;
  62. }
  63. if (group1.getDirectoryId() != group2.getDirectoryId())
  64. {
  65. return false;
  66. }
  67. // null groupname is illegal and so throwing an NPE is acceptable.
  68. if (!equalsInLowerCase(group1.getName(), group2.getName()))
  69. {
  70. return false;
  71. }
  72. return true;
  73. }
  74. public static boolean equalsObject(Group group, Object o)
  75. {
  76. if (group == o)
  77. {
  78. return true;
  79. }
  80. else if (group == null)
  81. {
  82. return false;
  83. }
  84. else if (!(o instanceof Group))
  85. {
  86. return false;
  87. }
  88. Group otherGroup = (Group) o;
  89. return equal(group, otherGroup);
  90. }
  91. public static int hashCode(Group group)
  92. {
  93. // Taken from Long.hashCode()
  94. int result = (int)(group.getDirectoryId() ^ (group.getDirectoryId() >>> 32));
  95. return 31 * toLowerCase(group.getName()).hashCode() + result;
  96. }
  97. public static int compareTo(Group group1, Group group2)
  98. {
  99. // First compare names
  100. int nameCompare = compareToInLowerCase(group1.getName(), group2.getName());
  101. if (nameCompare != 0)
  102. {
  103. return nameCompare;
  104. }
  105. // Names are equal - use directoryId as the tie-breaker
  106. final long directoryId1 = group1.getDirectoryId();
  107. final long directoryId2 = group2.getDirectoryId();
  108. return (directoryId1 < directoryId2 ? -1 : (directoryId1 == directoryId2 ? 0 : 1));
  109. }
  110. public int compare(Group group1, Group group2)
  111. {
  112. return compareTo(group1, group2);
  113. }
  114. }