01: // $Id: Stack.java,v 1.2 2005/05/30 14:31:29 belaban Exp $
02:
03: package org.jgroups.util;
04:
05: import java.io.IOException;
06: import java.io.ObjectInput;
07: import java.io.ObjectOutput;
08:
09: /**
10: * LIFO stack, with better performance than java.util.Stack (based on List).
11: */
12: public class Stack extends List {
13:
14: public Stack() {
15: }
16:
17: public void push(Object obj) {
18: addAtHead(obj);
19: }
20:
21: public Object pop() {
22: return removeFromHead();
23: }
24:
25: public Object peek() {
26: return peekAtHead();
27: }
28:
29: public Object bottom() {
30: return super .peek();
31: }
32:
33: public boolean empty() {
34: return size <= 0;
35: }
36:
37: public List copy() {
38: Stack retval = new Stack();
39:
40: synchronized (mutex) {
41: for (Element el = head; el != null; el = el.next)
42: retval.add(el.obj);
43: }
44: return retval;
45: }
46:
47: public void writeExternal(ObjectOutput out) throws IOException {
48: super .writeExternal(out);
49: }
50:
51: public void readExternal(ObjectInput in) throws IOException,
52: ClassNotFoundException {
53: super .readExternal(in);
54: }
55:
56: // public static void main(String[] args) {
57: // Stack st1=new Stack(), st2;
58: // byte[] buf;
59:
60: // try {
61: // for(int i=0; i < 5; i++)
62: // st1.push(new Integer(i));
63:
64: // buf=Util.objectToByteBuffer(st1);
65: // st1.pop();
66: // System.out.println(st1.dump());
67:
68: // st2=(Stack)Util.objectFromByteBuffer(buf);
69: // System.out.println(st2.dump());
70:
71: // st1=(Stack)st2.copy();
72: // System.out.println(st1.dump());
73: // System.out.println(st2.dump());
74: // }
75: // catch(Exception e) {
76: // log.error(e);
77: // }
78:
79: // }
80:
81: }
|