Browse Source

Merge pull request #130 from csmith/master

Make QueueItem an autovalue.
pull/131/head
Greg Holmes 7 years ago
parent
commit
05dbd4ea49

+ 1
- 1
irc/src/com/dmdirc/parser/irc/outputqueue/OutputQueue.java View File

205
      * @param priority The priority at which the line should be sent.
205
      * @param priority The priority at which the line should be sent.
206
      */
206
      */
207
     protected void enqueue(final String line, final QueuePriority priority) {
207
     protected void enqueue(final String line, final QueuePriority priority) {
208
-        queue.add(new QueueItem(line, priority));
208
+        queue.add(QueueItem.create(line, priority));
209
     }
209
     }
210
 
210
 
211
     /**
211
     /**

+ 39
- 50
irc/src/com/dmdirc/parser/irc/outputqueue/QueueItem.java View File

24
 
24
 
25
 import com.dmdirc.parser.common.QueuePriority;
25
 import com.dmdirc.parser.common.QueuePriority;
26
 
26
 
27
+import com.google.auto.value.AutoValue;
28
+
27
 import java.time.Clock;
29
 import java.time.Clock;
28
 import java.time.LocalDateTime;
30
 import java.time.LocalDateTime;
31
+import java.util.concurrent.atomic.AtomicLong;
29
 
32
 
30
 /**
33
 /**
31
  * Queued Item.
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
 }

+ 10
- 10
irc/test/com/dmdirc/parser/irc/outputqueue/QueueComparatorsTest.java View File

45
     private static final Clock NOW_MINUS_TEN = Clock.fixed(Instant.ofEpochMilli(90 * 1000),
45
     private static final Clock NOW_MINUS_TEN = Clock.fixed(Instant.ofEpochMilli(90 * 1000),
46
             ZoneId.systemDefault());
46
             ZoneId.systemDefault());
47
 
47
 
48
-    private final QueueItem currentLowPriority1 = new QueueItem(NOW_MINUS_FIVE,
49
-            "", QueuePriority.LOW);
48
+    private final QueueItem currentLowPriority1 = QueueItem.create(
49
+            NOW_MINUS_FIVE, "", QueuePriority.LOW);
50
 
50
 
51
-    private final QueueItem currentLowPriority2 = new QueueItem(NOW_MINUS_FIVE,
52
-            "", QueuePriority.LOW);
51
+    private final QueueItem currentLowPriority2 = QueueItem.create(
52
+            NOW_MINUS_FIVE, "", QueuePriority.LOW);
53
 
53
 
54
-    private final QueueItem currentNormalPriority = new QueueItem(NOW_MINUS_FIVE,
55
-            "", QueuePriority.NORMAL);
54
+    private final QueueItem currentNormalPriority = QueueItem.create(
55
+            NOW_MINUS_FIVE, "", QueuePriority.NORMAL);
56
 
56
 
57
-    private final QueueItem currentHighPriority = new QueueItem(NOW_MINUS_FIVE,
58
-            "", QueuePriority.HIGH);
57
+    private final QueueItem currentHighPriority = QueueItem.create(
58
+            NOW_MINUS_FIVE, "", QueuePriority.HIGH);
59
 
59
 
60
-    private final QueueItem oldLowPriority = new QueueItem(NOW_MINUS_TEN,
61
-            "", QueuePriority.LOW);
60
+    private final QueueItem oldLowPriority = QueueItem.create(
61
+            NOW_MINUS_TEN, "", QueuePriority.LOW);
62
 
62
 
63
     @Test
63
     @Test
64
     public void testComparesByPriority() {
64
     public void testComparesByPriority() {

Loading…
Cancel
Save