home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / main.bin / Stack.java < prev    next >
Text File  |  1997-11-15  |  3KB  |  114 lines

  1. /*
  2.  * @(#)Stack.java    1.16 97/01/28
  3.  * 
  4.  * Copyright (c) 1995, 1996 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  * CopyrightVersion 1.1_beta
  20.  * 
  21.  */
  22.  
  23. package java.util;
  24.  
  25. /**
  26.  * The <code>Stack</code> class represents a last-in-first-out 
  27.  * (LIFO) stack of objects. 
  28.  *
  29.  * @author  Jonathan Payne
  30.  * @version 1.16, 01/28/97
  31.  * @since   JDK1.0
  32.  */
  33. public
  34. class Stack extends Vector {
  35.     /**
  36.      * Pushes an item onto the top of this stack. 
  37.      *
  38.      * @param   item   the item to be pushed onto this stack.
  39.      * @return  the <code>item</code> argument.
  40.      * @since   JDK1.0
  41.      */
  42.     public Object push(Object item) {
  43.     addElement(item);
  44.  
  45.     return item;
  46.     }
  47.  
  48.     /**
  49.      * Removes the object at the top of this stack and returns that 
  50.      * object as the value of this function. 
  51.      *
  52.      * @return     The object at the top of this stack.
  53.      * @exception  EmptyStackException  if this stack is empty.
  54.      * @since      JDK1.0
  55.      */
  56.     public synchronized Object pop() {
  57.     Object    obj;
  58.     int    len = size();
  59.  
  60.     obj = peek();
  61.     removeElementAt(len - 1);
  62.  
  63.     return obj;
  64.     }
  65.  
  66.     /**
  67.      * Looks at the object at the top of this stack without removing it 
  68.      * from the stack. 
  69.      *
  70.      * @return     the object at the top of this stack. 
  71.      * @exception  EmptyStackException  if this stack is empty.
  72.      * @since      JDK1.0
  73.      */
  74.     public synchronized Object peek() {
  75.     int    len = size();
  76.  
  77.     if (len == 0)
  78.         throw new EmptyStackException();
  79.     return elementAt(len - 1);
  80.     }
  81.  
  82.     /**
  83.      * Tests if this stack is empty.
  84.      *
  85.      * @return  <code>true</code> if this stack is empty;
  86.      *          <code>false</code> otherwise.
  87.      * @since   JDK1.0
  88.      */
  89.     public boolean empty() {
  90.     return size() == 0;
  91.     }
  92.  
  93.     /**
  94.      * Returns where an object is on this stack. 
  95.      *
  96.      * @param   o   the desired object.
  97.      * @return  the distance from the top of the stack where the object is]
  98.      *          located; the return value <code>-1</code> indicates that the
  99.      *          object is not on the stack.
  100.      * @since   JDK1.0
  101.      */
  102.     public synchronized int search(Object o) {
  103.     int i = lastIndexOf(o);
  104.  
  105.     if (i >= 0) {
  106.         return size() - i;
  107.     }
  108.     return -1;
  109.     }
  110.  
  111.     /** use serialVersionUID from JDK 1.0.2 for interoperability */
  112.     private static final long serialVersionUID = 1224463164541339165L;
  113. }
  114.