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