001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.tools.ant.util;
019:
020: import java.util.Stack;
021:
022: /**
023: * Identity Stack.
024: * @since Ant 1.7
025: */
026: public class IdentityStack extends Stack {
027:
028: /**
029: * Get an IdentityStack containing the contents of the specified Stack.
030: * @param s the Stack to copy; ignored if null.
031: * @return an IdentityStack instance.
032: */
033: public static IdentityStack getInstance(Stack s) {
034: if (s instanceof IdentityStack) {
035: return (IdentityStack) s;
036: }
037: IdentityStack result = new IdentityStack();
038: if (s != null) {
039: result.addAll(s);
040: }
041: return result;
042: }
043:
044: /**
045: * Default constructor.
046: */
047: public IdentityStack() {
048: }
049:
050: /**
051: * Construct a new IdentityStack with the specified Object
052: * as the bottom element.
053: * @param o the bottom element.
054: */
055: public IdentityStack(Object o) {
056: super ();
057: push(o);
058: }
059:
060: /**
061: * Override methods that use <code>.equals()</code> comparisons on elements.
062: * @param o the Object to search for.
063: * @return true if the stack contains the object.
064: * @see java.util.Vector#contains(Object)
065: */
066: public synchronized boolean contains(Object o) {
067: return indexOf(o) >= 0;
068: }
069:
070: /**
071: * Override methods that use <code>.equals()</code> comparisons on elements.
072: * @param o the Object to search for.
073: * @param pos the position from which to search.
074: * @return the position of the object, -1 if not found.
075: * @see java.util.Vector#indexOf(Object, int)
076: */
077: public synchronized int indexOf(Object o, int pos) {
078: for (int i = pos; i < size(); i++) {
079: if (get(i) == o) {
080: return i;
081: }
082: }
083: return -1;
084: }
085:
086: /**
087: * Override methods that use <code>.equals()</code> comparisons on elements.
088: * @param o the Object to search for.
089: * @param pos the position from which to search (backward).
090: * @return the position of the object, -1 if not found.
091: * @see java.util.Vector#indexOf(Object, int)
092: */
093: public synchronized int lastIndexOf(Object o, int pos) {
094: for (int i = pos; i >= 0; i--) {
095: if (get(i) == o) {
096: return i;
097: }
098: }
099: return -1;
100: }
101:
102: }
|