1 /*
   2  * reserved comment block
   3  * DO NOT REMOVE OR ALTER!
   4  */
   5 /*
   6  * Copyright 2002,2003-2004 The Apache Software Foundation.
   7  *
   8  * Licensed under the Apache License, Version 2.0 (the "License");
   9  * you may not use this file except in compliance with the License.
  10  * 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 java.util.AbstractList;
  24 
  25 import com.sun.org.apache.xerces.internal.xs.ShortList;
  26 import com.sun.org.apache.xerces.internal.xs.XSException;
  27 
  28 /**
  29  * Containts a list of Object's.
  30  *
  31  * @xerces.internal
  32  *
  33  * @author Sandy Gao, IBM
  34  *
  35  * @version $Id: ShortListImpl.java,v 1.7 2010-11-01 04:40:06 joehw Exp $
  36  */
  37 public final class ShortListImpl extends AbstractList implements ShortList {
  38 
  39     /**
  40      * An immutable empty list.
  41      */
  42     public static final ShortListImpl EMPTY_LIST = new ShortListImpl(new short[0], 0);
  43 
  44     // The array to hold all data
  45     private final short[] fArray;
  46     // Number of elements in this list
  47     private final int fLength;
  48 
  49     /**
  50      * Construct an XSObjectList implementation
  51      *
  52      * @param array     the data array
  53      * @param length    the number of elements
  54      */
  55     public ShortListImpl(short[] array, int length) {
  56         fArray = array;
  57         fLength = length;
  58     }
  59 
  60     /**
  61      * The number of <code>Objects</code> in the list. The range of valid
  62      * child node indices is 0 to <code>length-1</code> inclusive.
  63      */
  64     public int getLength() {
  65         return fLength;
  66     }
  67 
  68     /**
  69      *  Checks if the <code>unsigned short</code> <code>item</code> is a
  70      * member of this list.
  71      * @param item  <code>unsigned short</code> whose presence in this list
  72      *   is to be tested.
  73      * @return  True if this list contains the <code>unsigned short</code>
  74      *   <code>item</code>.
  75      */
  76     public boolean contains(short item) {
  77         for (int i = 0; i < fLength; i++) {
  78             if (fArray[i] == item) {
  79                 return true;
  80             }
  81         }
  82         return false;
  83     }
  84 
  85     public short item(int index) throws XSException {
  86         if (index < 0 || index >= fLength) {
  87             throw new XSException(XSException.INDEX_SIZE_ERR, null);
  88         }
  89         return fArray[index];
  90     }
  91 
  92     public boolean equals(Object obj) {
  93         if (obj == null || !(obj instanceof ShortList)) {
  94             return false;
  95         }
  96         ShortList rhs = (ShortList)obj;
  97 
  98         if (fLength != rhs.getLength()) {
  99             return false;
 100         }
 101         for (int i = 0;i < fLength; ++i) {
 102             if (fArray[i] != rhs.item(i)) {
 103                 return false;
 104             }
 105         }
 106         return true;
 107     }
 108 
 109     /*
 110      * List methods
 111      */
 112 
 113     public Object get(int index) {
 114         if (index >= 0 && index < fLength) {
 115             return new Short(fArray[index]);
 116         }
 117         throw new IndexOutOfBoundsException("Index: " + index);
 118     }
 119 
 120     public int size() {
 121         return getLength();
 122     }
 123 
 124 } // class ShortListImpl