StackImpl.java revision 608:7e06bf1dcb09
1/*
2 * Copyright (c) 2001, 2003, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package com.sun.corba.se.impl.orbutil ;
27
28import java.util.EmptyStackException ;
29
30// We implement a Stack here instead of using java.util.Stack because
31// java.util.Stack is thread-safe, negatively impacting performance.
32// We use an ArrayList instead since it is not thread-safe.
33// RequestInfoStack is used quite frequently.
34public class StackImpl {
35    // The stack for RequestInfo objects.
36    private Object[] data = new Object[3] ;
37    private int top = -1 ;
38
39    // Tests if this stack is empty.
40    public final boolean empty() {
41        return top == -1;
42    }
43
44    // Looks at the object at the top of this stack without removing it
45    // from the stack.
46    public final Object peek() {
47        if (empty())
48            throw new EmptyStackException();
49
50        return data[ top ];
51    }
52
53    // Removes the object at the top of this stack and returns that
54    // object as the value of this function.
55    public final Object pop() {
56        Object obj = peek() ;
57        data[top] = null ;
58        top-- ;
59        return obj;
60    }
61
62    private void ensure()
63    {
64        if (top == (data.length-1)) {
65            int newSize = 2*data.length ;
66            Object[] newData = new Object[ newSize ] ;
67            System.arraycopy( data, 0, newData, 0, data.length ) ;
68            data = newData ;
69        }
70    }
71
72    // Pushes an item onto the top of the stack
73    public final Object push( Object item ) {
74        ensure() ;
75        top++ ;
76        data[top] = item;
77        return item;
78    }
79}
80