Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

FrameContainerComparator.java 4.4KB

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