KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xerces > impl > xs > util > XSObjectListImpl


1 /*
2  * Copyright 2002,2003-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.xerces.impl.xs.util;
18
19 import org.apache.xerces.xs.XSObject;
20 import org.apache.xerces.xs.XSObjectList;
21
22 /**
23  * Containts a list of XSObject's.
24  *
25  * @xerces.internal
26  *
27  * @author Sandy Gao, IBM
28  *
29  * @version $Id: XSObjectListImpl.java,v 1.11 2004/12/03 15:50:44 mrglavas Exp $
30  */

31 public class XSObjectListImpl implements XSObjectList {
32
33     /**
34      * An immutable empty list.
35      */

36     public static final XSObjectList EMPTY_LIST = new XSObjectList () {
37         public int getLength() {
38             return 0;
39         }
40         public XSObject item(int index) {
41             return null;
42         }
43     };
44     
45     private static final int DEFAULT_SIZE = 4;
46
47     // The array to hold all data
48
private XSObject[] fArray = null;
49     // Number of elements in this list
50
private int fLength = 0;
51     
52
53
54     public XSObjectListImpl() {
55         fArray = new XSObject[DEFAULT_SIZE];
56         fLength = 0;
57     }
58
59     /**
60      * Construct an XSObjectList implementation
61      *
62      * @param array the data array
63      * @param length the number of elements
64      */

65     public XSObjectListImpl(XSObject[] array, int length) {
66         fArray = array;
67         fLength = length;
68     }
69
70     /**
71      * The number of <code>XSObjects</code> in the list. The range of valid
72      * child node indices is 0 to <code>length-1</code> inclusive.
73      */

74     public int getLength() {
75         return fLength;
76     }
77
78     /**
79      * Returns the <code>index</code>th item in the collection. The index
80      * starts at 0. If <code>index</code> is greater than or equal to the
81      * number of nodes in the list, this returns <code>null</code>.
82      * @param index index into the collection.
83      * @return The XSObject at the <code>index</code>th position in the
84      * <code>XSObjectList</code>, or <code>null</code> if that is not a
85      * valid index.
86      */

87     public XSObject item(int index) {
88         if (index < 0 || index >= fLength)
89             return null;
90         return fArray[index];
91     }
92
93     // clear this object
94
public void clear() {
95         for (int i=0; i<fLength; i++) {
96             fArray[i] = null;
97         }
98         fArray = null;
99         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     public void add (int index, XSObject object){
111         fArray [index] = object;
112     }
113
114 } // class XSObjectList
115
Popular Tags