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