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.

QueueComparators.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Copyright (c) 2006-2016 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.parser.irc.outputqueue;
  23. import com.dmdirc.parser.common.QueuePriority;
  24. import java.time.Clock;
  25. import java.time.LocalDateTime;
  26. import java.time.temporal.TemporalAmount;
  27. import java.util.Comparator;
  28. /**
  29. * Provides comparators for comparing {@link QueueItem}s.
  30. */
  31. public final class QueueComparators {
  32. private QueueComparators() {
  33. // Shouldn't be used
  34. }
  35. /**
  36. * Provides a comparator that checks priority and item number.
  37. *
  38. * @return a comparator that compares items by their priority, and then their item number.
  39. */
  40. public static Comparator<QueueItem> byPriorityThenNumber() {
  41. return Comparator.comparing(QueueItem::getPriority)
  42. .thenComparing(QueueItem::getItemNumber);
  43. }
  44. /**
  45. * Provides a comparator that checks priority and item number, with special handling for
  46. * starved items.
  47. *
  48. * <p>If an item has been queued for longer than {@code starvationThreshold}, its priority
  49. * will be effectively ignored (i.e., it will just be compared on item number). This ensures
  50. * that lower priority items are not kept in the queue indefinitely if there is a constant
  51. * stream of higher priority items.
  52. *
  53. * @param starvationThreshold The time an item must be queued for to be considered starved.
  54. * @return a comparator that compares items by their priority unless they are starved, and then
  55. * their item number.
  56. */
  57. public static Comparator<QueueItem> byPriorityThenNumber(
  58. final TemporalAmount starvationThreshold) {
  59. return byPriorityThenNumber(Clock.systemDefaultZone(), starvationThreshold);
  60. }
  61. /**
  62. * Provides a comparator that checks priority and item number, with special handling for
  63. * starved items.
  64. *
  65. * <p>If an item has been queued for longer than {@code starvationThreshold}, its priority
  66. * will be effectively ignored (i.e., it will just be compared on item number). This ensures
  67. * that lower priority items are not kept in the queue indefinitely if there is a constant
  68. * stream of higher priority items.
  69. *
  70. * @param clock The clock to use to get the current time.
  71. * @param starvationThreshold The time an item must be queued for to be considered starved.
  72. * @return a comparator that compares items by their priority unless they are starved, and then
  73. * their item number.
  74. */
  75. public static Comparator<QueueItem> byPriorityThenNumber(
  76. final Clock clock,
  77. final TemporalAmount starvationThreshold) {
  78. return Comparator.<QueueItem, QueuePriority>comparing(
  79. item -> getPriorityIfNotStarved(item, clock, starvationThreshold))
  80. .thenComparing(QueueItem::getItemNumber);
  81. }
  82. /**
  83. * Gets the priority of the item if it was created within the given timespan, or
  84. * {@link QueuePriority#IMMEDIATE} if the item is older. This allows items that are otherwise
  85. * being starved to be sorted to the front of a queue.
  86. *
  87. * @param item The item to get the priority of.
  88. * @param clock The clock to use to get the current time.
  89. * @param timespan The timespan after which an item will be considered starved.
  90. * @return The priority of the item, or {@link QueuePriority#IMMEDIATE}.
  91. */
  92. private static QueuePriority getPriorityIfNotStarved(
  93. final QueueItem item, final Clock clock, final TemporalAmount timespan) {
  94. if (item.getTime().isAfter(LocalDateTime.now(clock).minus(timespan))) {
  95. return item.getPriority();
  96. } else {
  97. return QueuePriority.IMMEDIATE;
  98. }
  99. }
  100. }