polishing
This commit is contained in:
parent
7479e7b5a0
commit
9ba617bbf6
@ -11,7 +11,7 @@ public class CircularBuffer<E> {
|
|||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public CircularBuffer(int capacity) {
|
public CircularBuffer(int capacity) {
|
||||||
|
|
||||||
this.capacity = (capacity < 1 ? DEFAULT_CAPACITY : capacity);
|
this.capacity = (capacity < 1) ? DEFAULT_CAPACITY : capacity;
|
||||||
this.data = (E[]) new Object[capacity];
|
this.data = (E[]) new Object[capacity];
|
||||||
|
|
||||||
this.readSequence = 0;
|
this.readSequence = 0;
|
||||||
@ -20,7 +20,7 @@ public class CircularBuffer<E> {
|
|||||||
|
|
||||||
public boolean offer(E element) {
|
public boolean offer(E element) {
|
||||||
|
|
||||||
if (!isFull()) {
|
if (isNotFull()) {
|
||||||
|
|
||||||
int nextWriteSeq = writeSequence + 1;
|
int nextWriteSeq = writeSequence + 1;
|
||||||
data[nextWriteSeq % capacity] = element;
|
data[nextWriteSeq % capacity] = element;
|
||||||
@ -34,7 +34,7 @@ public class CircularBuffer<E> {
|
|||||||
|
|
||||||
public E poll() {
|
public E poll() {
|
||||||
|
|
||||||
if (!isEmpty()) {
|
if (isNotEmpty()) {
|
||||||
|
|
||||||
E nextValue = data[readSequence % capacity];
|
E nextValue = data[readSequence % capacity];
|
||||||
readSequence += 1;
|
readSequence += 1;
|
||||||
@ -62,4 +62,14 @@ public class CircularBuffer<E> {
|
|||||||
|
|
||||||
return size() >= capacity;
|
return size() >= capacity;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean isNotEmpty() {
|
||||||
|
|
||||||
|
return !isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isNotFull() {
|
||||||
|
|
||||||
|
return !isFull();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,14 +10,30 @@ import org.junit.jupiter.api.Test;
|
|||||||
public class CircularBufferUnitTest {
|
public class CircularBufferUnitTest {
|
||||||
|
|
||||||
private final String[] shapes = { "Circle", "Triangle", "Rectangle", "Square", "Rhombus", "Trapezoid", "Pentagon", "Pentagram", "Hexagon", "Hexagram" };
|
private final String[] shapes = { "Circle", "Triangle", "Rectangle", "Square", "Rhombus", "Trapezoid", "Pentagon", "Pentagram", "Hexagon", "Hexagram" };
|
||||||
|
private final int defaultCapacity = shapes.length;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenCircularBuffer_whenAnElementIsAddedAndRemoved_thenBufferIsEmpty() {
|
public void givenCircularBuffer_whenAnElementIsEnqueued_thenSizeIsOne() {
|
||||||
|
CircularBuffer<String> buffer = new CircularBuffer<>(defaultCapacity);
|
||||||
|
|
||||||
int capacity = 2;
|
assertTrue(buffer.offer("Square"));
|
||||||
CircularBuffer<String> buffer = new CircularBuffer<>(capacity);
|
assertEquals(1, buffer.size());
|
||||||
|
}
|
||||||
|
|
||||||
assertEquals(capacity, buffer.capacity());
|
@Test
|
||||||
|
public void givenCircularBuffer_whenAnElementIsDequeued_thenElementMatchesEnqueuedElement() {
|
||||||
|
CircularBuffer<String> buffer = new CircularBuffer<>(defaultCapacity);
|
||||||
|
|
||||||
|
buffer.offer("Triangle");
|
||||||
|
|
||||||
|
String shape = buffer.poll();
|
||||||
|
assertEquals("Triangle", shape);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenCircularBuffer_whenAnElementIsEnqueuedAndDeququed_thenBufferIsEmpty() {
|
||||||
|
|
||||||
|
CircularBuffer<String> buffer = new CircularBuffer<>(defaultCapacity);
|
||||||
|
|
||||||
buffer.offer("Rectangle");
|
buffer.offer("Rectangle");
|
||||||
|
|
||||||
@ -30,7 +46,7 @@ public class CircularBufferUnitTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenCircularBuffer_whenFilledToCapacity_thenNoMoreElementsCanBeAdded() {
|
public void givenCircularBuffer_whenFilledToCapacity_thenNoMoreElementsCanBeEnqueued() {
|
||||||
|
|
||||||
int capacity = shapes.length;
|
int capacity = shapes.length;
|
||||||
CircularBuffer<String> buffer = new CircularBuffer<>(capacity);
|
CircularBuffer<String> buffer = new CircularBuffer<>(capacity);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user