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