KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nu > xom > Nodes


1 /* Copyright 2002, 2003 Elliotte Rusty Harold
2    
3    This library is free software; you can redistribute it and/or modify
4    it under the terms of version 2.1 of the GNU Lesser General Public
5    License as published by the Free Software Foundation.
6    
7    This library is distributed in the hope that it will be useful,
8    but WITHOUT ANY WARRANTY; without even the implied warranty of
9    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10    GNU Lesser General Public License for more details.
11    
12    You should have received a copy of the GNU Lesser General Public
13    License along with this library; if not, write to the
14    Free Software Foundation, Inc., 59 Temple Place, Suite 330,
15    Boston, MA 02111-1307 USA
16    
17    You can contact Elliotte Rusty Harold by sending e-mail to
18    elharo@metalab.unc.edu. Please include the word "XOM" in the
19    subject line. The XOM home page is located at http://www.xom.nu/
20 */

21
22 package nu.xom;
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.List JavaDoc;
26
27 /**
28  *
29  * <p>
30  * Implements a list of nodes for traversal purposes.
31  * Changes to the document from which this list was generated
32  * are not reflected in this list, nor are changes to the list
33  * reflected in the document. Changes to the individual
34  * <code>Node</code> objects in the list and the document
35  * are reflected in the other one.
36  * </p>
37  *
38  * <p>
39  * There is no requirement that the list not contain duplicates,
40  * or that all the members come from the same document. It is simply
41  * a list of nodes.
42  * </p>
43  *
44  * @author Elliotte Rusty Harold
45  * @version 1.0
46  *
47  */

48 public final class Nodes {
49     
50     private List JavaDoc nodes;
51     
52     
53     /**
54      * <p>
55      * Creates an empty node list.
56      * </p>
57      */

58     public Nodes() {
59         nodes = new ArrayList JavaDoc();
60     }
61     
62     
63     /**
64      * <p>
65      * Creates a node list containing a single node.
66      * </p>
67      *
68      * @param node the node to insert in the list
69      */

70     public Nodes(Node node) {
71         nodes = new ArrayList JavaDoc(1);
72         nodes.add(node);
73     }
74     
75     
76     /**
77      * <p>
78      * Returns the number of nodes in the list.
79      * This is guaranteed to be non-negative.
80      * </p>
81      *
82      * @return the number of nodes in the list
83      */

84     public int size() {
85         return nodes.size();
86     }
87     
88     
89     /**
90      * <p>
91      * Returns the index<sup>th</sup> node in the list.
92      * The first node has index 0. The last node
93      * has index <code>size()-1</code>.
94      * </p>
95      *
96      * @param index the node to return
97      *
98      * @return the node at the specified position
99      *
100      * @throws <code>IndexOutOfBoundsException</code> if index is
101      * negative or greater than or equal to the size of the list
102      */

103     public Node get(int index) {
104         return (Node) nodes.get(index);
105     }
106
107     
108     /**
109      * <p>
110      * Removes the index<sup>th</sup>node in the list.
111      * Subsequent nodes have their indexes reduced by one.
112      * </p>
113      *
114      * @param index the node to remove
115      *
116      * @return the node at the specified position
117      *
118      * @throws <code>IndexOutOfBoundsException</code> if index is
119      * negative or greater than or equal to the size of the list
120      */

121     public Node remove(int index) {
122         return (Node) nodes.remove(index);
123     }
124     
125     
126     /**
127      * <p>
128      * Inserts a node at the index<sup>th</sup> position in the list.
129      * Subsequent nodes have their indexes increased by one.
130      * </p>
131      *
132      * @param node the node to insert
133      * @param index the position at which to insert the node
134      *
135      * @throws <code>IndexOutOfBoundsException</code> if index is
136      * negative or strictly greater than the size of the list
137      */

138     public void insert(Node node, int index) {
139         nodes.add(index, node);
140     }
141     
142     
143     /**
144      * <p>
145      * Adds a node at the end of this list.
146      * </p>
147      *
148      * @param node the node to add to the list
149      */

150     public void append(Node node) {
151         nodes.add(node);
152     }
153
154     
155 }
Popular Tags