|
@@ -24,89 +24,78 @@ package com.dmdirc.parser.irc.outputqueue;
|
24
|
24
|
|
25
|
25
|
import com.dmdirc.parser.common.QueuePriority;
|
26
|
26
|
|
|
27
|
+import com.google.auto.value.AutoValue;
|
|
28
|
+
|
27
|
29
|
import java.time.Clock;
|
28
|
30
|
import java.time.LocalDateTime;
|
|
31
|
+import java.util.concurrent.atomic.AtomicLong;
|
29
|
32
|
|
30
|
33
|
/**
|
31
|
34
|
* Queued Item.
|
32
|
35
|
*/
|
33
|
|
-public class QueueItem {
|
|
36
|
+@AutoValue
|
|
37
|
+public abstract class QueueItem {
|
34
|
38
|
|
35
|
|
- /** Global Item Number. */
|
36
|
|
- private static long number;
|
37
|
|
- /** Line to send. */
|
38
|
|
- private final String line;
|
39
|
|
- /** Time this line was added. */
|
40
|
|
- private final LocalDateTime time;
|
41
|
|
- /** Item Number. */
|
42
|
|
- private final long itemNumber;
|
43
|
|
- /** What is the priority of this line? */
|
44
|
|
- private final QueuePriority priority;
|
|
39
|
+ /** Static counter to assign IDs to items. */
|
|
40
|
+ // TODO: Move this into OutputQueue or somewhere not static.
|
|
41
|
+ private static final AtomicLong itemCounter = new AtomicLong(0);
|
45
|
42
|
|
46
|
43
|
/**
|
47
|
|
- * Create a new QueueItem.
|
|
44
|
+ * Get the line that is to be sent to the server.
|
48
|
45
|
*
|
49
|
|
- * @param line Line to send
|
50
|
|
- * @param priority Priority for the queue item
|
|
46
|
+ * @return the line to be sent.
|
51
|
47
|
*/
|
52
|
|
- public QueueItem(final String line, final QueuePriority priority) {
|
53
|
|
- this(Clock.systemDefaultZone(), line, priority);
|
54
|
|
- }
|
|
48
|
+ public abstract String getLine();
|
55
|
49
|
|
56
|
50
|
/**
|
57
|
|
- * Create a new QueueItem.
|
|
51
|
+ * Gets the time at which this item was queued.
|
58
|
52
|
*
|
59
|
|
- * @param line Line to send
|
60
|
|
- * @param priority Priority for the queue item
|
|
53
|
+ * @return the time this item was queued.
|
61
|
54
|
*/
|
62
|
|
- public QueueItem(final Clock clock, final String line,
|
63
|
|
- final QueuePriority priority) {
|
64
|
|
- this.line = line;
|
65
|
|
- this.priority = priority;
|
66
|
|
-
|
67
|
|
- this.time = LocalDateTime.now(clock);
|
68
|
|
- this.itemNumber = number++;
|
69
|
|
- }
|
|
55
|
+ public abstract LocalDateTime getTime();
|
70
|
56
|
|
71
|
57
|
/**
|
72
|
|
- * Get the value of line.
|
|
58
|
+ * Get the number of this item.
|
73
|
59
|
*
|
74
|
|
- * @return the value of line
|
|
60
|
+ * @return the number of this item (representing the order it was queued in).
|
75
|
61
|
*/
|
76
|
|
- public String getLine() {
|
77
|
|
- return line;
|
78
|
|
- }
|
|
62
|
+ public abstract long getItemNumber();
|
79
|
63
|
|
80
|
64
|
/**
|
81
|
|
- * Gets the time at which this item was queued.
|
|
65
|
+ * Get the priority of this item.
|
82
|
66
|
*
|
83
|
|
- * @return the time this item was queued.
|
|
67
|
+ * @return the item priority
|
84
|
68
|
*/
|
85
|
|
- public LocalDateTime getTime() {
|
86
|
|
- return time;
|
|
69
|
+ public abstract QueuePriority getPriority();
|
|
70
|
+
|
|
71
|
+ @Override
|
|
72
|
+ public String toString() {
|
|
73
|
+ return String.format("[%s %s] %s", getPriority(), getTime(), getLine());
|
87
|
74
|
}
|
88
|
75
|
|
89
|
76
|
/**
|
90
|
|
- * Get the number of this item.
|
|
77
|
+ * Creates a new {@link QueueItem}.
|
91
|
78
|
*
|
92
|
|
- * @return the value of itemNumber
|
|
79
|
+ * @param line The line to be sent to the server.
|
|
80
|
+ * @param priority The priority of the line.
|
|
81
|
+ * @return A new item to be submitted to the queue.
|
93
|
82
|
*/
|
94
|
|
- public long getItemNumber() {
|
95
|
|
- return itemNumber;
|
|
83
|
+ public static QueueItem create(final String line, final QueuePriority priority) {
|
|
84
|
+ return create(Clock.systemDefaultZone(), line, priority);
|
96
|
85
|
}
|
97
|
86
|
|
98
|
87
|
/**
|
99
|
|
- * Get the value of priority.
|
|
88
|
+ * Creates a new {@link QueueItem}.
|
100
|
89
|
*
|
101
|
|
- * @return the value of priority
|
|
90
|
+ * @param clock The clock to use to get the current time.
|
|
91
|
+ * @param line The line to be sent to the server.
|
|
92
|
+ * @param priority The priority of the line.
|
|
93
|
+ * @return A new item to be submitted to the queue.
|
102
|
94
|
*/
|
103
|
|
- public QueuePriority getPriority() {
|
104
|
|
- return priority;
|
105
|
|
- }
|
106
|
|
-
|
107
|
|
- @Override
|
108
|
|
- public String toString() {
|
109
|
|
- return String.format("[%s %s] %s", priority, time, line);
|
|
95
|
+ public static QueueItem create(
|
|
96
|
+ final Clock clock, final String line, final QueuePriority priority) {
|
|
97
|
+ return new AutoValue_QueueItem(line, LocalDateTime.now(clock),
|
|
98
|
+ itemCounter.getAndIncrement(), priority);
|
110
|
99
|
}
|
111
|
100
|
|
112
|
101
|
}
|