1 /** 2 * ServingXML 3 * 4 * Copyright (C) 2006 Daniel Parker 5 * daniel.parker@servingxml.com 6 * 7 * Licensed under the Apache License, Version 2.0 (the "License"); 8 * you may not use this file except in compliance with the License. 9 * You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, software 14 * distributed under the License is distributed on an "AS IS" BASIS, 15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 * See the License for the specific language governing permissions and 17 * limitations under the License. 18 * 19 **/ 20 21 package com.servingxml.util; 22 23 import java.util.ArrayList; 24 import java.util.List; 25 import java.util.Iterator; 26 27 public class Stack<T> { 28 private final List<T> list = new ArrayList<T>(); 29 push(T o)30 public final void push(T o) { 31 list.add(o); 32 } 33 pop()34 public final T pop() { 35 int index = list.size() - 1; 36 return list.remove(index); 37 } 38 enqueue(T o)39 public final void enqueue(T o) { 40 list.add(0,o); 41 } 42 dequeue()43 public final T dequeue() { 44 return list.remove(0); 45 } 46 peek()47 public final T peek() { 48 int index = list.size() - 1; 49 return list.get(index); 50 } 51 peek(int i)52 public T peek(int i) { 53 int index = list.size() - 1 - i; 54 return list.get(i); 55 } 56 empty()57 public final boolean empty() { 58 return list.size() == 0; 59 } 60 iterator()61 public Iterator iterator() { 62 return list.iterator(); 63 } 64 size()65 public int size() { 66 return list.size(); 67 } 68 } 69 70