KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > corba > se > impl > orbutil > StackImpl


1 /*
2  * @(#)StackImpl.java 1.11 04/06/21
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package com.sun.corba.se.impl.orbutil ;
9
10 import java.util.EmptyStackException JavaDoc ;
11
12 // We implement a Stack here instead of using java.util.Stack because
13
// java.util.Stack is thread-safe, negatively impacting performance.
14
// We use an ArrayList instead since it is not thread-safe.
15
// RequestInfoStack is used quite frequently.
16
public class StackImpl {
17     // The stack for RequestInfo objects.
18
private Object JavaDoc[] data = new Object JavaDoc[3] ;
19     private int top = -1 ;
20
21     // Tests if this stack is empty.
22
public final boolean empty() {
23     return top == -1;
24     }
25
26     // Looks at the object at the top of this stack without removing it
27
// from the stack.
28
public final Object JavaDoc peek() {
29     if (empty())
30         throw new EmptyStackException JavaDoc();
31
32     return data[ top ];
33     }
34
35     // Removes the object at the top of this stack and returns that
36
// object as the value of this function.
37
public final Object JavaDoc pop() {
38     Object JavaDoc obj = peek() ;
39     data[top] = null ;
40     top-- ;
41     return obj;
42     }
43
44     private void ensure()
45     {
46     if (top == (data.length-1)) {
47         int newSize = 2*data.length ;
48         Object JavaDoc[] newData = new Object JavaDoc[ newSize ] ;
49         System.arraycopy( data, 0, newData, 0, data.length ) ;
50         data = newData ;
51     }
52     }
53
54     // Pushes an item onto the top of the stack
55
public final Object JavaDoc push( Object JavaDoc item ) {
56     ensure() ;
57     top++ ;
58     data[top] = item;
59     return item;
60     }
61 }
62
Popular Tags