KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > util > IdentityStack


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 package org.apache.tools.ant.util;
19
20 import java.util.Stack JavaDoc;
21
22 /**
23  * Identity Stack.
24  * @since Ant 1.7
25  */

26 public class IdentityStack extends Stack JavaDoc {
27
28     /**
29      * Get an IdentityStack containing the contents of the specified Stack.
30      * @param s the Stack to copy; ignored if null.
31      * @return an IdentityStack instance.
32      */

33     public static IdentityStack getInstance(Stack JavaDoc s) {
34         if (s instanceof IdentityStack) {
35             return (IdentityStack) s;
36         }
37         IdentityStack result = new IdentityStack();
38         if (s != null) {
39             result.addAll(s);
40         }
41         return result;
42     }
43
44     /**
45      * Default constructor.
46      */

47     public IdentityStack() {
48     }
49
50     /**
51      * Construct a new IdentityStack with the specified Object
52      * as the bottom element.
53      * @param o the bottom element.
54      */

55     public IdentityStack(Object JavaDoc o) {
56         super();
57         push(o);
58     }
59
60     /**
61      * Override methods that use <code>.equals()</code> comparisons on elements.
62      * @param o the Object to search for.
63      * @return true if the stack contains the object.
64      * @see java.util.Vector#contains(Object)
65      */

66     public synchronized boolean contains(Object JavaDoc o) {
67         return indexOf(o) >= 0;
68     }
69
70     /**
71      * Override methods that use <code>.equals()</code> comparisons on elements.
72      * @param o the Object to search for.
73      * @param pos the position from which to search.
74      * @return the position of the object, -1 if not found.
75      * @see java.util.Vector#indexOf(Object, int)
76      */

77     public synchronized int indexOf(Object JavaDoc o, int pos) {
78         for (int i = pos; i < size(); i++) {
79             if (get(i) == o) {
80                 return i;
81             }
82         }
83         return -1;
84     }
85
86     /**
87      * Override methods that use <code>.equals()</code> comparisons on elements.
88      * @param o the Object to search for.
89      * @param pos the position from which to search (backward).
90      * @return the position of the object, -1 if not found.
91      * @see java.util.Vector#indexOf(Object, int)
92      */

93     public synchronized int lastIndexOf(Object JavaDoc o, int pos) {
94         for (int i = pos; i >= 0; i--) {
95             if (get(i) == o) {
96                 return i;
97             }
98         }
99         return -1;
100     }
101
102 }
103
Popular Tags