1 /*
   2  * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
   3  * @LastModified: Oct 2017
   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 com.sun.org.apache.xerces.internal.xs.LSInputList;
  25 import java.lang.reflect.Array;
  26 import java.util.AbstractList;
  27 import org.w3c.dom.ls.LSInput;
  28 
  29 /**
  30  * Contains a list of LSInputs.
  31  *
  32  * @xerces.internal
  33  *
  34  * @author Michael Glavassevich, IBM
  35  *
  36  */
  37 @SuppressWarnings("unchecked") // method <T>toArray(T[])
  38 public final class LSInputListImpl extends AbstractList<LSInput> implements LSInputList {
  39 
  40     /**
  41      * An immutable empty list.
  42      */
  43     public static final LSInputListImpl EMPTY_LIST = new LSInputListImpl(new LSInput[0], 0);
  44 
  45     // The array to hold all data
  46     private final LSInput[] fArray;
  47     // Number of elements in this list
  48     private final int fLength;
  49 
  50     /**
  51      * Construct an LSInputList implementation
  52      *
  53      * @param array     the data array
  54      * @param length    the number of elements
  55      */
  56     public LSInputListImpl(LSInput[] array, int length) {
  57         fArray = array;
  58         fLength = length;
  59     }
  60 
  61     /**
  62      * The number of <code>LSInput</code>s in the list. The range of valid
  63      * child object indices is 0 to <code>length-1</code> inclusive.
  64      */
  65     public int getLength() {
  66         return fLength;
  67     }
  68 
  69     /**
  70      * Returns the <code>index</code>th item in the collection or
  71      * <code>null</code> if <code>index</code> is greater than or equal to
  72      * the number of objects in the list. The index starts at 0.
  73      * @param index  index into the collection.
  74      * @return  The <code>LSInput</code> at the <code>index</code>th
  75      *   position in the <code>LSInputList</code>, or <code>null</code> if
  76      *   the index specified is not valid.
  77      */
  78     public LSInput item(int index) {
  79         if (index < 0 || index >= fLength) {
  80             return null;
  81         }
  82         return fArray[index];
  83     }
  84 
  85     /*
  86      * List methods
  87      */
  88 
  89     public LSInput get(int index) {
  90         if (index >= 0 && index < fLength) {
  91             return fArray[index];
  92         }
  93         throw new IndexOutOfBoundsException("Index: " + index);
  94     }
  95 
  96     public int size() {
  97         return getLength();
  98     }
  99 
 100     public Object[] toArray() {
 101         Object[] a = new Object[fLength];
 102         toArray0(a);
 103         return a;
 104     }
 105 
 106     public Object[] toArray(Object[] a) {
 107         if (a.length < fLength) {
 108             Class<?> arrayClass = a.getClass();
 109             Class<?> componentType = arrayClass.getComponentType();
 110             a = (Object[]) Array.newInstance(componentType, fLength);
 111         }
 112         toArray0(a);
 113         if (a.length > fLength) {
 114             a[fLength] = null;
 115         }
 116         return a;
 117     }
 118 
 119     private void toArray0(Object[] a) {
 120         if (fLength > 0) {
 121             System.arraycopy(fArray, 0, a, 0, fLength);
 122         }
 123     }
 124 
 125 } // LSInputListImpl