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 import java.util.Vector; 27 28 import com.sun.org.apache.xerces.internal.xs.StringList; 29 30 /** 31 * Containts a list of Object's. 32 * 33 * @xerces.internal 34 * 35 * @author Sandy Gao, IBM 36 * 37 * @version $Id: StringListImpl.java,v 1.7 2010-11-01 04:40:06 joehw Exp $ 38 */ 39 public final class StringListImpl extends AbstractList implements StringList { 40 41 /** 42 * An immutable empty list. 43 */ 44 public static final StringListImpl EMPTY_LIST = new StringListImpl(new String[0], 0); 45 46 // The array to hold all data 47 private final String[] fArray; 48 // Number of elements in this list 49 private final int fLength; 50 51 // REVISIT: this is temp solution. In general we need to use this class 52 // instead of the Vector. 53 private final Vector fVector; 54 55 public StringListImpl(Vector v) { 56 fVector = v; 57 fLength = (v == null) ? 0 : v.size(); 58 fArray = null; 59 } 60 61 /** 62 * Construct an XSObjectList implementation 63 * 64 * @param array the data array 65 * @param length the number of elements 66 */ 67 public StringListImpl(String[] array, int length) { 68 fArray = array; 69 fLength = length; 70 fVector = null; 71 } 72 73 /** 74 * The number of <code>Objects</code> in the list. The range of valid 75 * child node indices is 0 to <code>length-1</code> inclusive. 76 */ 77 public int getLength() { 78 return fLength; 79 } 80 81 /** 82 * Checks if the <code>GenericString</code> <code>item</code> is a member 83 * of this list. 84 * @param item <code>GenericString</code> whose presence in this list is 85 * to be tested. 86 * @return True if this list contains the <code>GenericString</code> 87 * <code>item</code>. 88 */ 89 public boolean contains(String item) { 90 if (fVector != null) { 91 return fVector.contains(item); 92 } 93 if (item == null) { 94 for (int i = 0; i < fLength; i++) { 95 if (fArray[i] == null) 96 return true; 97 } 98 } 99 else { 100 for (int i = 0; i < fLength; i++) { 101 if (item.equals(fArray[i])) 102 return true; 103 } 104 } 105 return false; 106 } 107 108 public String item(int index) { 109 if (index < 0 || index >= fLength) { 110 return null; 111 } 112 if (fVector != null) { 113 return (String)fVector.elementAt(index); 114 } 115 return fArray[index]; 116 } 117 118 /* 119 * List methods 120 */ 121 122 public Object get(int index) { 123 if (index >= 0 && index < fLength) { 124 if (fVector != null) { 125 return fVector.elementAt(index); 126 } 127 return fArray[index]; 128 } 129 throw new IndexOutOfBoundsException("Index: " + index); 130 } 131 132 public int size() { 133 return getLength(); 134 } 135 136 public Object[] toArray() { 137 if (fVector != null) { 138 return fVector.toArray(); 139 } 140 Object[] a = new Object[fLength]; 141 toArray0(a); 142 return a; 143 } 144 145 public Object[] toArray(Object[] a) { 146 if (fVector != null) { 147 return fVector.toArray(a); 148 } 149 if (a.length < fLength) { 150 Class arrayClass = a.getClass(); 151 Class componentType = arrayClass.getComponentType(); 152 a = (Object[]) Array.newInstance(componentType, fLength); 153 } 154 toArray0(a); 155 if (a.length > fLength) { 156 a[fLength] = null; 157 } 158 return a; 159 } 160 161 private void toArray0(Object[] a) { 162 if (fLength > 0) { 163 System.arraycopy(fArray, 0, a, 0, fLength); 164 } 165 } 166 167 } // class StringListImpl