1
2
3
4
5
6
7
8
9
10 package org.dom4j.tree;
11
12 import java.util.AbstractList;
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>ContentListFacade</code> represents a facade of the
21 * content of a {@link org.dom4j.Branch} which is returned via calls to the
22 * {@link org.dom4j.Branch#content} method to allow users to modify the content
23 * of a {@link org.dom4j.Branch} directly using the {@link List} interface.
24 * This list is backed by the branch such that changes to the list will
25 * be reflected in the branch and changes to the branch will be reflected
26 * be reflected in this list.</p>
27 *
28 * @author <a href="mailto:james.strachan@metastuff.com">James Strachan</a>
29 * @version $Revision: 1.8 $
30 */
31 public class ContentListFacade extends AbstractList {
32
33 /*** The content of the Branch which is modified if I am modified */
34 private List branchContent;
35
36 /*** The <code>AbstractBranch</code> instance which owns the content */
37 private AbstractBranch branch;
38
39
40 public ContentListFacade(AbstractBranch branch, List branchContent) {
41 this.branch = branch;
42 this.branchContent = branchContent;
43 }
44
45 public boolean add(Object object) {
46 branch.childAdded( asNode( object ) );
47 return branchContent.add(object);
48 }
49
50 public void add(int index, Object object) {
51 branch.childAdded( asNode( object ) );
52 branchContent.add(index, object);
53 }
54
55 public Object set(int index, Object object) {
56 branch.childAdded( asNode( object ) );
57 return branchContent.set(index, object);
58 }
59
60 public boolean remove(Object object) {
61 branch.childRemoved( asNode( object ) );
62 return branchContent.remove(object);
63 }
64
65 public Object remove(int index) {
66 Object object = branchContent.remove(index);
67 if ( object != null ) {
68 branch.childRemoved( asNode( object ) );
69 }
70 return object;
71 }
72
73 public boolean addAll(Collection collection) {
74 int count = branchContent.size();
75 for (Iterator iter = collection.iterator(); iter.hasNext(); count++ ) {
76 add(iter.next());
77 }
78 return count == branchContent.size();
79 }
80
81 public boolean addAll(int index, Collection collection) {
82 int count = branchContent.size();
83 for (Iterator iter = collection.iterator(); iter.hasNext(); count-- ) {
84 add(index++, iter.next());
85 }
86 return count == branchContent.size();
87 }
88
89 public void clear() {
90 for ( Iterator iter = iterator(); iter.hasNext(); ) {
91 Object object = iter.next();
92 branch.childRemoved( asNode( object ) );
93 }
94 branchContent.clear();
95 }
96
97 public boolean removeAll(Collection c) {
98 for ( Iterator iter = c.iterator(); iter.hasNext(); ) {
99 Object object = iter.next();
100 branch.childRemoved( asNode( object ) );
101 }
102 return branchContent.removeAll(c);
103 }
104
105 public int size() {
106 return branchContent.size();
107 }
108
109 public boolean isEmpty() {
110 return branchContent.isEmpty();
111 }
112
113 public boolean contains(Object o) {
114 return branchContent.contains(o);
115 }
116
117 public Object[] toArray() {
118 return branchContent.toArray();
119 }
120
121 public Object[] toArray(Object[] a) {
122 return branchContent.toArray(a);
123 }
124
125 public boolean containsAll(Collection c) {
126 return branchContent.containsAll(c);
127 }
128
129 public Object get(int index) {
130 return branchContent.get(index);
131 }
132
133 public int indexOf(Object o) {
134 return branchContent.indexOf(o);
135 }
136
137 public int lastIndexOf(Object o) {
138 return branchContent.lastIndexOf(o);
139 }
140
141 protected Node asNode(Object object) {
142 if (object instanceof Node) {
143 return (Node) object;
144 }
145 else {
146 throw new IllegalAddException( "This list must contain instances of Node. Invalid type: "+ object );
147 }
148 }
149
150 protected List getBackingList() {
151 return branchContent;
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
200
201