1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package net.sf.composite.util;
25
26 import java.util.Stack;
27
28 /***
29 * Identity Stack repackaged from Apache Ant.
30 * @since Ant 1.7
31 */
32 public class IdentityStack extends Stack {
33
34 /***
35 * Get an IdentityStack containing the contents of the specified Stack.
36 * @param s the Stack to copy; ignored if null.
37 * @return an IdentityStack instance.
38 */
39 public static IdentityStack getInstance(Stack s) {
40 if (s instanceof IdentityStack) {
41 return (IdentityStack) s;
42 }
43 IdentityStack result = new IdentityStack();
44 if (s != null) {
45 result.addAll(s);
46 }
47 return result;
48 }
49
50 /***
51 * Default constructor.
52 */
53 public IdentityStack() {
54 }
55
56 /***
57 * Construct a new IdentityStack with the specified Object
58 * as the bottom element.
59 * @param o the bottom element.
60 */
61 public IdentityStack(Object o) {
62 super();
63 push(o);
64 }
65
66 /***
67 * Override methods that use <code>.equals()</code> comparisons on elements.
68 * @param o the Object to search for.
69 * @return true if the stack contains the object.
70 * @see java.util.Vector#contains(Object)
71 */
72 public synchronized boolean contains(Object o) {
73 return indexOf(o) >= 0;
74 }
75
76 /***
77 * Override methods that use <code>.equals()</code> comparisons on elements.
78 * @param o the Object to search for.
79 * @param pos the position from which to search.
80 * @return the position of the object, -1 if not found.
81 * @see java.util.Vector#indexOf(Object, int)
82 */
83 public synchronized int indexOf(Object o, int pos) {
84 for (int i = pos; i < size(); i++) {
85 if (get(i) == o) {
86 return i;
87 }
88 }
89 return -1;
90 }
91
92 /***
93 * Override methods that use <code>.equals()</code> comparisons on elements.
94 * @param o the Object to search for.
95 * @param pos the position from which to search (backward).
96 * @return the position of the object, -1 if not found.
97 * @see java.util.Vector#indexOf(Object, int)
98 */
99 public synchronized int lastIndexOf(Object o, int pos) {
100 for (int i = pos; i >= 0; i--) {
101 if (get(i) == o) {
102 return i;
103 }
104 }
105 return -1;
106 }
107
108 }