001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.xerces.impl.xs.util;
019:
020: import org.apache.xerces.xs.XSObject;
021: import org.apache.xerces.xs.XSObjectList;
022:
023: /**
024: * Containts a list of XSObject's.
025: *
026: * @xerces.internal
027: *
028: * @author Sandy Gao, IBM
029: *
030: * @version $Id: XSObjectListImpl.java 446723 2006-09-15 20:37:45Z mrglavas $
031: */
032: public class XSObjectListImpl implements XSObjectList {
033:
034: /**
035: * An immutable empty list.
036: */
037: public static final XSObjectList EMPTY_LIST = new XSObjectList() {
038: public int getLength() {
039: return 0;
040: }
041:
042: public XSObject item(int index) {
043: return null;
044: }
045: };
046:
047: private static final int DEFAULT_SIZE = 4;
048:
049: // The array to hold all data
050: private XSObject[] fArray = null;
051: // Number of elements in this list
052: private int fLength = 0;
053:
054: public XSObjectListImpl() {
055: fArray = new XSObject[DEFAULT_SIZE];
056: fLength = 0;
057: }
058:
059: /**
060: * Construct an XSObjectList implementation
061: *
062: * @param array the data array
063: * @param length the number of elements
064: */
065: public XSObjectListImpl(XSObject[] array, int length) {
066: fArray = array;
067: fLength = length;
068: }
069:
070: /**
071: * The number of <code>XSObjects</code> in the list. The range of valid
072: * child node indices is 0 to <code>length-1</code> inclusive.
073: */
074: public int getLength() {
075: return fLength;
076: }
077:
078: /**
079: * Returns the <code>index</code>th item in the collection. The index
080: * starts at 0. If <code>index</code> is greater than or equal to the
081: * number of nodes in the list, this returns <code>null</code>.
082: * @param index index into the collection.
083: * @return The XSObject at the <code>index</code>th position in the
084: * <code>XSObjectList</code>, or <code>null</code> if that is not a
085: * valid index.
086: */
087: public XSObject item(int index) {
088: if (index < 0 || index >= fLength)
089: return null;
090: return fArray[index];
091: }
092:
093: // clear this object
094: public void clear() {
095: for (int i = 0; i < fLength; i++) {
096: fArray[i] = null;
097: }
098: fArray = null;
099: fLength = 0;
100: }
101:
102: public void add(XSObject object) {
103: if (fLength == fArray.length) {
104: XSObject[] temp = new XSObject[fLength + 4];
105: System.arraycopy(fArray, 0, temp, 0, fLength);
106: fArray = temp;
107: }
108: fArray[fLength++] = object;
109: }
110:
111: public void add(int index, XSObject object) {
112: fArray[index] = object;
113: }
114:
115: } // class XSObjectList
|