Skip to content

Commit

Permalink
change array type; move cast from access to array creation time
Browse files Browse the repository at this point in the history
  • Loading branch information
xzel23 committed Mar 12, 2024
1 parent 41f7bf6 commit b392e27
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions utility/src/main/java/com/dua3/utility/lang/RingBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
*/
public class RingBuffer<E> implements Collection<E> {

private Object[] data;
private E[] data;
private int entries;
private int start;

Expand All @@ -38,8 +38,9 @@ public class RingBuffer<E> implements Collection<E> {
*
* @param capacity the initial capacity
*/
@SuppressWarnings("unchecked")
public RingBuffer(int capacity) {
data = new Object[capacity];
data = (E[]) new Object[capacity];
start = 0;
entries = 0;
}
Expand All @@ -57,7 +58,8 @@ public boolean add(@Nullable E item) {
}

/**
* Add item to end of collection.
* Add an item at the end of this collection. If the item count has reached the capacity, the first item will be
* removed.
*
* @param item the item to add
* @return true, if the buffer size increased as a result of this operation (in other words, false if an item
Expand Down Expand Up @@ -143,10 +145,9 @@ public void clear() {
* @param i index
* @return the i-th element
*/
@SuppressWarnings("unchecked")
public E get(int i) {
checkIndex(i);
return (E) data[index(i)];
return data[index(i)];
}

/**
Expand Down Expand Up @@ -235,9 +236,10 @@ public <T> T[] toArray(T[] a) {
*
* @param n the new capacity.
*/
@SuppressWarnings("unchecked")
public void setCapacity(int n) {
if (n != capacity()) {
Object[] dataNew = new Object[n];
E[] dataNew = (E[]) new Object[n];
int itemsToCopy = Math.min(size(), n);
int startIndex = Math.max(0, size() - n);
for (int i = 0; i < itemsToCopy; i++) {
Expand Down

0 comments on commit b392e27

Please sign in to comment.