01: /*
02: * Copyright 2007 Google Inc.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
05: * use this file except in compliance with the License. You may obtain a copy of
06: * the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13: * License for the specific language governing permissions and limitations under
14: * the License.
15: */
16: package java.util;
17:
18: /**
19: * Maintains a last-in, first-out collection of objects. <a
20: * href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/Stack.html">[Sun
21: * docs]</a>
22: *
23: * @param <E> element type.
24: */
25: public class Stack<E> extends Vector<E> {
26:
27: @Override
28: public Object clone() {
29: Stack<E> s = new Stack<E>();
30: s.addAll(this );
31: return s;
32: }
33:
34: public boolean empty() {
35: return isEmpty();
36: }
37:
38: public E peek() {
39: int sz = size();
40: if (sz > 0) {
41: return get(sz - 1);
42: } else {
43: throw new EmptyStackException();
44: }
45: }
46:
47: public E pop() {
48: int sz = size();
49: if (sz > 0) {
50: return remove(sz - 1);
51: } else {
52: throw new EmptyStackException();
53: }
54: }
55:
56: public E push(E o) {
57: add(o);
58: return o;
59: }
60:
61: public int search(Object o) {
62: int pos = lastIndexOf(o);
63: if (pos >= 0) {
64: return size() - pos;
65: }
66: return -1;
67: }
68:
69: }
|