1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.aether.util.graph.visitor;
20
21 import java.util.AbstractList;
22 import java.util.NoSuchElementException;
23 import java.util.RandomAccess;
24
25
26
27
28
29 class Stack<E> extends AbstractList<E> implements RandomAccess {
30
31 @SuppressWarnings("unchecked")
32
33 private E[] elements = (E[]) new Object[96];
34
35
36 private int size;
37
38 public void push(E element) {
39 if (size >= elements.length) {
40 @SuppressWarnings("unchecked")
41 E[] tmp = (E[]) new Object[size + 64];
42 System.arraycopy(elements, 0, tmp, 0, elements.length);
43 elements = tmp;
44 }
45 elements[size++] = element;
46 }
47
48 public E pop() {
49 if (size <= 0) {
50 throw new NoSuchElementException();
51 }
52 return elements[--size];
53 }
54
55 public E peek() {
56 if (size <= 0) {
57 return null;
58 }
59 return elements[size - 1];
60 }
61
62 @Override
63 public E get(int index) {
64 if (index < 0 || index >= size) {
65 throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
66 }
67 return elements[size - index - 1];
68 }
69
70 @Override
71 public int size() {
72 return size;
73 }
74 }