1 /*
   2  * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
   3  */
   4 /*
   5  * Licensed to the Apache Software Foundation (ASF) under one or more
   6  * contributor license agreements.  See the NOTICE file distributed with
   7  * this work for additional information regarding copyright ownership.
   8  * The ASF licenses this file to You under the Apache License, Version 2.0
   9  * (the "License"); you may not use this file except in compliance with
  10  * the License.  You may obtain a copy of the License at
  11  *
  12  *      http://www.apache.org/licenses/LICENSE-2.0
  13  *
  14  * Unless required by applicable law or agreed to in writing, software
  15  * distributed under the License is distributed on an "AS IS" BASIS,
  16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17  * See the License for the specific language governing permissions and
  18  * limitations under the License.
  19  */
  20 
  21 package com.sun.org.apache.xerces.internal.impl.xs.util;
  22 
  23 import com.sun.org.apache.xerces.internal.xs.ShortList;
  24 import com.sun.org.apache.xerces.internal.xs.XSException;
  25 import java.util.AbstractList;
  26 
  27 /**
  28  * Containts a list of Object's.
  29  *
  30  * @xerces.internal
  31  *
  32  * @author Sandy Gao, IBM
  33  *
  34  * @LastModified: Oct 2017
  35  */
  36 public final class ShortListImpl extends AbstractList<Short> implements ShortList {
  37 
  38     /**
  39      * An immutable empty list.
  40      */
  41     public static final ShortListImpl EMPTY_LIST = new ShortListImpl(new short[0], 0);
  42 
  43     // The array to hold all data
  44     private final short[] fArray;
  45     // Number of elements in this list
  46     private final int fLength;
  47 
  48     /**
  49      * Construct an XSObjectList implementation
  50      *
  51      * @param array     the data array
  52      * @param length    the number of elements
  53      */
  54     public ShortListImpl(short[] array, int length) {
  55         fArray = array;
  56         fLength = length;
  57     }
  58 
  59     /**
  60      * The number of <code>Objects</code> in the list. The range of valid
  61      * child node indices is 0 to <code>length-1</code> inclusive.
  62      */
  63     public int getLength() {
  64         return fLength;
  65     }
  66 
  67     /**
  68      *  Checks if the <code>unsigned short</code> <code>item</code> is a
  69      * member of this list.
  70      * @param item  <code>unsigned short</code> whose presence in this list
  71      *   is to be tested.
  72      * @return  True if this list contains the <code>unsigned short</code>
  73      *   <code>item</code>.
  74      */
  75     public boolean contains(short item) {
  76         for (int i = 0; i < fLength; i++) {
  77             if (fArray[i] == item) {
  78                 return true;
  79             }
  80         }
  81         return false;
  82     }
  83 
  84     public short item(int index) throws XSException {
  85         if (index < 0 || index >= fLength) {
  86             throw new XSException(XSException.INDEX_SIZE_ERR, null);
  87         }
  88         return fArray[index];
  89     }
  90 
  91     public boolean equals(Object obj) {
  92         if (obj == null || !(obj instanceof ShortList)) {
  93             return false;
  94         }
  95         ShortList rhs = (ShortList)obj;
  96 
  97         if (fLength != rhs.getLength()) {
  98             return false;
  99         }
 100         for (int i = 0;i < fLength; ++i) {
 101             if (fArray[i] != rhs.item(i)) {
 102                 return false;
 103             }
 104         }
 105         return true;
 106     }
 107 
 108     /*
 109      * List methods
 110      */
 111 
 112     public Short get(int index) {
 113         if (index >= 0 && index < fLength) {
 114             return fArray[index];
 115         }
 116         throw new IndexOutOfBoundsException("Index: " + index);
 117     }
 118 
 119     public int size() {
 120         return getLength();
 121     }
 122 
 123 } // class ShortListImpl