annotate NGSrich_0.5.5/src/org/jdom/ContentList.java @ 0:89ad0a9cca52 default tip

Uploaded
author pfrommolt
date Mon, 21 Nov 2011 08:12:19 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
1 /*--
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
2
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
3 $Id: ContentList.java,v 1.42 2007/11/10 05:28:58 jhunter Exp $
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
4
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
5 Copyright (C) 2000-2007 Jason Hunter & Brett McLaughlin.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
6 All rights reserved.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
7
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
8 Redistribution and use in source and binary forms, with or without
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
9 modification, are permitted provided that the following conditions
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
10 are met:
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
11
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
12 1. Redistributions of source code must retain the above copyright
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
13 notice, this list of conditions, and the following disclaimer.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
14
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
15 2. Redistributions in binary form must reproduce the above copyright
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
16 notice, this list of conditions, and the disclaimer that follows
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
17 these conditions in the documentation and/or other materials
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
18 provided with the distribution.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
19
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
20 3. The name "JDOM" must not be used to endorse or promote products
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
21 derived from this software without prior written permission. For
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
22 written permission, please contact <request_AT_jdom_DOT_org>.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
23
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
24 4. Products derived from this software may not be called "JDOM", nor
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
25 may "JDOM" appear in their name, without prior written permission
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
26 from the JDOM Project Management <request_AT_jdom_DOT_org).
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
27
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
28 In addition, we request (but do not require) that you include in the
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
29 end-user documentation provided with the redistribution and/or in the
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
30 software itself an acknowledgement equivalent to the following:
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
31 "This product includes software developed by the
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
32 JDOM Project (http://www.jdom.org/)."
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
33 Alternatively, the acknowledgment may be graphical using the logos
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
34 available at http://www.jdom.org/images/logos.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
35
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
36 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
37 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
38 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
39 DISCLAIMED. IN NO EVENT SHALL THE JDOM AUTHORS OR THE PROJECT
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
40 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
41 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
42 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
43 USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
44 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
45 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
46 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
47 SUCH DAMAGE.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
48
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
49 This software consists of voluntary contributions made by many
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
50 individuals on behalf of the JDOM Project and was originally
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
51 created by Jason Hunter <jhunter_AT_jdom_DOT_org> and
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
52 Brett McLaughlin <brett_AT_jdom_DOT_org>. For more information
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
53 on the JDOM Project, please see <http://www.jdom.org/>.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
54
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
55 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
56
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
57 package org.jdom;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
58
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
59 import java.util.*;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
60
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
61 import org.jdom.filter.*;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
62
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
63 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
64 * A non-public list implementation holding only legal JDOM content, including
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
65 * content for Document or Element nodes. Users see this class as a simple List
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
66 * implementation.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
67 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
68 * @see CDATA
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
69 * @see Comment
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
70 * @see Element
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
71 * @see EntityRef
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
72 * @see ProcessingInstruction
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
73 * @see Text
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
74 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
75 * @version $Revision: 1.42 $, $Date: 2007/11/10 05:28:58 $
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
76 * @author Alex Rosen
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
77 * @author Philippe Riand
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
78 * @author Bradley S. Huffman
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
79 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
80 final class ContentList extends AbstractList implements java.io.Serializable {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
81
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
82 private static final String CVS_ID =
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
83 "@(#) $RCSfile: ContentList.java,v $ $Revision: 1.42 $ $Date: 2007/11/10 05:28:58 $ $Name: jdom_1_1_1 $";
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
84
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
85 private static final long serialVersionUID = 1L;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
86
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
87 private static final int INITIAL_ARRAY_SIZE = 5;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
88
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
89 /** Our backing list */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
90 private Content elementData[];
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
91 private int size;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
92
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
93 /** Document or Element this list belongs to */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
94 private Parent parent;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
95
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
96 /** Force either a Document or Element parent */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
97 ContentList(Parent parent) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
98 this.parent = parent;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
99 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
100
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
101 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
102 * Package internal method to support building from sources that are
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
103 * 100% trusted.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
104 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
105 * @param c content to add without any checks
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
106 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
107 final void uncheckedAddContent(Content c) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
108 c.parent = parent;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
109 ensureCapacity(size + 1);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
110 elementData[size++] = c;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
111 modCount++;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
112 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
113
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
114 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
115 * Inserts the specified object at the specified position in this list.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
116 * Shifts the object currently at that position (if any) and any
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
117 * subsequent objects to the right (adds one to their indices).
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
118 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
119 * @param index The location to set the value to.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
120 * @param obj The object to insert into the list.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
121 * throws IndexOutOfBoundsException if index < 0 || index > size()
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
122 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
123 public void add(int index, Object obj) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
124 if (obj == null) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
125 throw new IllegalAddException("Cannot add null object");
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
126 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
127 if (obj instanceof String) { // String is OK to add as special case
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
128 obj = new Text(obj.toString()); // wrap it as a Content
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
129 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
130 if ((obj instanceof Content)) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
131 add(index, (Content) obj);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
132 } else {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
133 throw new IllegalAddException("Class " +
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
134 obj.getClass().getName() +
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
135 " is of unrecognized type and cannot be added");
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
136 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
137 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
138
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
139 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
140 * @see org.jdom.ContentList#add(int, org.jdom.Content)
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
141 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
142 private void documentCanContain(int index, Content child) throws IllegalAddException {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
143 if (child instanceof Element) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
144 if (indexOfFirstElement() >= 0) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
145 throw new IllegalAddException(
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
146 "Cannot add a second root element, only one is allowed");
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
147 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
148 if (indexOfDocType() > index) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
149 throw new IllegalAddException(
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
150 "A root element cannot be added before the DocType");
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
151 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
152 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
153 if (child instanceof DocType) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
154 if (indexOfDocType() >= 0) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
155 throw new IllegalAddException(
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
156 "Cannot add a second doctype, only one is allowed");
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
157 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
158 int firstElt = indexOfFirstElement();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
159 if (firstElt != -1 && firstElt < index) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
160 throw new IllegalAddException(
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
161 "A DocType cannot be added after the root element");
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
162 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
163 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
164 if (child instanceof CDATA) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
165 throw new IllegalAddException("A CDATA is not allowed at the document root");
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
166 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
167
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
168 if (child instanceof Text) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
169 throw new IllegalAddException("A Text is not allowed at the document root");
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
170 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
171
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
172 if (child instanceof EntityRef) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
173 throw new IllegalAddException("An EntityRef is not allowed at the document root");
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
174 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
175 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
176
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
177 private static void elementCanContain(int index, Content child) throws IllegalAddException {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
178 if (child instanceof DocType) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
179 throw new IllegalAddException(
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
180 "A DocType is not allowed except at the document level");
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
181 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
182 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
183
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
184 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
185 * Check and add the <code>Element</code> to this list at
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
186 * the given index.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
187 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
188 * @param index index where to add <code>Element</code>
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
189 * @param child <code>Element</code> to add
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
190 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
191 void add(int index, Content child) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
192 if (child == null) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
193 throw new IllegalAddException("Cannot add null object");
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
194 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
195 if (parent instanceof Document) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
196 documentCanContain(index, child);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
197 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
198 else {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
199 elementCanContain(index, child);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
200 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
201
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
202 if (child.getParent() != null) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
203 Parent p = child.getParent();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
204 if (p instanceof Document) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
205 throw new IllegalAddException((Element)child,
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
206 "The Content already has an existing parent document");
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
207 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
208 else {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
209 throw new IllegalAddException(
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
210 "The Content already has an existing parent \"" +
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
211 ((Element)p).getQualifiedName() + "\"");
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
212 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
213 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
214
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
215 if (child == parent) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
216 throw new IllegalAddException(
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
217 "The Element cannot be added to itself");
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
218 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
219
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
220 // Detect if we have <a><b><c/></b></a> and c.add(a)
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
221 if ((parent instanceof Element && child instanceof Element) &&
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
222 ((Element) child).isAncestor((Element)parent)) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
223 throw new IllegalAddException(
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
224 "The Element cannot be added as a descendent of itself");
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
225 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
226
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
227 if (index<0 || index>size) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
228 throw new IndexOutOfBoundsException("Index: " + index +
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
229 " Size: " + size());
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
230 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
231
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
232 child.setParent(parent);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
233
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
234 ensureCapacity(size+1);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
235 if( index==size ) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
236 elementData[size++] = child;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
237 } else {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
238 System.arraycopy(elementData, index, elementData, index + 1, size - index);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
239 elementData[index] = child;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
240 size++;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
241 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
242 modCount++;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
243 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
244
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
245 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
246 * Add the specified collecton to the end of this list.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
247 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
248 * @param collection The collection to add to the list.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
249 * @return <code>true</code> if the list was modified as a result of
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
250 * the add.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
251 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
252 public boolean addAll(Collection collection) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
253 return addAll(size(), collection);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
254 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
255
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
256 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
257 * Inserts the specified collecton at the specified position in this list.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
258 * Shifts the object currently at that position (if any) and any
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
259 * subsequent objects to the right (adds one to their indices).
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
260 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
261 * @param index The offset to start adding the data in the collection
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
262 * @param collection The collection to insert into the list.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
263 * @return <code>true</code> if the list was modified as a result of
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
264 * the add.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
265 * throws IndexOutOfBoundsException if index < 0 || index > size()
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
266 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
267 public boolean addAll(int index, Collection collection) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
268 if (index<0 || index>size) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
269 throw new IndexOutOfBoundsException("Index: " + index +
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
270 " Size: " + size());
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
271 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
272
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
273 if ((collection == null) || (collection.size() == 0)) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
274 return false;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
275 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
276 ensureCapacity(size() + collection.size());
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
277
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
278 int count = 0;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
279 try {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
280 Iterator i = collection.iterator();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
281 while (i.hasNext()) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
282 Object obj = i.next();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
283 add(index + count, obj);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
284 count++;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
285 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
286 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
287 catch (RuntimeException exception) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
288 for (int i = 0; i < count; i++) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
289 remove(index);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
290 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
291 throw exception;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
292 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
293
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
294 return true;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
295 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
296
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
297 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
298 * Clear the current list.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
299 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
300 public void clear() {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
301 if (elementData != null) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
302 for (int i = 0; i < size; i++) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
303 Content obj = elementData[i];
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
304 removeParent(obj);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
305 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
306 elementData = null;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
307 size = 0;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
308 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
309 modCount++;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
310 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
311
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
312 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
313 * Clear the current list and set it to the contents
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
314 * of the <code>Collection</code>.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
315 * object.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
316 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
317 * @param collection The collection to use.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
318 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
319 void clearAndSet(Collection collection) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
320 Content[] old = elementData;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
321 int oldSize = size;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
322
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
323 elementData = null;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
324 size = 0;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
325
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
326 if ((collection != null) && (collection.size() != 0)) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
327 ensureCapacity(collection.size());
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
328 try {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
329 addAll(0, collection);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
330 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
331 catch (RuntimeException exception) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
332 elementData = old;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
333 size = oldSize;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
334 throw exception;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
335 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
336 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
337
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
338 if (old != null) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
339 for (int i = 0; i < oldSize; i++) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
340 removeParent(old[i]);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
341 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
342 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
343 modCount++;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
344 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
345
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
346 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
347 * Increases the capacity of this <code>ContentList</code> instance,
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
348 * if necessary, to ensure that it can hold at least the number of
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
349 * items specified by the minimum capacity argument.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
350 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
351 * @param minCapacity the desired minimum capacity.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
352 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
353 void ensureCapacity(int minCapacity) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
354 if( elementData==null ) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
355 elementData = new Content[Math.max(minCapacity, INITIAL_ARRAY_SIZE)];
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
356 } else {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
357 int oldCapacity = elementData.length;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
358 if (minCapacity > oldCapacity) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
359 Object oldData[] = elementData;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
360 int newCapacity = (oldCapacity * 3)/2 + 1;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
361 if (newCapacity < minCapacity)
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
362 newCapacity = minCapacity;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
363 elementData = new Content[newCapacity];
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
364 System.arraycopy(oldData, 0, elementData, 0, size);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
365 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
366 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
367 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
368
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
369 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
370 * Return the object at the specified offset.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
371 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
372 * @param index The offset of the object.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
373 * @return The Object which was returned.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
374 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
375 public Object get(int index) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
376 if (index<0 || index>=size) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
377 throw new IndexOutOfBoundsException("Index: " + index +
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
378 " Size: " + size());
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
379 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
380 return elementData[index];
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
381 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
382
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
383 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
384 * Return a view of this list based on the given filter.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
385 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
386 * @param filter <code>Filter</code> for this view.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
387 * @return a list representing the rules of the <code>Filter</code>.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
388 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
389 List getView(Filter filter) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
390 return new FilterList(filter);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
391 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
392
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
393 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
394 * Return the index of the first Element in the list. If the parent
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
395 * is a <code>Document</code> then the element is the root element.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
396 * If the list contains no Elements, it returns -1.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
397 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
398 * @return index of first element, or -1 if one doesn't exist
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
399 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
400 int indexOfFirstElement() {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
401 if( elementData!=null ) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
402 for (int i = 0; i < size; i++) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
403 if (elementData[i] instanceof Element) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
404 return i;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
405 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
406 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
407 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
408 return -1;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
409 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
410
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
411 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
412 * Return the index of the DocType element in the list. If the list contains
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
413 * no DocType, it returns -1.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
414 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
415 * @return index of the DocType, or -1 if it doesn't
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
416 * exist
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
417 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
418 int indexOfDocType() {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
419 if (elementData != null) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
420 for (int i = 0; i < size; i++) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
421 if (elementData[i] instanceof DocType) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
422 return i;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
423 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
424 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
425 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
426 return -1;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
427 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
428
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
429 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
430 * Remove the object at the specified offset.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
431 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
432 * @param index The offset of the object.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
433 * @return The Object which was removed.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
434 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
435 public Object remove(int index) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
436 if (index<0 || index>=size)
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
437 throw new IndexOutOfBoundsException("Index: " + index +
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
438 " Size: " + size());
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
439
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
440 Content old = elementData[index];
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
441 removeParent(old);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
442 int numMoved = size - index - 1;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
443 if (numMoved > 0)
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
444 System.arraycopy(elementData, index+1, elementData, index,numMoved);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
445 elementData[--size] = null; // Let gc do its work
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
446 modCount++;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
447 return old;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
448 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
449
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
450
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
451 /** Remove the parent of a Object */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
452 private static void removeParent(Content c) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
453 c.setParent(null);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
454 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
455
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
456 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
457 * Set the object at the specified location to the supplied
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
458 * object.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
459 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
460 * @param index The location to set the value to.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
461 * @param obj The location to set the value to.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
462 * @return The object which was replaced.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
463 * throws IndexOutOfBoundsException if index < 0 || index >= size()
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
464 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
465 public Object set(int index, Object obj) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
466 if (index<0 || index>=size)
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
467 throw new IndexOutOfBoundsException("Index: " + index +
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
468 " Size: " + size());
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
469
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
470 if ((obj instanceof Element) && (parent instanceof Document)) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
471 int root = indexOfFirstElement();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
472 if ((root >= 0) && (root != index)) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
473 throw new IllegalAddException(
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
474 "Cannot add a second root element, only one is allowed");
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
475 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
476 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
477
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
478 if ((obj instanceof DocType) && (parent instanceof Document)) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
479 int docTypeIndex = indexOfDocType();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
480 if ((docTypeIndex >= 0) && (docTypeIndex != index)) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
481 throw new IllegalAddException(
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
482 "Cannot add a second doctype, only one is allowed");
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
483 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
484 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
485
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
486 Object old = remove(index);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
487 try {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
488 add(index, obj);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
489 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
490 catch (RuntimeException exception) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
491 add(index, old);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
492 throw exception;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
493 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
494 return old;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
495 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
496
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
497 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
498 * Return the number of items in this list
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
499 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
500 * @return The number of items in this list.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
501 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
502 public int size() {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
503 return size;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
504 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
505
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
506 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
507 * Return this list as a <code>String</code>
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
508 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
509 * @return The number of items in this list.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
510 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
511 public String toString() {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
512 return super.toString();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
513 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
514
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
515 /** Give access of ContentList.modCount to FilterList */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
516 private int getModCount() {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
517 return modCount;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
518 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
519
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
520 /* * * * * * * * * * * * * FilterList * * * * * * * * * * * * * * * */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
521 /* * * * * * * * * * * * * FilterList * * * * * * * * * * * * * * * */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
522
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
523 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
524 * <code>FilterList</code> represents legal JDOM content, including content
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
525 * for <code>Document</code>s or <code>Element</code>s.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
526 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
527
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
528 class FilterList extends AbstractList implements java.io.Serializable {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
529
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
530 /** The Filter */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
531 Filter filter;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
532
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
533 /** Current number of items in this view */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
534 int count = 0;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
535
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
536 /** Expected modCount in our backing list */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
537 int expected = -1;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
538
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
539 // Implementation Note: Directly after size() is called, expected
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
540 // is sync'd with ContentList.modCount and count provides
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
541 // the true size of this view. Before the first call to
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
542 // size() or if the backing list is modified outside this
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
543 // FilterList, both might contain bogus values and should
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
544 // not be used without first calling size();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
545
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
546 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
547 * Create a new instance of the FilterList with the specified Filter.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
548 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
549 FilterList(Filter filter) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
550 this.filter = filter;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
551 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
552
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
553 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
554 * Inserts the specified object at the specified position in this list.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
555 * Shifts the object currently at that position (if any) and any
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
556 * subsequent objects to the right (adds one to their indices).
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
557 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
558 * @param index The location to set the value to.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
559 * @param obj The object to insert into the list.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
560 * throws IndexOutOfBoundsException if index < 0 || index > size()
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
561 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
562 public void add(int index, Object obj) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
563 if (filter.matches(obj)) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
564 int adjusted = getAdjustedIndex(index);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
565 ContentList.this.add(adjusted, obj);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
566 expected++;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
567 count++;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
568 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
569 else throw new IllegalAddException("Filter won't allow the " +
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
570 obj.getClass().getName() +
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
571 " '" + obj + "' to be added to the list");
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
572 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
573
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
574 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
575 * Return the object at the specified offset.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
576 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
577 * @param index The offset of the object.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
578 * @return The Object which was returned.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
579 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
580 public Object get(int index) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
581 int adjusted = getAdjustedIndex(index);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
582 return ContentList.this.get(adjusted);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
583 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
584
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
585 public Iterator iterator() {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
586 return new FilterListIterator(filter, 0);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
587 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
588
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
589 public ListIterator listIterator() {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
590 return new FilterListIterator(filter, 0);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
591 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
592
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
593 public ListIterator listIterator(int index) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
594 return new FilterListIterator(filter, index);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
595 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
596
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
597 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
598 * Remove the object at the specified offset.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
599 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
600 * @param index The offset of the object.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
601 * @return The Object which was removed.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
602 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
603 public Object remove(int index) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
604 int adjusted = getAdjustedIndex(index);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
605 Object old = ContentList.this.get(adjusted);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
606 if (filter.matches(old)) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
607 old = ContentList.this.remove(adjusted);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
608 expected++;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
609 count--;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
610 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
611 else {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
612 throw new IllegalAddException("Filter won't allow the " +
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
613 (old.getClass()).getName() +
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
614 " '" + old + "' (index " + index +
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
615 ") to be removed");
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
616 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
617 return old;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
618 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
619
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
620 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
621 * Set the object at the specified location to the supplied
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
622 * object.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
623 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
624 * @param index The location to set the value to.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
625 * @param obj The location to set the value to.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
626 * @return The object which was replaced.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
627 * throws IndexOutOfBoundsException if index < 0 || index >= size()
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
628 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
629 public Object set(int index, Object obj) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
630 Object old = null;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
631 if (filter.matches(obj)) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
632 int adjusted = getAdjustedIndex(index);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
633 old = ContentList.this.get(adjusted);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
634 if (!filter.matches(old)) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
635 throw new IllegalAddException("Filter won't allow the " +
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
636 (old.getClass()).getName() +
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
637 " '" + old + "' (index " + index +
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
638 ") to be removed");
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
639 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
640 old = ContentList.this.set(adjusted, obj);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
641 expected += 2;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
642 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
643 else {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
644 throw new IllegalAddException("Filter won't allow index " +
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
645 index + " to be set to " +
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
646 (obj.getClass()).getName());
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
647 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
648 return old;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
649 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
650
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
651 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
652 * Return the number of items in this list
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
653 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
654 * @return The number of items in this list.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
655 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
656 public int size() {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
657 // Implementation Note: Directly after size() is called, expected
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
658 // is sync'd with ContentList.modCount and count provides
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
659 // the true size of this view. Before the first call to
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
660 // size() or if the backing list is modified outside this
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
661 // FilterList, both might contain bogus values and should
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
662 // not be used without first calling size();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
663
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
664 if (expected == ContentList.this.getModCount()) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
665 return count;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
666 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
667
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
668 count = 0;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
669 for (int i = 0; i < ContentList.this.size(); i++) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
670 Object obj = ContentList.this.elementData[i];
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
671 if (filter.matches(obj)) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
672 count++;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
673 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
674 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
675 expected = ContentList.this.getModCount();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
676 return count;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
677 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
678
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
679 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
680 * Return the adjusted index
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
681 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
682 * @param index Index of in this view.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
683 * @return True index in backing list
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
684 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
685 final private int getAdjustedIndex(int index) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
686 int adjusted = 0;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
687 for (int i = 0; i < ContentList.this.size; i++) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
688 Object obj = ContentList.this.elementData[i];
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
689 if (filter.matches(obj)) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
690 if (index == adjusted) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
691 return i;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
692 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
693 adjusted++;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
694 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
695 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
696
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
697 if (index == adjusted) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
698 return ContentList.this.size;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
699 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
700
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
701 return ContentList.this.size + 1;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
702 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
703 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
704
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
705 /* * * * * * * * * * * * * FilterListIterator * * * * * * * * * * * */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
706 /* * * * * * * * * * * * * FilterListIterator * * * * * * * * * * * */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
707
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
708 class FilterListIterator implements ListIterator {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
709
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
710 /** The Filter that applies */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
711 Filter filter;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
712
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
713 /** Whether this iterator is in forward or reverse. */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
714 private boolean forward = false;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
715 /** Whether a call to remove() is valid */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
716 private boolean canremove = false;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
717 /** Whether a call to set() is valid */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
718 private boolean canset = false;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
719
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
720 /** Index in backing list of next object */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
721 private int cursor = -1;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
722 /** the backing index to use if we actually DO move */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
723 private int tmpcursor = -1;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
724 /** Index in ListIterator */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
725 private int index = -1;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
726
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
727 /** Expected modCount in our backing list */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
728 private int expected = -1;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
729
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
730 /** Number of elements matching the filter. */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
731 private int fsize = 0;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
732
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
733 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
734 * Default constructor
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
735 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
736 FilterListIterator(Filter filter, int start) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
737 this.filter = filter;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
738 expected = ContentList.this.getModCount();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
739 // always start list iterators in backward mode ....
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
740 // it makes sense... really.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
741 forward = false;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
742
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
743 if (start < 0) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
744 throw new IndexOutOfBoundsException("Index: " + start);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
745 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
746
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
747 // the number of matching elements....
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
748 fsize = 0;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
749
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
750 // go through the list, count the matching elements...
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
751 for (int i = 0; i < ContentList.this.size(); i++) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
752 if (filter.matches(ContentList.this.get(i))) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
753 if (start == fsize) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
754 // set the back-end cursor to the matching element....
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
755 cursor = i;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
756 // set the front-end cursor too.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
757 index = fsize;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
758 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
759 fsize++;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
760 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
761 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
762
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
763 if (start > fsize) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
764 throw new IndexOutOfBoundsException("Index: " + start + " Size: " + fsize);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
765 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
766 if (cursor == -1) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
767 // implies that start == fsize (i.e. after the last element
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
768 // put the insertion point at the end of the Underlying
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
769 // content list ....
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
770 // i.e. an add() at this point may potentially end up with
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
771 // filtered content between previous() and next()
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
772 // the alternative is to put the cursor on the Content after
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
773 // the last Content that the filter passed
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
774 // The implications are ambiguous.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
775 cursor = ContentList.this.size();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
776 index = fsize;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
777 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
778
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
779 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
780
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
781 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
782 * Returns <code>true</code> if this list iterator has a next element.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
783 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
784 public boolean hasNext() {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
785 return nextIndex() < fsize;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
786 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
787
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
788 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
789 * Returns the next element in the list.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
790 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
791 public Object next() {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
792 if (!hasNext())
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
793 throw new NoSuchElementException("next() is beyond the end of the Iterator");
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
794 index = nextIndex();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
795 cursor = tmpcursor;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
796 forward = true;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
797 canremove = true;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
798 canset = true;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
799 return ContentList.this.get(cursor);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
800 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
801
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
802 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
803 * Returns <code>true</code> if this list iterator has more elements
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
804 * when traversing the list in the reverse direction.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
805 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
806 public boolean hasPrevious() {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
807 return previousIndex() >= 0;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
808 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
809
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
810 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
811 * Returns the previous element in the list.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
812 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
813 public Object previous() {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
814 if (!hasPrevious())
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
815 throw new NoSuchElementException("previous() is before the start of the Iterator");
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
816 index = previousIndex();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
817 cursor = tmpcursor;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
818 forward = false;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
819 canremove = true;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
820 canset = true;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
821 return ContentList.this.get(cursor);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
822 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
823
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
824 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
825 * Returns the index of the element that would be returned by a
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
826 * subsequent call to <code>next</code>.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
827 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
828 public int nextIndex() {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
829 checkConcurrentModification();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
830 if (forward) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
831 // Starting with next possibility ....
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
832 for (int i = cursor + 1; i < ContentList.this.size(); i++) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
833 if (filter.matches(ContentList.this.get(i))) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
834 tmpcursor = i;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
835 return index + 1;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
836 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
837 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
838 // Never found another match.... put the insertion point at
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
839 // the end of the list....
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
840 tmpcursor = ContentList.this.size();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
841 return index + 1;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
842 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
843
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
844 // We've been going back... so nextIndex() returns the same
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
845 // element.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
846 tmpcursor = cursor;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
847 return index;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
848 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
849
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
850 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
851 * Returns the index of the element that would be returned by a
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
852 * subsequent call to <code>previous</code>. (Returns -1 if the
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
853 * list iterator is at the beginning of the list.)
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
854 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
855 public int previousIndex() {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
856 checkConcurrentModification();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
857 if (!forward) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
858 // starting with next possibility ....
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
859 for (int i = cursor - 1; i >= 0; i--) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
860 if (filter.matches(ContentList.this.get(i))) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
861 tmpcursor = i;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
862 return index - 1;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
863 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
864 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
865 // Never found another match.... put the insertion point at
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
866 // the start of the list....
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
867 tmpcursor = -1;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
868 return index - 1;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
869 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
870
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
871 // We've been going forwards... so previousIndex() returns same
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
872 // element.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
873 tmpcursor = cursor;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
874 return index;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
875 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
876
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
877 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
878 * Inserts the specified element into the list.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
879 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
880 public void add(Object obj) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
881 // Call to nextIndex() will check concurrent.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
882 nextIndex();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
883 // tmpcursor is the backing cursor of the next element
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
884 // Remember that List.add(index,obj) is really an insert....
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
885 ContentList.this.add(tmpcursor, obj);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
886 forward = true;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
887 expected = ContentList.this.getModCount();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
888 canremove = canset = false;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
889 index = nextIndex();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
890 cursor = tmpcursor;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
891 fsize++;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
892 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
893
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
894 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
895 * Removes from the list the last element that was returned by
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
896 * the last call to <code>next</code> or <code>previous</code>.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
897 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
898 public void remove() {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
899 if (!canremove)
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
900 throw new IllegalStateException("Can not remove an "
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
901 + "element unless either next() or previous() has been called "
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
902 + "since the last remove()");
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
903 nextIndex(); // to get out cursor ...
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
904 ContentList.this.remove(cursor);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
905 cursor = tmpcursor - 1;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
906 expected = ContentList.this.getModCount();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
907
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
908 forward = false;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
909 canremove = false;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
910 canset = false;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
911 fsize--;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
912 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
913
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
914 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
915 * Replaces the last element returned by <code>next</code> or
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
916 * <code>previous</code> with the specified element.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
917 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
918 public void set(Object obj) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
919 if (!canset)
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
920 throw new IllegalStateException("Can not set an element "
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
921 + "unless either next() or previous() has been called since the "
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
922 + "last remove() or set()");
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
923 checkConcurrentModification();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
924
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
925 if (!filter.matches(obj)) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
926 throw new IllegalAddException("Filter won't allow index " + index + " to be set to "
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
927 + (obj.getClass()).getName());
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
928 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
929
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
930 ContentList.this.set(cursor, obj);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
931 expected = ContentList.this.getModCount();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
932
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
933 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
934
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
935 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
936 * Check if are backing list is being modified by someone else.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
937 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
938 private void checkConcurrentModification() {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
939 if (expected != ContentList.this.getModCount()) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
940 throw new ConcurrentModificationException();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
941 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
942 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
943 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
944 }