44 lines
1.1 KiB
Java
44 lines
1.1 KiB
Java
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
|
|
*
|
|
* <p>A stack must provide the classical {@link #push(Object)} and
|
|
* {@link #pop()} operations, but may be also <em>peekable</em> 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<K> extends it.unimi.dsi.fastutil.Stack<K> {
|
|
|
|
/**
|
|
* Pop multiple times
|
|
* @param count the number of times to pop
|
|
* @return list of popped elements
|
|
*/
|
|
default List<K> pop(int count) {
|
|
if (count < 0) {
|
|
throw new IndexOutOfBoundsException(count);
|
|
}
|
|
var items = new ArrayList<K>(count);
|
|
for (int i = 0; i < count; i++) {
|
|
items.add(this.pop());
|
|
}
|
|
return items;
|
|
}
|
|
|
|
static <T> Stack<T> create() {
|
|
return new ArrayStack<>();
|
|
}
|
|
|
|
static <T, U extends Stack<T> & Collection<T>> Stack<T> wrap(U stack) {
|
|
return new FastUtilStackWrapper<>(stack);
|
|
}
|
|
|
|
}
|