| 27 | import java.util.Arrays; |
| 28 | |
| 29 | public class ObjStack<T> implements Mutable { |
| 30 | public static final int DEFAULT_INITIAL_CAPACITY = 16; |
| 31 | private final int initialCapacity; |
| 32 | private int bottom; |
| 33 | private T[] elements; |
| 34 | private int head; |
| 35 | private int mask; |
| 36 | private int tail; |
| 37 | |
| 38 | public ObjStack() { |
| 39 | this(DEFAULT_INITIAL_CAPACITY); |
| 40 | } |
| 41 | |
| 42 | public ObjStack(int initialCapacity) { |
| 43 | this.initialCapacity = initialCapacity; |
| 44 | allocateElements(initialCapacity); |
| 45 | } |
| 46 | |
| 47 | public void clear() { |
| 48 | if (head != tail) { |
| 49 | head = tail = 0; |
| 50 | Arrays.fill(elements, null); |
| 51 | bottom = 0; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | public int getBottom() { |
| 56 | return bottom; |
| 57 | } |
| 58 | |
| 59 | public int getCapacity() { |
| 60 | return elements.length; |
| 61 | } |
| 62 | |
| 63 | public boolean notEmpty() { |
| 64 | return size() > 0; |
| 65 | } |
| 66 | |
| 67 | public T peek() { |
| 68 | return peek(0); |
| 69 | } |
| 70 | |
| 71 | public T peek(int n) { |
| 72 | return n < size() ? elements[(head + n) & mask] : null; |
| 73 | } |
| 74 | |
| 75 | public T pop() { |
| 76 | if (size() == 0) { |
| 77 | return null; |
| 78 | } |
| 79 | final int h = head; |
| 80 | final T result = elements[h]; |
| 81 | if (result == null) { |
| 82 | return null; |
| 83 | } |
| 84 | elements[h] = null; |
| 85 | head = (h + 1) & mask; |
| 86 | return result; |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…