polishing

This commit is contained in:
priyank-sriv
2020-06-18 02:25:41 +05:30
parent 7479e7b5a0
commit 9ba617bbf6
2 changed files with 34 additions and 8 deletions
@@ -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();
}
}