You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FrameContainerComparator.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Copyright (c) 2006-2015 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc;
  23. import com.dmdirc.interfaces.Connection;
  24. import com.dmdirc.interfaces.GroupChat;
  25. import java.io.Serializable;
  26. import java.util.Comparator;
  27. import static com.google.common.base.Preconditions.checkNotNull;
  28. /**
  29. * Compares FrameContainers by name.
  30. */
  31. public class FrameContainerComparator implements Comparator<FrameContainer>, Serializable {
  32. /** A version number for this class. */
  33. private static final long serialVersionUID = 1;
  34. /**
  35. * Compares two frame containers names.
  36. *
  37. * @param item1 The first container to compare
  38. * @param item2 The second container to compare
  39. *
  40. * @return -1 if item1 is before item2, 0 if they're equal, +1 if item1 is after item2.
  41. */
  42. @Override
  43. @Precondition({
  44. "item1 is non-null",
  45. "item2 is non-null",
  46. "item1.toString() returns a non-null value",
  47. "item2.toString() returns a non-null value"
  48. })
  49. public int compare(final FrameContainer item1, final FrameContainer item2) {
  50. checkNotNull(item1);
  51. checkNotNull(item2);
  52. checkNotNull(item1.getName());
  53. checkNotNull(item2.getName());
  54. if (sortBefore(item1, item2)) {
  55. return -1;
  56. } else if (sortAfter(item1, item2)) {
  57. return 1;
  58. } else {
  59. final int position = item1.getName()
  60. .compareToIgnoreCase(item2.getName());
  61. if (position == 0) {
  62. return Integer.valueOf(item1.hashCode()).compareTo(item2.hashCode());
  63. } else {
  64. return position;
  65. }
  66. }
  67. }
  68. /**
  69. * Compares frame container types and checks order preferences.
  70. *
  71. * @param item1 The new container to be tested
  72. * @param item2 The existing container to test against
  73. *
  74. * @return True iff the new container should be before the old container
  75. */
  76. private static boolean sortBefore(
  77. final FrameContainer item1,
  78. final FrameContainer item2) {
  79. return getPosition(item1) < getPosition(item2);
  80. }
  81. /**
  82. * Compares frame container types and checks order preferences.
  83. *
  84. * @param item1 The new container to be tested
  85. * @param item2 The existing container to test against
  86. *
  87. * @return True iff the new container should be after the old container
  88. */
  89. private static boolean sortAfter(final FrameContainer item1,
  90. final FrameContainer item2) {
  91. return getPosition(item1) > getPosition(item2);
  92. }
  93. /**
  94. * Returns an integer corresponding to the expected order of a frame container.
  95. *
  96. * @param item The frame container to be tested
  97. *
  98. * @return Position of the frame container
  99. */
  100. private static int getPosition(final FrameContainer item) {
  101. if (item instanceof GlobalWindow) {
  102. return 0;
  103. } else if (item instanceof Connection) {
  104. return 1;
  105. } else if (item instanceof GroupChat) {
  106. return 2;
  107. } else if (item instanceof Query) {
  108. return 3;
  109. } else {
  110. return 4;
  111. }
  112. }
  113. }