View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one or more
3    *  contributor license agreements.  See the NOTICE file distributed with
4    *  this work for additional information regarding copyright ownership.
5    *  The ASF licenses this file to You under the Apache License, Version 2.0
6    *  (the "License"); you may not use this file except in compliance with
7    *  the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   *
17   */
18  
19  /*
20   * NOTICE: In accordance with section 4d of the Apache License, version 2.0,
21   * let it be known that this source file is to be considered software developed
22   * by the Apache Software Foundation.
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 }