com.amazon.ion.impl._Private_RecyclingQueue Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ion-java Show documentation
Show all versions of ion-java Show documentation
A Java implementation of the Amazon Ion data notation.
The newest version!
package com.amazon.ion.impl;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* A queue whose elements are recycled. This queue will be extended and iterated frequently.
* @param the type of elements stored.
*/
public class _Private_RecyclingQueue {
/**
* Factory for new queue elements.
* @param the type of element.
*/
public interface ElementFactory {
/**
* @return a new instance.
*/
T newElement();
}
@FunctionalInterface
public interface Recycler {
/**
* Re-initialize an element
*/
void recycle(T t);
}
/**
* Iterator for the queue.
*/
private class ElementIterator implements Iterator {
int i = 0;
@Override
public boolean hasNext() {
return i <= currentIndex;
}
@Override
public T next() {
return elements.get(i++);
}
}
private final ElementIterator iterator;
private final List elements;
private final ElementFactory elementFactory;
private int currentIndex;
private T top;
/**
* @param initialCapacity the initial capacity of the underlying collection.
* @param elementFactory the factory used to create a new element on {@link #push()} when the queue has
* not previously grown to the new depth.
*/
public _Private_RecyclingQueue(int initialCapacity, ElementFactory elementFactory) {
elements = new ArrayList(initialCapacity);
this.elementFactory = elementFactory;
currentIndex = -1;
iterator = new ElementIterator();
}
public void truncate(int index) {
currentIndex = index;
}
public T get(int index) {
return elements.get(index);
}
/**
* Pushes an element onto the top of the queue, instantiating a new element only if the queue has not
* previously grown to the new depth.
* @return the element at the top of the queue after the push. This element must be initialized by the caller.
*/
public int push(Recycler recycler) {
currentIndex++;
if (currentIndex >= elements.size()) {
top = elementFactory.newElement();
elements.add(top);
} else {
top = elements.get(currentIndex);
}
recycler.recycle(top);
return currentIndex;
}
/**
* Reclaim the current element.
*/
public void remove() {
currentIndex = Math.max(-1, currentIndex - 1);
}
public Iterator iterate() {
iterator.i = 0;
return iterator;
}
/**
* @return true if the queue is empty; otherwise, false.
*/
public boolean isEmpty() {
return currentIndex < 0;
}
/**
* Reset the index and make the queue reusable.
*/
public void clear() {
currentIndex = -1;
}
/**
* @return the number of elements within the queue.
*/
public int size() {
return currentIndex + 1;
}
}