001: /**
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */package com.tc.util;
004:
005: import java.util.ArrayList;
006: import java.util.EmptyStackException;
007: import java.util.Iterator;
008: import java.util.List;
009:
010: /*
011: * This stack implementation uses ArrayList internally. This is mainly created so that we dont have synchronization
012: * overheads that java.util.Stack imposses since it is based on Vector. This class maintains an interface level compatibility
013: * with java.util.Stack but doesnot implement all of Vector interfaces.
014: */
015: public class Stack {
016:
017: private final List list = new ArrayList();
018:
019: /**
020: * Creates an empty Stack.
021: */
022: public Stack() {
023: }
024:
025: /**
026: * Pushes an item onto the top of this stack. This has exactly the same effect as: <blockquote>
027: *
028: * @param item the item to be pushed onto this stack.
029: * @return the <code>item</code> argument.
030: * @see java.util.Vector#addElement
031: */
032: public Object push(Object item) {
033: list.add(item);
034: return item;
035: }
036:
037: /**
038: * Removes the object at the top of this stack and returns that object as the value of this function.
039: *
040: * @return The object at the top of this stack (the last item of the <tt>Vector</tt> object).
041: * @exception EmptyStackException if this stack is empty.
042: */
043: public Object pop() {
044: int len = size();
045:
046: if (len == 0)
047: throw new EmptyStackException();
048: return list.remove(len - 1);
049: }
050:
051: /**
052: * Looks at the object at the top of this stack without removing it from the stack.
053: *
054: * @return the object at the top of this stack (the last item of the <tt>Vector</tt> object).
055: * @exception EmptyStackException if this stack is empty.
056: */
057: public Object peek() {
058: int len = size();
059:
060: if (len == 0)
061: throw new EmptyStackException();
062: return list.get(len - 1);
063: }
064:
065: /**
066: * Tests if this stack is empty.
067: *
068: * @return <code>true</code> if and only if this stack contains no items; <code>false</code> otherwise.
069: */
070: public boolean empty() {
071: return size() == 0;
072: }
073:
074: /**
075: * Size of this Stack
076: *
077: * @return the size of the stack
078: */
079: public int size() {
080: return list.size();
081: }
082:
083: /**
084: * Returns the 1-based position where an object is on this stack. If the object <tt>o</tt> occurs as an item in this
085: * stack, this method returns the distance from the top of the stack of the occurrence nearest the top of the stack;
086: * the topmost item on the stack is considered to be at distance <tt>1</tt>. The <tt>equals</tt> method is used
087: * to compare <tt>o</tt> to the items in this stack.
088: *
089: * @param o the desired object.
090: * @return the 1-based position from the top of the stack where the object is located; the return value
091: * <code>-1</code> indicates that the object is not on the stack.
092: */
093: public int search(Object o) {
094: int i = list.lastIndexOf(o);
095:
096: if (i >= 0) {
097: return size() - i;
098: }
099: return -1;
100: }
101:
102: private static final long serialVersionUID = 343422342343423234L;
103:
104: /* I am not in big favor of having these interfaces */
105:
106: public Object get(int index) {
107: return list.get(index);
108: }
109:
110: public Object remove(int index) {
111: return list.remove(index);
112: }
113:
114: public Iterator iterator() {
115: return list.iterator();
116: }
117:
118: public boolean isEmpty() {
119: return empty();
120: }
121:
122: public boolean contains(Object o) {
123: return list.contains(o);
124: }
125:
126: public boolean remove(Object o) {
127: return list.remove(o);
128: }
129: }
|