KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > util > Stack


1 /**
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.util;
5
6 import java.util.ArrayList JavaDoc;
7 import java.util.EmptyStackException JavaDoc;
8 import java.util.Iterator JavaDoc;
9 import java.util.List JavaDoc;
10
11 /*
12  * This stack implementation uses ArrayList internally. This is mainly created so that we dont have synchronization
13  * overheads that java.util.Stack imposses since it is based on Vector. This class maintains an interface level compatibility
14  * with java.util.Stack but doesnot implement all of Vector interfaces.
15  */

16 public class Stack {
17
18   private final List JavaDoc list = new ArrayList JavaDoc();
19
20   /**
21    * Creates an empty Stack.
22    */

23   public Stack() {
24   }
25
26   /**
27    * Pushes an item onto the top of this stack. This has exactly the same effect as: <blockquote>
28    *
29    * @param item the item to be pushed onto this stack.
30    * @return the <code>item</code> argument.
31    * @see java.util.Vector#addElement
32    */

33   public Object JavaDoc push(Object JavaDoc item) {
34     list.add(item);
35     return item;
36   }
37
38   /**
39    * Removes the object at the top of this stack and returns that object as the value of this function.
40    *
41    * @return The object at the top of this stack (the last item of the <tt>Vector</tt> object).
42    * @exception EmptyStackException if this stack is empty.
43    */

44   public Object JavaDoc pop() {
45     int len = size();
46
47     if (len == 0) throw new EmptyStackException JavaDoc();
48     return list.remove(len - 1);
49   }
50
51   /**
52    * Looks at the object at the top of this stack without removing it from the stack.
53    *
54    * @return the object at the top of this stack (the last item of the <tt>Vector</tt> object).
55    * @exception EmptyStackException if this stack is empty.
56    */

57   public Object JavaDoc peek() {
58     int len = size();
59
60     if (len == 0) throw new EmptyStackException JavaDoc();
61     return list.get(len - 1);
62   }
63
64   /**
65    * Tests if this stack is empty.
66    *
67    * @return <code>true</code> if and only if this stack contains no items; <code>false</code> otherwise.
68    */

69   public boolean empty() {
70     return size() == 0;
71   }
72   
73   /**
74    * Size of this Stack
75    *
76    * @return the size of the stack
77    */

78   public int size() {
79     return list.size();
80   }
81
82   /**
83    * Returns the 1-based position where an object is on this stack. If the object <tt>o</tt> occurs as an item in this
84    * stack, this method returns the distance from the top of the stack of the occurrence nearest the top of the stack;
85    * the topmost item on the stack is considered to be at distance <tt>1</tt>. The <tt>equals</tt> method is used
86    * to compare <tt>o</tt> to the items in this stack.
87    *
88    * @param o the desired object.
89    * @return the 1-based position from the top of the stack where the object is located; the return value
90    * <code>-1</code> indicates that the object is not on the stack.
91    */

92   public int search(Object JavaDoc o) {
93     int i = list.lastIndexOf(o);
94
95     if (i >= 0) { return size() - i; }
96     return -1;
97   }
98
99   private static final long serialVersionUID = 343422342343423234L;
100
101   
102   /* I am not in big favor of having these interfaces */
103   
104   public Object JavaDoc get(int index) {
105     return list.get(index);
106   }
107
108   public Object JavaDoc remove(int index) {
109     return list.remove(index);
110   }
111
112   public Iterator JavaDoc iterator() {
113     return list.iterator();
114   }
115
116   public boolean isEmpty() {
117     return empty();
118   }
119
120   public boolean contains(Object JavaDoc o) {
121     return list.contains(o);
122   }
123   
124   public boolean remove(Object JavaDoc o) {
125     return list.remove(o);
126   }
127 }
128
Popular Tags