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