package org.warp.commonutils.type; import java.util.ArrayList; import java.util.Collection; import java.util.List; import org.warp.commonutils.error.IndexOutOfBoundsException; /** * A stack * *

A stack must provide the classical {@link #push(Object)} and * {@link #pop()} operations, but may be also peekable to some extent: it may provide just the {@link #top()} * function, or even a more powerful {@link #peek(int)} method that provides access to all elements on the stack * (indexed from the top, which has index 0). */ public interface Stack extends it.unimi.dsi.fastutil.Stack { /** * Pop multiple times * @param count the number of times to pop * @return list of popped elements */ default List pop(int count) { if (count < 0) { throw new IndexOutOfBoundsException(count); } var items = new ArrayList(count); for (int i = 0; i < count; i++) { items.add(this.pop()); } return items; } static Stack create() { return new ArrayStack<>(); } static & Collection> Stack wrap(U stack) { return new FastUtilStackWrapper<>(stack); } }