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