View Javadoc

1   package org.apache.ojb.broker.util;
2   
3   /* Copyright 2004-2006 The Apache Software Foundation
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * 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  import java.util.ArrayList;
19  import java.util.Iterator;
20  import java.util.Collection;
21  
22  /**
23   * Object identity based {@link java.util.List}, use <tt>"=="</tt> instead of
24   * <tt>element_1.equals(element_2)</tt> to compare objects.
25   *
26   * @version $Id: IdentityArrayList.java,v 1.1 2007-08-24 22:17:36 ewestfal Exp $
27   */
28  public class IdentityArrayList extends ArrayList
29  {
30  
31      public IdentityArrayList()
32      {
33      }
34  
35      public IdentityArrayList(int initialCapacity)
36      {
37          super(initialCapacity);
38      }
39  
40      public IdentityArrayList(Collection c)
41      {
42          super(c);
43      }
44  
45      public boolean contains(Object elem)
46      {
47          return indexOf(elem) >= 0;
48      }
49  
50      public int indexOf(Object elem)
51      {
52          for(int i = 0; i < size(); i++)
53              if(elem == get(i))
54                  return i;
55          return -1;
56      }
57  
58      public int lastIndexOf(Object elem)
59      {
60          for(int i = size() - 1; i >= 0; i--)
61              if(elem == get(i))
62                  return i;
63          return -1;
64      }
65  
66      public boolean remove(Object o)
67      {
68          Iterator e = iterator();
69          if(o == null)
70          {
71              while(e.hasNext())
72              {
73                  if(e.next() == null)
74                  {
75                      e.remove();
76                      return true;
77                  }
78              }
79          }
80          else
81          {
82              while(e.hasNext())
83              {
84                  if(o == e.next())
85                  {
86                      e.remove();
87                      return true;
88                  }
89              }
90          }
91          return false;
92      }
93  }