Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

FrameContainerComparator.java 4.1KB

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