Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d20627f5 authored by Sudheer Shanka's avatar Sudheer Shanka
Browse files

Add createNewItem() to RingBuffer util class.

Test: atest tests/net/java/com/android/internal/util/RingBufferTest.java
Change-Id: Idec1f354fc702ead0603341806c6c4ff863c0522
parent dd312aba
Loading
Loading
Loading
Loading
+14 −9
Original line number Diff line number Diff line
@@ -67,16 +67,21 @@ public class RingBuffer<T> {
     */
    public T getNextSlot() {
        final int nextSlotIdx = indexOf(mCursor++);
        T item = mBuffer[nextSlotIdx];
        if (item == null) {
        if (mBuffer[nextSlotIdx] == null) {
            mBuffer[nextSlotIdx] = createNewItem();
        }
        return mBuffer[nextSlotIdx];
    }

    /**
     * @return a new object of type <T> or null if a new object could not be created.
     */
    protected T createNewItem() {
        try {
                item = (T) mBuffer.getClass().getComponentType().newInstance();
            return (T) mBuffer.getClass().getComponentType().newInstance();
        } catch (IllegalAccessException | InstantiationException e) {
            return null;
        }
            mBuffer[nextSlotIdx] = item;
        }
        return item;
    }

    public T[] toArray() {