View Javadoc
1   /*
2    * reserved comment block
3    * DO NOT REMOVE OR ALTER!
4    */
5   /*
6    * Licensed to the Apache Software Foundation (ASF) under one or more
7    * contributor license agreements.  See the NOTICE file distributed with
8    * this work for additional information regarding copyright ownership.
9    * The ASF licenses this file to You under the Apache License, Version 2.0
10   * (the "License"); you may not use this file except in compliance with
11   * the License.  You may obtain a copy of the License at
12   *
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   *
15   * Unless required by applicable law or agreed to in writing, software
16   * distributed under the License is distributed on an "AS IS" BASIS,
17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   * See the License for the specific language governing permissions and
19   * limitations under the License.
20   */
21  
22  package com.sun.org.apache.xerces.internal.impl.xs.util;
23  
24  import java.lang.reflect.Array;
25  import java.util.AbstractList;
26  
27  import com.sun.org.apache.xerces.internal.xs.datatypes.ObjectList;
28  
29  /**
30   * Contains a list of Objects.
31   *
32   * @xerces.internal
33   *
34   * @version $Id: ObjectListImpl.java,v 1.2 2010-10-26 23:01:04 joehw Exp $
35   */
36  public final class ObjectListImpl extends AbstractList implements ObjectList {
37  
38      /**
39       * An immutable empty list.
40       */
41      public static final ObjectListImpl EMPTY_LIST = new ObjectListImpl(new Object[0], 0);
42  
43      // The array to hold all data
44      private final Object[] fArray;
45  
46      // Number of elements in this list
47      private final int fLength;
48  
49      public ObjectListImpl(Object[] array, int length) {
50          fArray = array;
51          fLength = length;
52      }
53  
54      public int getLength() {
55          return fLength;
56      }
57  
58      public boolean contains(Object item) {
59          if (item == null) {
60              for (int i = 0; i < fLength; i++) {
61                  if (fArray[i] == null)
62                      return true;
63              }
64          }
65          else {
66              for (int i = 0; i < fLength; i++) {
67                  if (item.equals(fArray[i]))
68                      return true;
69              }
70          }
71          return false;
72      }
73  
74      public Object item(int index) {
75          if (index < 0 || index >= fLength) {
76              return null;
77          }
78          return fArray[index];
79      }
80  
81      /*
82       * List methods
83       */
84      public Object get(int index) {
85          if (index >= 0 && index < fLength) {
86              return fArray[index];
87          }
88          throw new IndexOutOfBoundsException("Index: " + index);
89      }
90  
91      public int size() {
92          return getLength();
93      }
94  
95      public Object[] toArray() {
96          Object[] a = new Object[fLength];
97          toArray0(a);
98          return a;
99      }
100 
101     public Object[] toArray(Object[] a) {
102         if (a.length < fLength) {
103             Class arrayClass = a.getClass();
104             Class componentType = arrayClass.getComponentType();
105             a = (Object[]) Array.newInstance(componentType, fLength);
106         }
107         toArray0(a);
108         if (a.length > fLength) {
109             a[fLength] = null;
110         }
111         return a;
112     }
113 
114     private void toArray0(Object[] a) {
115         if (fLength > 0) {
116             System.arraycopy(fArray, 0, a, 0, fLength);
117         }
118     }
119 }