1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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 }