1
2
3
4
5
6
7
8
9
10 package org.dom4j.tree;
11
12 import java.util.ArrayList;
13 import java.util.Collection;
14 import java.util.Iterator;
15 import java.util.List;
16
17 import org.dom4j.IllegalAddException;
18 import org.dom4j.Node;
19
20 /*** <p><code>BackedList</code> represents a list of content
21 * of a {@link org.dom4j.Branch}. Changes to the list will
22 * be reflected in the branch, though changes to the branch will not
23 * be reflected in this list.</p>
24 *
25 * @author <a href="mailto:james.strachan@metastuff.com">James Strachan</a>
26 * @version $Revision: 1.12 $
27 */
28 public class BackedList extends ArrayList {
29
30 /*** The content of the Branch which is modified if I am modified */
31 private List branchContent;
32
33 /*** The <code>AbstractBranch</code> instance which owns the content */
34 private AbstractBranch branch;
35
36
37 public BackedList(AbstractBranch branch,List branchContent) {
38 this( branch, branchContent, branchContent.size() );
39 }
40
41 public BackedList(AbstractBranch branch,List branchContent,int capacity) {
42 super( capacity );
43 this.branch = branch;
44 this.branchContent = branchContent;
45 }
46
47 public BackedList(AbstractBranch branch,List branchContent,List initialContent) {
48 super( initialContent );
49 this.branch = branch;
50 this.branchContent = branchContent;
51 }
52
53 public boolean add(Object object) {
54 branch.addNode( asNode( object ) );
55 return super.add(object);
56 }
57
58 public void add(int index, Object object) {
59 int size = size();
60 if ( index < 0 ) {
61 throw new IndexOutOfBoundsException( "Index value: " + index + " is less than zero" );
62 }
63 else if ( index > size ) {
64 throw new IndexOutOfBoundsException( "Index value: " + index + " cannot be greater than the size: " + size );
65 }
66
67 int realIndex = size == 0
68 ? branchContent.size()
69 : index < size
70 ? branchContent.indexOf(get(index))
71 : (branchContent.indexOf(get(size - 1)) + 1);
72
73 branch.addNode(realIndex, asNode( object ) );
74 super.add(index, object);
75 }
76
77 public Object set(int index, Object object) {
78 int realIndex = branchContent.indexOf( get(index) );
79 if ( realIndex < 0 ) {
80 realIndex = ( index == 0 ) ? 0 : Integer.MAX_VALUE;
81 }
82 if ( realIndex < branchContent.size() ) {
83 branch.removeNode( asNode( get(index) ) );
84 branch.addNode(realIndex, asNode( object ) );
85 }
86 else {
87 branch.removeNode( asNode( get(index) ) );
88 branch.addNode( asNode( object ) );
89 }
90 branch.childAdded( asNode( object ) );
91 return super.set(index, object);
92 }
93
94 public boolean remove(Object object) {
95 branch.removeNode( asNode( object ) );
96 return super.remove(object);
97 }
98
99 public Object remove(int index) {
100 Object object = super.remove(index);
101 if ( object != null ) {
102 branch.removeNode( asNode( object ) );
103 }
104 return object;
105 }
106
107 public boolean addAll(Collection collection) {
108 ensureCapacity(size() + collection.size());
109
110 int count = size();
111 for (Iterator iter = collection.iterator(); iter.hasNext(); count-- ) {
112 add(iter.next());
113 }
114 return count != 0;
115 }
116
117 public boolean addAll(int index, Collection collection) {
118 ensureCapacity(size() + collection.size());
119
120 int count = size();
121 for (Iterator iter = collection.iterator(); iter.hasNext(); count-- ) {
122 add(index++, iter.next());
123 }
124 return count != 0;
125 }
126
127 public void clear() {
128 for ( Iterator iter = iterator(); iter.hasNext(); ) {
129 Object object = iter.next();
130 branchContent.remove(object);
131 branch.childRemoved( asNode( object ) );
132 }
133 super.clear();
134 }
135
136 /*** Performs a local addition which is not forward through to the
137 * Branch or backing list
138 */
139 public void addLocal(Object object) {
140 super.add(object);
141 }
142
143 protected Node asNode(Object object) {
144 if (object instanceof Node) {
145 return (Node) object;
146 }
147 else {
148 throw new IllegalAddException( "This list must contain instances of Node. Invalid type: "+ object );
149 }
150 }
151 }
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199