|
@@ -0,0 +1,101 @@
|
|
1
|
+/*
|
|
2
|
+ * Copyright (c) 2006-2010 Chris Smith, Shane Mc Cormack, Gregory Holmes
|
|
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.util;
|
|
24
|
+
|
|
25
|
+import java.util.Iterator;
|
|
26
|
+import java.util.LinkedHashSet;
|
|
27
|
+import java.util.NoSuchElementException;
|
|
28
|
+import java.util.Queue;
|
|
29
|
+
|
|
30
|
+/**
|
|
31
|
+ * A LinkedHashSet with a Queue implementation added, also supports readding
|
|
32
|
+ * unique items to the head of the queue.
|
|
33
|
+ *
|
|
34
|
+ * @param <E> the type of elements held in this collection
|
|
35
|
+ */
|
|
36
|
+public class QueuedLinkedHashSet<E> extends LinkedHashSet<E>
|
|
37
|
+ implements Queue<E> {
|
|
38
|
+
|
|
39
|
+ /** {@inheritDoc} */
|
|
40
|
+ @Override
|
|
41
|
+ public boolean offer(final E e) {
|
|
42
|
+ return add(e);
|
|
43
|
+ }
|
|
44
|
+
|
|
45
|
+ /** {@inheritDoc} */
|
|
46
|
+ @Override
|
|
47
|
+ public E remove() {
|
|
48
|
+ if (isEmpty()) {
|
|
49
|
+ throw new NoSuchElementException("Queue is empty.");
|
|
50
|
+ }
|
|
51
|
+ return poll();
|
|
52
|
+ }
|
|
53
|
+
|
|
54
|
+ /** {@inheritDoc} */
|
|
55
|
+ @Override
|
|
56
|
+ public E poll() {
|
|
57
|
+ final E object = peek();
|
|
58
|
+ if (object != null) {
|
|
59
|
+ remove(object);
|
|
60
|
+ }
|
|
61
|
+ return object;
|
|
62
|
+ }
|
|
63
|
+
|
|
64
|
+ /** {@inheritDoc} */
|
|
65
|
+ @Override
|
|
66
|
+ public E element() {
|
|
67
|
+ if (isEmpty()) {
|
|
68
|
+ throw new NoSuchElementException("Queue is empty.");
|
|
69
|
+ }
|
|
70
|
+ return peek();
|
|
71
|
+ }
|
|
72
|
+
|
|
73
|
+ /** {@inheritDoc} */
|
|
74
|
+ @Override
|
|
75
|
+ public E peek() {
|
|
76
|
+ if (isEmpty()) {
|
|
77
|
+ return null;
|
|
78
|
+ }
|
|
79
|
+ final Iterator<E> iterator = iterator();
|
|
80
|
+ E object = null;
|
|
81
|
+ while (iterator.hasNext()) {
|
|
82
|
+ object = iterator.next();
|
|
83
|
+ }
|
|
84
|
+ return object;
|
|
85
|
+ }
|
|
86
|
+
|
|
87
|
+ /**
|
|
88
|
+ * Offers an item to this queue, if the item exists in the queue it removes
|
|
89
|
+ * it and re-adds it to the queue.
|
|
90
|
+ *
|
|
91
|
+ * @param e Object to add
|
|
92
|
+ *
|
|
93
|
+ * @return true iif the item was added
|
|
94
|
+ */
|
|
95
|
+ public boolean offerAndMove(final E e) {
|
|
96
|
+ if (contains(e)) {
|
|
97
|
+ remove(e);
|
|
98
|
+ }
|
|
99
|
+ return offer(e);
|
|
100
|
+ }
|
|
101
|
+}
|