forked from douglascraigschmidt/LiveLessons
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleQueue.java
More file actions
129 lines (117 loc) · 2.9 KB
/
SimpleQueue.java
File metadata and controls
129 lines (117 loc) · 2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import java.util.List;
import java.util.concurrent.CyclicBarrier;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;
/*
* @class SimpleQueue
*
* @brief Defines an implementation of the BlockingQueue interface
* that (intentially) doesn't work properly when accessed via
* multiple threads since it's not synchronized properly.
*/
class SimpleQueue<E> implements BlockingQueue<E> {
/**
* The queue consists of a List of E's.
*/
private List<E> mList = new ArrayList<E>();
/**
* True if the queue is empty.
*/
public boolean isEmpty() {
return mList.size() == 0;
}
/**
* Add a new E to the end of the queue.
*/
public void put(E msg) throws InterruptedException {
mList.add(msg);
}
/**
* Remove the E at the front of the queue.
*/
public E take() throws InterruptedException {
return mList.remove(0);
}
/**
* Returns the number of elements in this queue.
*/
public int size() {
return mList.size();
}
/**
* All these methods are inherited from the BlockingQueue
* interface. They are defined as no-ops to ensure the "Buggyness"
* of this class ;-)
*/
public int drainTo(Collection<? super E> c) {
return 0;
}
public int drainTo(Collection<? super E> c, int maxElements) {
return 0;
}
public boolean contains(Object o) {
return false;
}
public boolean remove(Object o) {
return false;
}
public int remainingCapacity() {
return 0;
}
public E poll() {
return null;
}
public E poll(long timeout, TimeUnit unit) throws InterruptedException {
return take();
}
public E peek() {
return null;
}
public boolean offer(E e) {
return false;
}
public boolean offer(E e, long timeout, TimeUnit unit) {
try {
put(e);
}
catch (InterruptedException ex) {
// Just swallow this exception for this simple (buggy) test.
}
return true;
}
public boolean add(E e) {
return false;
}
public E element() {
return null;
}
public E remove() {
return null;
}
public void clear() {
}
public boolean retainAll(Collection<?> collection) {
return false;
}
public boolean removeAll(Collection<?> collection) {
return false;
}
public boolean addAll(Collection<? extends E> collection) {
return false;
}
public boolean containsAll(Collection<?> collection) {
return false;
}
public Object[] toArray() {
return null;
}
public <T> T[] toArray(T[] array) {
return null;
}
public Iterator<E> iterator() {
return null;
}
}