|
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 |
| |
|
21 |
| |
|
22 |
| |
|
23 |
| |
|
24 |
| |
|
25 |
| |
|
26 |
| |
|
27 |
| |
|
28 |
| public class BackedList extends ArrayList { |
|
29 |
| |
|
30 |
| |
|
31 |
| private List branchContent; |
|
32 |
| |
|
33 |
| |
|
34 |
| private AbstractBranch branch; |
|
35 |
| |
|
36 |
| |
|
37 |
48326
| public BackedList(AbstractBranch branch,List branchContent) {
|
|
38 |
48326
| this( branch, branchContent, branchContent.size() );
|
|
39 |
| } |
|
40 |
| |
|
41 |
48338
| public BackedList(AbstractBranch branch,List branchContent,int capacity) {
|
|
42 |
48338
| super( capacity );
|
|
43 |
48338
| this.branch = branch;
|
|
44 |
48338
| this.branchContent = branchContent;
|
|
45 |
| } |
|
46 |
| |
|
47 |
0
| public BackedList(AbstractBranch branch,List branchContent,List initialContent) {
|
|
48 |
0
| super( initialContent );
|
|
49 |
0
| this.branch = branch;
|
|
50 |
0
| this.branchContent = branchContent;
|
|
51 |
| } |
|
52 |
| |
|
53 |
10
| public boolean add(Object object) {
|
|
54 |
10
| branch.addNode( asNode( object ) );
|
|
55 |
10
| return super.add(object);
|
|
56 |
| } |
|
57 |
| |
|
58 |
16
| public void add(int index, Object object) {
|
|
59 |
16
| int size = size();
|
|
60 |
16
| if ( index < 0 ) {
|
|
61 |
0
| throw new IndexOutOfBoundsException( "Index value: " + index + " is less than zero" );
|
|
62 |
| } |
|
63 |
16
| else if ( index > size ) {
|
|
64 |
0
| throw new IndexOutOfBoundsException( "Index value: " + index + " cannot be greater than the size: " + size );
|
|
65 |
| } |
|
66 |
| |
|
67 |
16
| int realIndex = size == 0
|
|
68 |
| ? branchContent.size() |
|
69 |
16
| : index < size
|
|
70 |
| ? branchContent.indexOf(get(index)) |
|
71 |
| : (branchContent.indexOf(get(size - 1)) + 1); |
|
72 |
| |
|
73 |
16
| branch.addNode(realIndex, asNode( object ) );
|
|
74 |
14
| super.add(index, object);
|
|
75 |
| } |
|
76 |
| |
|
77 |
0
| public Object set(int index, Object object) {
|
|
78 |
0
| int realIndex = branchContent.indexOf( get(index) );
|
|
79 |
0
| if ( realIndex < 0 ) {
|
|
80 |
0
| realIndex = ( index == 0 ) ? 0 : Integer.MAX_VALUE;
|
|
81 |
| } |
|
82 |
0
| if ( realIndex < branchContent.size() ) {
|
|
83 |
0
| branch.removeNode( asNode( get(index) ) );
|
|
84 |
0
| branch.addNode(realIndex, asNode( object ) );
|
|
85 |
| } |
|
86 |
| else { |
|
87 |
0
| branch.removeNode( asNode( get(index) ) );
|
|
88 |
0
| branch.addNode( asNode( object ) );
|
|
89 |
| } |
|
90 |
0
| branch.childAdded( asNode( object ) );
|
|
91 |
0
| return super.set(index, object);
|
|
92 |
| } |
|
93 |
| |
|
94 |
0
| public boolean remove(Object object) {
|
|
95 |
0
| branch.removeNode( asNode( object ) );
|
|
96 |
0
| return super.remove(object);
|
|
97 |
| } |
|
98 |
| |
|
99 |
6
| public Object remove(int index) {
|
|
100 |
6
| Object object = super.remove(index);
|
|
101 |
6
| if ( object != null ) {
|
|
102 |
6
| branch.removeNode( asNode( object ) );
|
|
103 |
| } |
|
104 |
6
| return object;
|
|
105 |
| } |
|
106 |
| |
|
107 |
0
| public boolean addAll(Collection collection) {
|
|
108 |
0
| ensureCapacity(size() + collection.size());
|
|
109 |
| |
|
110 |
0
| int count = size();
|
|
111 |
0
| for (Iterator iter = collection.iterator(); iter.hasNext(); count-- ) {
|
|
112 |
0
| add(iter.next());
|
|
113 |
| } |
|
114 |
0
| return count != 0;
|
|
115 |
| } |
|
116 |
| |
|
117 |
0
| public boolean addAll(int index, Collection collection) {
|
|
118 |
0
| ensureCapacity(size() + collection.size());
|
|
119 |
| |
|
120 |
0
| int count = size();
|
|
121 |
0
| for (Iterator iter = collection.iterator(); iter.hasNext(); count-- ) {
|
|
122 |
0
| add(index++, iter.next());
|
|
123 |
| } |
|
124 |
0
| return count != 0;
|
|
125 |
| } |
|
126 |
| |
|
127 |
6
| public void clear() {
|
|
128 |
6
| for ( Iterator iter = iterator(); iter.hasNext(); ) {
|
|
129 |
6
| Object object = iter.next();
|
|
130 |
6
| branchContent.remove(object);
|
|
131 |
6
| branch.childRemoved( asNode( object ) );
|
|
132 |
| } |
|
133 |
6
| super.clear();
|
|
134 |
| } |
|
135 |
| |
|
136 |
| |
|
137 |
| |
|
138 |
| |
|
139 |
8640
| public void addLocal(Object object) {
|
|
140 |
8640
| super.add(object);
|
|
141 |
| } |
|
142 |
| |
|
143 |
38
| protected Node asNode(Object object) {
|
|
144 |
38
| if (object instanceof Node) {
|
|
145 |
38
| return (Node) object;
|
|
146 |
| } |
|
147 |
| else { |
|
148 |
0
| 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 |
| |