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