KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > util > Stack


1 /*
2   Copyright (C) 2001 Laurent Martelli
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12   GNU Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public License
15   along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */

17
18 package org.objectweb.jac.util;
19
20 public class Stack extends java.util.Stack JavaDoc {
21     /**
22      * Returns the i-th element from the top of the stack
23      *
24      * @param i index of element to get (0 is the top of the stack,
25      * size()-1 is the bottom)
26      *
27      * @see #poke(int,Object)
28      * @see #top()
29      */

30     public Object JavaDoc peek(int i) {
31         return get(size()-1-i);
32     }
33
34     /**
35      * Returns the top element of the stack
36      */

37     public Object JavaDoc top() {
38         return peek(0);
39     }
40
41     /**
42      * Returns the top element of the stack if it's not empty, null
43      * otherwise.
44      */

45     public Object JavaDoc safeTop() {
46         return empty()?null:peek(0);
47     }
48
49     /**
50      * Sets the value of an element of the stack
51      *
52      * @param i index of element to set (0 is the top of the stack,
53      * size()-1 is the bottom)
54      * @param value the new value
55      *
56      * @see #peek(int)
57      */

58     public void poke(int i, Object JavaDoc value) {
59         set(size()-1-i,value);
60     }
61     /**
62     * Pops n elements from the top of the stack
63     * @param n number of elements to pop off the stack
64     */

65     public void pop(int n) {
66         for (;n>0; n--) {
67             pop();
68         }
69     }
70     /**
71     * swap peek() and peek(1)
72     */

73     public void swap() {
74         Object JavaDoc tmp = peek();
75         poke(0,peek(1));
76         poke(1,tmp);
77     }
78 }
79
Popular Tags