diff options
Diffstat (limited to 'com/android/internal/util/RingBuffer.java')
-rw-r--r-- | com/android/internal/util/RingBuffer.java | 23 |
1 files changed, 14 insertions, 9 deletions
diff --git a/com/android/internal/util/RingBuffer.java b/com/android/internal/util/RingBuffer.java index 9a6e542c..8fc4c30e 100644 --- a/com/android/internal/util/RingBuffer.java +++ b/com/android/internal/util/RingBuffer.java @@ -67,16 +67,21 @@ public class RingBuffer<T> { */ public T getNextSlot() { final int nextSlotIdx = indexOf(mCursor++); - T item = mBuffer[nextSlotIdx]; - if (item == null) { - try { - item = (T) mBuffer.getClass().getComponentType().newInstance(); - } catch (IllegalAccessException | InstantiationException e) { - return null; - } - mBuffer[nextSlotIdx] = item; + 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 { + return (T) mBuffer.getClass().getComponentType().newInstance(); + } catch (IllegalAccessException | InstantiationException e) { + return null; } - return item; } public T[] toArray() { |