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