22
33import java .util .concurrent .atomic .AtomicInteger ;
44
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+ */
512public class CircularBuffer <Item > {
613 private final Item [] buffer ;
714 private final CircularPointer putPointer ;
815 private final CircularPointer getPointer ;
916 private final AtomicInteger size = new AtomicInteger (0 );
1017
18+ /**
19+ * Constructor to initialize the circular buffer with a specified size.
20+ *
21+ * @param size The size of the circular buffer.
22+ */
1123 public CircularBuffer (int size ) {
1224 // noinspection unchecked
1325 this .buffer = (Item []) new Object [size ];
1426 this .putPointer = new CircularPointer (0 , size );
1527 this .getPointer = new CircularPointer (0 , size );
1628 }
1729
30+ /**
31+ * Checks if the circular buffer is empty.
32+ *
33+ * @return {@code true} if the buffer is empty, {@code false} otherwise.
34+ */
1835 public boolean isEmpty () {
1936 return size .get () == 0 ;
2037 }
2138
39+ /**
40+ * Checks if the circular buffer is full.
41+ *
42+ * @return {@code true} if the buffer is full, {@code false} otherwise.
43+ */
2244 public boolean isFull () {
2345 return size .get () == buffer .length ;
2446 }
2547
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+ */
2653 public Item get () {
2754 if (isEmpty ()) {
2855 return null ;
@@ -33,6 +60,12 @@ public Item get() {
3360 return item ;
3461 }
3562
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+ */
3669 public boolean put (Item item ) {
3770 if (isFull ()) {
3871 return false ;
@@ -43,15 +76,30 @@ public boolean put(Item item) {
4376 return true ;
4477 }
4578
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+ */
4683 private static class CircularPointer {
4784 private int pointer ;
4885 private final int max ;
4986
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+ */
5093 CircularPointer (int pointer , int max ) {
5194 this .pointer = pointer ;
5295 this .max = max ;
5396 }
5497
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+ */
55103 public int getAndIncrement () {
56104 if (pointer == max ) {
57105 pointer = 0 ;
0 commit comments