KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > util > collection > SimpleStack


1 // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
2

3 package jodd.util.collection;
4
5 import java.util.LinkedList JavaDoc;
6
7 /**
8  * Simple Stack (LIFO) class.
9  */

10 public class SimpleStack {
11
12     private LinkedList JavaDoc list = new LinkedList JavaDoc();
13
14     /**
15      * Stack push.
16      *
17      * @param o
18      */

19     public void push(Object JavaDoc o) {
20         list.addLast(o);
21     }
22
23     /**
24      * Stack pop.
25      *
26      * @return poped object from stack
27      */

28     public Object JavaDoc pop() {
29         if (list.isEmpty()) {
30             return null;
31         }
32         return list.removeLast();
33     }
34
35
36     public Object JavaDoc[] popAll() {
37         Object JavaDoc[] res = new Object JavaDoc[list.size()];
38         for (int i = 0; i < res.length; i++) {
39             res[i] = list.get(i);
40         }
41         list.clear();
42         return res;
43     }
44
45     /**
46      * Peek element from stack.
47      *
48      * @return peeked object
49      */

50     public Object JavaDoc peek() {
51         return list.getLast();
52     }
53
54
55     /**
56      * Is stack empty?
57      *
58      * @return true if stack is empty
59      */

60     public boolean isEmpty() {
61         return list.isEmpty();
62     }
63
64     /**
65      * Returns stack size.
66      *
67      * @return stack size
68      */

69     public int size() {
70         return list.size();
71     }
72
73 }
74
Popular Tags