001: /* Copyright 2002, 2003, 2005 Elliotte Rusty Harold
002:
003: This library is free software; you can redistribute it and/or modify
004: it under the terms of version 2.1 of the GNU Lesser General Public
005: License as published by the Free Software Foundation.
006:
007: This library is distributed in the hope that it will be useful,
008: but WITHOUT ANY WARRANTY; without even the implied warranty of
009: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
010: GNU Lesser General Public License for more details.
011:
012: You should have received a copy of the GNU Lesser General Public
013: License along with this library; if not, write to the
014: Free Software Foundation, Inc., 59 Temple Place, Suite 330,
015: Boston, MA 02111-1307 USA
016:
017: You can contact Elliotte Rusty Harold by sending e-mail to
018: elharo@metalab.unc.edu. Please include the word "XOM" in the
019: subject line. The XOM home page is located at http://www.xom.nu/
020: */
021:
022: package nu.xom;
023:
024: import java.util.ArrayList;
025: import java.util.List;
026:
027: /**
028: *
029: * <p>
030: * Implements a list of nodes for traversal purposes.
031: * Changes to the document from which this list was generated
032: * are not reflected in this list, nor are changes to the list
033: * reflected in the document. Changes to the individual
034: * <code>Node</code> objects in the list and the document
035: * are reflected in the other one.
036: * </p>
037: *
038: * <p>
039: * There is no requirement that the list not contain duplicates,
040: * or that all the members come from the same document. It is simply
041: * a list of nodes.
042: * </p>
043: *
044: * @author Elliotte Rusty Harold
045: * @version 1.1b4
046: *
047: */
048: public final class Nodes {
049:
050: private final List nodes;
051:
052: /**
053: * <p>
054: * Creates an empty node list.
055: * </p>
056: */
057: public Nodes() {
058: nodes = new ArrayList();
059: }
060:
061: /**
062: * <p>
063: * Creates a node list containing a single node.
064: * </p>
065: *
066: * @param node the node to insert in the list
067: *
068: * @throws NullPointerException if <code>node</code> is null
069: */
070: public Nodes(Node node) {
071:
072: if (node == null) {
073: throw new NullPointerException(
074: "Nodes content must be non-null");
075: }
076: nodes = new ArrayList(1);
077: nodes.add(node);
078:
079: }
080:
081: Nodes(List results) {
082: this .nodes = results;
083: }
084:
085: /**
086: * <p>
087: * Returns the number of nodes in the list.
088: * This is guaranteed to be non-negative.
089: * </p>
090: *
091: * @return the number of nodes in the list
092: */
093: public int size() {
094: return nodes.size();
095: }
096:
097: /**
098: * <p>
099: * Returns the index<sup>th</sup> node in the list.
100: * The first node has index 0. The last node
101: * has index <code>size()-1</code>.
102: * </p>
103: *
104: * @param index the node to return
105: *
106: * @return the node at the specified position
107: *
108: * @throws IndexOutOfBoundsException if <code>index</code> is
109: * negative or greater than or equal to the size of the list
110: */
111: public Node get(int index) {
112: return (Node) nodes.get(index);
113: }
114:
115: /**
116: * <p>
117: * Removes the index<sup>th</sup>node in the list.
118: * Subsequent nodes have their indexes reduced by one.
119: * </p>
120: *
121: * @param index the node to remove
122: *
123: * @return the node at the specified position
124: *
125: * @throws <code>IndexOutOfBoundsException</code> if index is
126: * negative or greater than or equal to the size of the list
127: */
128: public Node remove(int index) {
129: return (Node) nodes.remove(index);
130: }
131:
132: /**
133: * <p>
134: * Inserts a node at the index<sup>th</sup> position in the list.
135: * Subsequent nodes have their indexes increased by one.
136: * </p>
137: *
138: * @param node the node to insert
139: * @param index the position at which to insert the node
140: *
141: * @throws IndexOutOfBoundsException if <code>index</code> is
142: * negative or strictly greater than the size of the list
143: * @throws NullPointerException if <code>node</code> is null
144: */
145: public void insert(Node node, int index) {
146: if (node == null) {
147: throw new NullPointerException(
148: "Nodes content must be non-null");
149: }
150: nodes.add(index, node);
151: }
152:
153: /**
154: * <p>
155: * Adds a node at the end of this list.
156: * </p>
157: *
158: * @param node the node to add to the list
159: *
160: * @throws NullPointerException if <code>node</code> is null
161: */
162: public void append(Node node) {
163: if (node == null) {
164: throw new NullPointerException(
165: "Nodes content must be non-null");
166: }
167: nodes.add(node);
168: }
169:
170: /**
171: * <p>
172: * Determines whether a node is contained in this list.
173: * </p>
174: *
175: * @param node the node to search for
176: *
177: * @return true if this list contains the specified node;
178: * false otherwise
179: */
180: public boolean contains(Node node) {
181: return nodes.contains(node);
182: }
183:
184: }
|