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.

WindowModelComparator.java 4.3KB

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