@@ -52,7 +52,7 @@ public T next() {
5252
5353 /**
5454 * @param initialCapacity the initial capacity of the underlying collection.
55- * @param elementFactory the factory used to create a new element on {@link #push()} when the queue has
55+ * @param elementFactory the factory used to create a new element on {@link #push(Recycler )} when the queue has
5656 * not previously grown to the new depth.
5757 */
5858 public _Private_RecyclingQueue (int initialCapacity , ElementFactory <T > elementFactory ) {
@@ -73,7 +73,7 @@ public T get(int index) {
7373 /**
7474 * Pushes an element onto the top of the queue, instantiating a new element only if the queue has not
7575 * previously grown to the new depth.
76- * @return the element at the top of the queue after the push. This element must be initialized by the caller.
76+ * @return the index of the element at the top of the queue after the push. This element must be initialized by the caller.
7777 */
7878 public int push (Recycler <T > recycler ) {
7979 currentIndex ++;
@@ -87,6 +87,23 @@ public int push(Recycler<T> recycler) {
8787 return currentIndex ;
8888 }
8989
90+ /**
91+ * Pushes an element onto the top of the queue, instantiating a new element only if the queue has not
92+ * previously grown to the new depth.
93+ * @return the element at the top of the queue after the push.
94+ */
95+ public T pushAndGet (Recycler <T > recycler ) {
96+ currentIndex ++;
97+ if (currentIndex >= elements .size ()) {
98+ top = elementFactory .newElement ();
99+ elements .add (top );
100+ } else {
101+ top = elements .get (currentIndex );
102+ }
103+ recycler .recycle (top );
104+ return top ;
105+ }
106+
90107 /**
91108 * Reclaim the current element.
92109 */
@@ -119,4 +136,4 @@ public void clear() {
119136 public int size () {
120137 return currentIndex + 1 ;
121138 }
122- }
139+ }
0 commit comments