01: /*
02: * JacORB - a free Java ORB
03: *
04: * Copyright (C) The JacORB project, 1997-2006.
05: *
06: * This library is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU Library General Public
08: * License as published by the Free Software Foundation; either
09: * version 2 of the License, or (at your option) any later version.
10: *
11: * This library is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14: * Library General Public License for more details.
15: *
16: * You should have received a copy of the GNU Library General Public
17: * License along with this library; if not, write to the Free
18: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19: */
20:
21: package org.jacorb.util;
22:
23: import java.util.EmptyStackException;
24: import java.util.LinkedList;
25:
26: /**
27: * unsynchronized implementation of a stack (LIFO queue)
28: *
29: * @see java.util.Stack
30: * @author Alphonse Bendt
31: * @version $Id: Stack.java,v 1.1 2006/06/29 15:15:58 alphonse.bendt Exp $
32: */
33: public class Stack {
34: private final LinkedList elements = new LinkedList();
35:
36: public boolean empty() {
37: return elements.isEmpty();
38: }
39:
40: public Object push(Object element) {
41: elements.add(element);
42:
43: return element;
44: }
45:
46: public Object pop() {
47: if (empty()) {
48: throw new EmptyStackException();
49: }
50:
51: return elements.removeLast();
52: }
53:
54: public Object peek() {
55: if (empty()) {
56: throw new EmptyStackException();
57: }
58:
59: return elements.getLast();
60: }
61:
62: public int search(Object value) {
63: final int result;
64:
65: if (empty()) {
66: result = -1;
67: } else {
68: result = elements.size() - elements.lastIndexOf(value);
69: }
70:
71: return result;
72: }
73: }
|