| 1 | // Copyright 2004-2007 Jean-Francois Poilpret |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package net.sourceforge.hiveutils.collections.impl; |
| 16 | |
| 17 | import java.util.ArrayList; |
| 18 | import java.util.EmptyStackException; |
| 19 | import java.util.List; |
| 20 | |
| 21 | import net.sourceforge.hiveutils.collections.Stack; |
| 22 | |
| 23 | /** |
| 24 | * Implementation of <code>Stack</code>, based on <code>ArrayList</code>. |
| 25 | * |
| 26 | * @author Jean-Francois Poilpret |
| 27 | */ |
| 28 | public class StackImpl<T> implements Stack<T> |
| 29 | { |
| 30 | public void push(T o) |
| 31 | { |
| 32 | _stack.add(o); |
| 33 | } |
| 34 | |
| 35 | public T pop() |
| 36 | { |
| 37 | checkNotEmpty(); |
| 38 | return _stack.remove(_stack.size() - 1); |
| 39 | } |
| 40 | |
| 41 | public T peek() |
| 42 | { |
| 43 | checkNotEmpty(); |
| 44 | return _stack.get(_stack.size() - 1); |
| 45 | } |
| 46 | |
| 47 | public boolean isEmpty() |
| 48 | { |
| 49 | return _stack.isEmpty(); |
| 50 | } |
| 51 | |
| 52 | public void clear() |
| 53 | { |
| 54 | _stack.clear(); |
| 55 | } |
| 56 | |
| 57 | private void checkNotEmpty() |
| 58 | { |
| 59 | if (_stack.isEmpty()) |
| 60 | { |
| 61 | throw new EmptyStackException(); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | final private List<T> _stack = new ArrayList<T>(); |
| 66 | } |