001    package org.apache.ojb.broker.util;
002    
003    /* Copyright 2004-2006 The Apache Software Foundation
004     *
005     * Licensed under the Apache License, Version 2.0 (the "License");
006     * you may not use this file except in compliance with the License.
007     * 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    import java.util.ArrayList;
019    import java.util.Iterator;
020    import java.util.Collection;
021    
022    /**
023     * Object identity based {@link java.util.List}, use <tt>"=="</tt> instead of
024     * <tt>element_1.equals(element_2)</tt> to compare objects.
025     *
026     * @version $Id: IdentityArrayList.java,v 1.1 2007-08-24 22:17:36 ewestfal Exp $
027     */
028    public class IdentityArrayList extends ArrayList
029    {
030    
031        public IdentityArrayList()
032        {
033        }
034    
035        public IdentityArrayList(int initialCapacity)
036        {
037            super(initialCapacity);
038        }
039    
040        public IdentityArrayList(Collection c)
041        {
042            super(c);
043        }
044    
045        public boolean contains(Object elem)
046        {
047            return indexOf(elem) >= 0;
048        }
049    
050        public int indexOf(Object elem)
051        {
052            for(int i = 0; i < size(); i++)
053                if(elem == get(i))
054                    return i;
055            return -1;
056        }
057    
058        public int lastIndexOf(Object elem)
059        {
060            for(int i = size() - 1; i >= 0; i--)
061                if(elem == get(i))
062                    return i;
063            return -1;
064        }
065    
066        public boolean remove(Object o)
067        {
068            Iterator e = iterator();
069            if(o == null)
070            {
071                while(e.hasNext())
072                {
073                    if(e.next() == null)
074                    {
075                        e.remove();
076                        return true;
077                    }
078                }
079            }
080            else
081            {
082                while(e.hasNext())
083                {
084                    if(o == e.next())
085                    {
086                        e.remove();
087                        return true;
088                    }
089                }
090            }
091            return false;
092        }
093    }