1 18 package org.apache.tomcat.util.digester; 19 20 import java.util.ArrayList ; 21 import java.util.EmptyStackException ; 22 23 45 public class ArrayStack extends ArrayList { 46 47 48 private static final long serialVersionUID = 2130079159931574599L; 49 50 54 public ArrayStack() { 55 super(); 56 } 57 58 65 public ArrayStack(int initialSize) { 66 super(initialSize); 67 } 68 69 77 public boolean empty() { 78 return isEmpty(); 79 } 80 81 87 public Object peek() throws EmptyStackException { 88 int n = size(); 89 if (n <= 0) { 90 throw new EmptyStackException (); 91 } else { 92 return get(n - 1); 93 } 94 } 95 96 105 public Object peek(int n) throws EmptyStackException { 106 int m = (size() - n) - 1; 107 if (m < 0) { 108 throw new EmptyStackException (); 109 } else { 110 return get(m); 111 } 112 } 113 114 120 public Object pop() throws EmptyStackException { 121 int n = size(); 122 if (n <= 0) { 123 throw new EmptyStackException (); 124 } else { 125 return remove(n - 1); 126 } 127 } 128 129 136 public Object push(Object item) { 137 add(item); 138 return item; 139 } 140 141 142 153 public int search(Object object) { 154 int i = size() - 1; int n = 1; while (i >= 0) { 157 Object current = get(i); 158 if ((object == null && current == null) || 159 (object != null && object.equals(current))) { 160 return n; 161 } 162 i--; 163 n++; 164 } 165 return -1; 166 } 167 168 169 } 170 | Popular Tags |