KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > util > Stack


1 // $Id: Stack.java,v 1.1.1.1 2003/09/09 01:24:12 belaban Exp $
2

3 package org.jgroups.util;
4
5
6 import java.io.IOException JavaDoc;
7 import java.io.ObjectInput JavaDoc;
8 import java.io.ObjectOutput JavaDoc;
9
10
11
12 /**
13  * LIFO stack, with better performance than java.util.Stack (based on List).
14  */

15 public class Stack extends List {
16
17     
18     public Stack() {}
19
20
21     public void push(Object JavaDoc obj) {
22     addAtHead(obj);
23     }
24
25     public Object JavaDoc pop() {
26     return removeFromHead();
27     }
28
29     public Object JavaDoc peek() {
30     return peekAtHead();
31     }
32
33     
34     public Object JavaDoc bottom() {
35     return super.peek();
36     }
37
38
39     public boolean empty() {
40     return size <= 0;
41     }
42
43
44     public List copy() {
45     Stack retval=new Stack();
46
47     synchronized(mutex) {
48         for(Element el=head; el != null; el=el.next)
49         retval.add(el.obj);
50     }
51     return retval;
52     }
53
54
55
56     public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc {
57     super.writeExternal(out);
58     }
59
60
61
62     public void readExternal(ObjectInput JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
63     super.readExternal(in);
64     }
65
66
67
68
69
70 // public static void main(String[] args) {
71
// Stack st1=new Stack(), st2;
72
// byte[] buf;
73

74
75 // try {
76
// for(int i=0; i < 5; i++)
77
// st1.push(new Integer(i));
78

79         
80 // buf=Util.objectToByteBuffer(st1);
81
// st1.pop();
82
// System.out.println(st1.dump());
83

84 // st2=(Stack)Util.objectFromByteBuffer(buf);
85
// System.out.println(st2.dump());
86

87
88 // st1=(Stack)st2.copy();
89
// System.out.println(st1.dump());
90
// System.out.println(st2.dump());
91
// }
92
// catch(Exception e) {
93
// System.err.println(e);
94
// }
95

96     
97
98 // }
99

100 }
101
Popular Tags