|
|
@@ -1,17 +1,17 @@
|
|
1
|
1
|
import java.util.LinkedList;
|
|
2
|
2
|
import java.util.Queue;
|
|
3
|
3
|
|
|
4
|
|
-public class CircularBuffer<T> {
|
|
|
4
|
+class CircularBuffer<T> {
|
|
5
|
5
|
|
|
6
|
6
|
private Queue<T> buffer;
|
|
7
|
7
|
private int capacity;
|
|
8
|
8
|
|
|
9
|
|
- public CircularBuffer(int size) {
|
|
|
9
|
+ CircularBuffer(int size) {
|
|
10
|
10
|
this.buffer = new LinkedList<>();
|
|
11
|
11
|
this.capacity = size;
|
|
12
|
12
|
}
|
|
13
|
13
|
|
|
14
|
|
- public T read() throws BufferIOException {
|
|
|
14
|
+ T read() throws BufferIOException {
|
|
15
|
15
|
if (this.buffer.size() == 0) {
|
|
16
|
16
|
throw new BufferIOException("Tried to read from empty buffer");
|
|
17
|
17
|
}
|
|
|
@@ -19,14 +19,14 @@ public class CircularBuffer<T> {
|
|
19
|
19
|
return this.buffer.remove();
|
|
20
|
20
|
}
|
|
21
|
21
|
|
|
22
|
|
- public void write(T data) throws BufferIOException {
|
|
|
22
|
+ void write(T data) throws BufferIOException {
|
|
23
|
23
|
if (this.buffer.size() == this.capacity) {
|
|
24
|
24
|
throw new BufferIOException("Tried to write to full buffer");
|
|
25
|
25
|
}
|
|
26
|
26
|
|
|
27
|
27
|
this.buffer.add(data);
|
|
28
|
28
|
}
|
|
29
|
|
- public void overwrite(T data) {
|
|
|
29
|
+ void overwrite(T data) {
|
|
30
|
30
|
if (this.buffer.size() == this.capacity) {
|
|
31
|
31
|
this.buffer.remove();
|
|
32
|
32
|
}
|
|
|
@@ -34,7 +34,7 @@ public class CircularBuffer<T> {
|
|
34
|
34
|
this.buffer.add(data);
|
|
35
|
35
|
}
|
|
36
|
36
|
|
|
37
|
|
- public void clear() {
|
|
|
37
|
+ void clear() {
|
|
38
|
38
|
this.buffer.clear();
|
|
39
|
39
|
}
|
|
40
|
40
|
}
|