286N/A/*
286N/A * reserved comment block
286N/A * DO NOT REMOVE OR ALTER!
286N/A */
286N/A/*
286N/A * Copyright 2002,2003-2004 The Apache Software Foundation.
286N/A *
286N/A * Licensed under the Apache License, Version 2.0 (the "License");
286N/A * you may not use this file except in compliance with the License.
286N/A * You may obtain a copy of the License at
286N/A *
286N/A * http://www.apache.org/licenses/LICENSE-2.0
286N/A *
286N/A * Unless required by applicable law or agreed to in writing, software
286N/A * distributed under the License is distributed on an "AS IS" BASIS,
286N/A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
286N/A * See the License for the specific language governing permissions and
286N/A * limitations under the License.
286N/A */
286N/A
286N/Apackage com.sun.org.apache.xerces.internal.impl.xs.util;
286N/A
286N/Aimport java.lang.reflect.Array;
286N/Aimport java.util.AbstractList;
286N/Aimport java.util.Vector;
286N/A
286N/Aimport com.sun.org.apache.xerces.internal.xs.StringList;
286N/A
286N/A/**
286N/A * Containts a list of Object's.
286N/A *
286N/A * @xerces.internal
286N/A *
286N/A * @author Sandy Gao, IBM
286N/A *
286N/A * @version $Id: StringListImpl.java,v 1.7 2010-11-01 04:40:06 joehw Exp $
286N/A */
286N/Apublic final class StringListImpl extends AbstractList implements StringList {
286N/A
286N/A /**
286N/A * An immutable empty list.
286N/A */
286N/A public static final StringListImpl EMPTY_LIST = new StringListImpl(new String[0], 0);
286N/A
286N/A // The array to hold all data
286N/A private final String[] fArray;
286N/A // Number of elements in this list
286N/A private final int fLength;
286N/A
286N/A // REVISIT: this is temp solution. In general we need to use this class
286N/A // instead of the Vector.
286N/A private final Vector fVector;
286N/A
286N/A public StringListImpl(Vector v) {
286N/A fVector = v;
286N/A fLength = (v == null) ? 0 : v.size();
286N/A fArray = null;
286N/A }
286N/A
286N/A /**
286N/A * Construct an XSObjectList implementation
286N/A *
286N/A * @param array the data array
286N/A * @param length the number of elements
286N/A */
286N/A public StringListImpl(String[] array, int length) {
286N/A fArray = array;
286N/A fLength = length;
286N/A fVector = null;
286N/A }
286N/A
286N/A /**
286N/A * The number of <code>Objects</code> in the list. The range of valid
286N/A * child node indices is 0 to <code>length-1</code> inclusive.
286N/A */
286N/A public int getLength() {
286N/A return fLength;
286N/A }
286N/A
286N/A /**
286N/A * Checks if the <code>GenericString</code> <code>item</code> is a member
286N/A * of this list.
286N/A * @param item <code>GenericString</code> whose presence in this list is
286N/A * to be tested.
286N/A * @return True if this list contains the <code>GenericString</code>
286N/A * <code>item</code>.
286N/A */
286N/A public boolean contains(String item) {
286N/A if (fVector != null) {
286N/A return fVector.contains(item);
286N/A }
286N/A if (item == null) {
286N/A for (int i = 0; i < fLength; i++) {
286N/A if (fArray[i] == null)
286N/A return true;
286N/A }
286N/A }
286N/A else {
286N/A for (int i = 0; i < fLength; i++) {
286N/A if (item.equals(fArray[i]))
286N/A return true;
286N/A }
286N/A }
286N/A return false;
286N/A }
286N/A
286N/A public String item(int index) {
286N/A if (index < 0 || index >= fLength) {
286N/A return null;
286N/A }
286N/A if (fVector != null) {
286N/A return (String)fVector.elementAt(index);
286N/A }
286N/A return fArray[index];
286N/A }
286N/A
286N/A /*
286N/A * List methods
286N/A */
286N/A
286N/A public Object get(int index) {
286N/A if (index >= 0 && index < fLength) {
286N/A if (fVector != null) {
286N/A return fVector.elementAt(index);
286N/A }
286N/A return fArray[index];
286N/A }
286N/A throw new IndexOutOfBoundsException("Index: " + index);
286N/A }
286N/A
286N/A public int size() {
286N/A return getLength();
286N/A }
286N/A
286N/A public Object[] toArray() {
286N/A if (fVector != null) {
286N/A return fVector.toArray();
286N/A }
286N/A Object[] a = new Object[fLength];
286N/A toArray0(a);
286N/A return a;
286N/A }
286N/A
286N/A public Object[] toArray(Object[] a) {
286N/A if (fVector != null) {
286N/A return fVector.toArray(a);
286N/A }
286N/A if (a.length < fLength) {
286N/A Class arrayClass = a.getClass();
286N/A Class componentType = arrayClass.getComponentType();
286N/A a = (Object[]) Array.newInstance(componentType, fLength);
286N/A }
286N/A toArray0(a);
286N/A if (a.length > fLength) {
286N/A a[fLength] = null;
286N/A }
286N/A return a;
286N/A }
286N/A
286N/A private void toArray0(Object[] a) {
286N/A if (fLength > 0) {
286N/A System.arraycopy(fArray, 0, a, 0, fLength);
286N/A }
286N/A }
286N/A
286N/A} // class StringListImpl