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.

NicklistComparator.java 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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.addons.ui_swing.components;
  18. import com.dmdirc.interfaces.GroupChatUser;
  19. import com.google.common.collect.ComparisonChain;
  20. import java.io.Serializable;
  21. import java.util.Comparator;
  22. /**
  23. * Compares nicklist entries to each other, for sorting purposes.
  24. */
  25. public final class NicklistComparator implements Comparator<GroupChatUser>, Serializable {
  26. /** A version number for this class. */
  27. private static final long serialVersionUID = 1;
  28. /** whether to sort the nicklist by modes. */
  29. private final boolean sortByMode;
  30. /** whether to sort the nicklist by case. */
  31. private final boolean sortByCase;
  32. /**
  33. * Creates a new instance of NicklistComparator.
  34. *
  35. * @param newSortByMode sorts by channel mode of the user
  36. * @param newSortByCase sorts by nickname case
  37. */
  38. public NicklistComparator(final boolean newSortByMode,
  39. final boolean newSortByCase) {
  40. this.sortByMode = newSortByMode;
  41. this.sortByCase = newSortByCase;
  42. }
  43. @Override
  44. public int compare(final GroupChatUser client1, final GroupChatUser client2) {
  45. ComparisonChain comparisonChain = ComparisonChain.start();
  46. if (sortByMode) {
  47. comparisonChain = comparisonChain.compare(client1.getAllModes(),
  48. client2.getAllModes(), client1.getModeComparator());
  49. }
  50. if (sortByCase) {
  51. comparisonChain = comparisonChain.compare(client1.getNickname(), client2.getNickname());
  52. } else {
  53. comparisonChain = comparisonChain.compare(client1.getNickname().toLowerCase(),
  54. client2.getNickname().toLowerCase());
  55. }
  56. return comparisonChain.result();
  57. }
  58. }