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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright (c) 2006-2012 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.logger.Logger;
  24. import java.io.Serializable;
  25. import java.util.Comparator;
  26. /**
  27. * Compares FrameContainers by name.
  28. */
  29. public final class FrameContainerComparator implements Comparator<FrameContainer>,
  30. Serializable {
  31. /**
  32. * A version number for this class. It should be changed whenever the class
  33. * structure is changed (or anything else that would prevent serialized
  34. * objects being unserialized with the new class).
  35. */
  36. private static final long serialVersionUID = 1;
  37. /**
  38. * Compares two frame containers names.
  39. *
  40. * @param item1 The first container to compare
  41. * @param item2 The second container to compare
  42. * @return -1 if item1 is before item2, 0 if they're equal,
  43. * +1 if item1 is after item2.
  44. */
  45. @Override
  46. @Precondition({
  47. "item1 is non-null",
  48. "item2 is non-null",
  49. "item1.toString() returns a non-null value",
  50. "item2.toString() returns a non-null value"
  51. })
  52. public int compare(final FrameContainer item1, final FrameContainer item2) {
  53. Logger.assertTrue(item1 != null);
  54. Logger.assertTrue(item2 != null);
  55. Logger.assertTrue(item1.getName() != null);
  56. Logger.assertTrue(item2.getName() != null);
  57. if (sortBefore(item1, item2)) {
  58. return -1;
  59. } else if (sortAfter(item1, item2)) {
  60. return 1;
  61. } else {
  62. final int position = item1.getName()
  63. .compareToIgnoreCase(item2.getName());
  64. if (position == 0) {
  65. return Integer.valueOf(item1.hashCode())
  66. .compareTo(Integer.valueOf(item2.hashCode()));
  67. } else {
  68. return position;
  69. }
  70. }
  71. }
  72. /**
  73. * Compares frame container types and checks order preferences.
  74. *
  75. * @param item1 The new container to be tested
  76. * @param item2 The existing container to test against
  77. * @return True iff the new container should be before the old container
  78. */
  79. private static boolean sortBefore(final FrameContainer item1,
  80. final FrameContainer item2) {
  81. return getPosition(item1) < getPosition(item2);
  82. }
  83. /**
  84. * Compares frame container types and checks order preferences.
  85. *
  86. * @param item1 The new container to be tested
  87. * @param item2 The existing container to test against
  88. * @return True iff the new container should be after the old container
  89. */
  90. private static boolean sortAfter(final FrameContainer item1,
  91. final FrameContainer item2) {
  92. return getPosition(item1) > getPosition(item2);
  93. }
  94. /**
  95. * Returns an integer corresponding to the expected order of a frame
  96. * container.
  97. *
  98. * @param item The frame container to be tested
  99. * @return Position of the frame container
  100. */
  101. private static int getPosition(final FrameContainer item) {
  102. if (item instanceof GlobalWindow) {
  103. return 0;
  104. } else if (item instanceof Server) {
  105. return 1;
  106. } else if (item instanceof Raw) {
  107. return 2;
  108. } else if (item instanceof Channel) {
  109. return 3;
  110. } else if (item instanceof Query) {
  111. return 4;
  112. } else {
  113. return 5;
  114. }
  115. }
  116. }