Skip to content

Commit

Permalink
Throw exception in circular buffer for invalid operations
Browse files Browse the repository at this point in the history
  • Loading branch information
calcmogul committed Nov 27, 2023
1 parent cbbeab2 commit 340d013
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions wpiutil/src/main/java/edu/wpi/first/util/CircularBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,13 @@ public T getFirst() {
* Get value at back of buffer.
*
* @return value at back of buffer
* @throws IndexOutOfBoundsException if the index is out of range (index < 0 || index >= size())
*/
@SuppressWarnings("unchecked")
public T getLast() {
// If there are no elements in the buffer, do nothing
if (m_length == 0) {
return (T) new Object();
throw new IndexOutOfBoundsException();
}

return m_data[(m_front + m_length - 1) % m_data.length];
Expand Down Expand Up @@ -102,12 +103,13 @@ public void addLast(T value) {
* Pop value at front of buffer.
*
* @return value at front of buffer
* @throws IndexOutOfBoundsException if the index is out of range (index < 0 || index >= size())
*/
@SuppressWarnings("unchecked")
public T removeFirst() {
// If there are no elements in the buffer, do nothing
if (m_length == 0) {
return (T) new Object();
throw new IndexOutOfBoundsException();
}

T temp = m_data[m_front];
Expand All @@ -120,12 +122,13 @@ public T removeFirst() {
* Pop value at back of buffer.
*
* @return value at back of buffer
* @throws IndexOutOfBoundsException if the index is out of range (index < 0 || index >= size())
*/
@SuppressWarnings("unchecked")
public T removeLast() {
// If there are no elements in the buffer, do nothing
if (m_length == 0) {
return (T) new Object();
throw new IndexOutOfBoundsException();
}

m_length--;
Expand Down

0 comments on commit 340d013

Please sign in to comment.