2
2
3
3
import java .util .concurrent .atomic .AtomicInteger ;
4
4
5
+ /**
6
+ * The {@code CircularBuffer} class implements a generic circular (or ring) buffer.
7
+ * A circular buffer is a fixed-size data structure that operates in a FIFO (First In, First Out) manner.
8
+ * The buffer allows you to overwrite old data when the buffer is full and efficiently use limited memory.
9
+ *
10
+ * @param <Item> The type of elements stored in the circular buffer.
11
+ */
5
12
public class CircularBuffer <Item > {
6
13
private final Item [] buffer ;
7
14
private final CircularPointer putPointer ;
8
15
private final CircularPointer getPointer ;
9
16
private final AtomicInteger size = new AtomicInteger (0 );
10
17
18
+ /**
19
+ * Constructor to initialize the circular buffer with a specified size.
20
+ *
21
+ * @param size The size of the circular buffer.
22
+ */
11
23
public CircularBuffer (int size ) {
12
24
// noinspection unchecked
13
25
this .buffer = (Item []) new Object [size ];
14
26
this .putPointer = new CircularPointer (0 , size );
15
27
this .getPointer = new CircularPointer (0 , size );
16
28
}
17
29
30
+ /**
31
+ * Checks if the circular buffer is empty.
32
+ *
33
+ * @return {@code true} if the buffer is empty, {@code false} otherwise.
34
+ */
18
35
public boolean isEmpty () {
19
36
return size .get () == 0 ;
20
37
}
21
38
39
+ /**
40
+ * Checks if the circular buffer is full.
41
+ *
42
+ * @return {@code true} if the buffer is full, {@code false} otherwise.
43
+ */
22
44
public boolean isFull () {
23
45
return size .get () == buffer .length ;
24
46
}
25
47
48
+ /**
49
+ * Retrieves and removes the item at the front of the buffer (FIFO).
50
+ *
51
+ * @return The item at the front of the buffer, or {@code null} if the buffer is empty.
52
+ */
26
53
public Item get () {
27
54
if (isEmpty ()) {
28
55
return null ;
@@ -33,6 +60,12 @@ public Item get() {
33
60
return item ;
34
61
}
35
62
63
+ /**
64
+ * Adds an item to the end of the buffer (FIFO).
65
+ *
66
+ * @param item The item to be added.
67
+ * @return {@code true} if the item was successfully added, or {@code false} if the buffer is full.
68
+ */
36
69
public boolean put (Item item ) {
37
70
if (isFull ()) {
38
71
return false ;
@@ -43,15 +76,30 @@ public boolean put(Item item) {
43
76
return true ;
44
77
}
45
78
79
+ /**
80
+ * The {@code CircularPointer} class is a helper class used to track the current index (pointer)
81
+ * in the circular buffer. It automatically wraps around when reaching the maximum index.
82
+ */
46
83
private static class CircularPointer {
47
84
private int pointer ;
48
85
private final int max ;
49
86
87
+ /**
88
+ * Constructor to initialize the circular pointer.
89
+ *
90
+ * @param pointer The initial position of the pointer.
91
+ * @param max The maximum size (capacity) of the circular buffer.
92
+ */
50
93
CircularPointer (int pointer , int max ) {
51
94
this .pointer = pointer ;
52
95
this .max = max ;
53
96
}
54
97
98
+ /**
99
+ * Increments the pointer by 1 and wraps it around to 0 if it exceeds the maximum value.
100
+ *
101
+ * @return The current pointer value before incrementing.
102
+ */
55
103
public int getAndIncrement () {
56
104
if (pointer == max ) {
57
105
pointer = 0 ;
0 commit comments