1 16 package org.apache.commons.collections; 17 18 import java.util.ArrayList ; 19 import java.util.EmptyStackException ; 20 21 44 public class ArrayStack extends ArrayList implements Buffer { 45 46 47 private static final long serialVersionUID = 2130079159931574599L; 48 49 53 public ArrayStack() { 54 super(); 55 } 56 57 64 public ArrayStack(int initialSize) { 65 super(initialSize); 66 } 67 68 76 public boolean empty() { 77 return isEmpty(); 78 } 79 80 86 public Object peek() throws EmptyStackException { 87 int n = size(); 88 if (n <= 0) { 89 throw new EmptyStackException (); 90 } else { 91 return get(n - 1); 92 } 93 } 94 95 104 public Object peek(int n) throws EmptyStackException { 105 int m = (size() - n) - 1; 106 if (m < 0) { 107 throw new EmptyStackException (); 108 } else { 109 return get(m); 110 } 111 } 112 113 119 public Object pop() throws EmptyStackException { 120 int n = size(); 121 if (n <= 0) { 122 throw new EmptyStackException (); 123 } else { 124 return remove(n - 1); 125 } 126 } 127 128 135 public Object push(Object item) { 136 add(item); 137 return item; 138 } 139 140 151 public int search(Object object) { 152 int i = size() - 1; int n = 1; while (i >= 0) { 155 Object current = get(i); 156 if ((object == null && current == null) || 157 (object != null && object.equals(current))) { 158 return n; 159 } 160 i--; 161 n++; 162 } 163 return -1; 164 } 165 166 172 public Object get() { 173 int size = size(); 174 if (size == 0) { 175 throw new BufferUnderflowException(); 176 } 177 return get(size - 1); 178 } 179 180 186 public Object remove() { 187 int size = size(); 188 if (size == 0) { 189 throw new BufferUnderflowException(); 190 } 191 return remove(size - 1); 192 } 193 194 } 195 | Popular Tags |