1 /*
2 * Copyright (C) 2004 TiongHiang Lee
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library 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 GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Email: thlee@onemindsoft.org
19 */
20
21 package org.onemind.commons.java.datastructure;
22
23 /***
24 * An extension of java.util.Stack to have popUntil and pushReturnSize method
25 * @author TiongHiang Lee (thlee@onemindsoft.org)
26 * @version $Id: Stack.java,v 1.2 2004/08/26 12:33:16 thlee Exp $ $Name: $
27 */
28 public class Stack extends java.util.Stack
29 {
30
31 /***
32 * {@inheritDoc}
33 */
34 public Stack()
35 {
36 }
37
38 /***
39 * Push the object ot the stack and return the size before pushing the object in
40 * @param o the object
41 * @return the size before the push
42 */
43 public int pushReturnSize(Object o)
44 {
45 int i = size();
46 push(o);
47 return i;
48 }
49
50 /***
51 * Pop until the stack reach size i
52 * @param i the size
53 */
54 public void popUntil(int i)
55 {
56 if (i < 0)
57 {
58 throw new IllegalArgumentException("Invalid size for popUtil");
59 } else if (i > size())
60 {
61 throw new RuntimeException("Stack already less than " + i);
62 }
63 while (size() > i)
64 {
65 pop();
66 }
67 }
68 }