Clover coverage report - dom4j - 1.6.1 Coverage timestamp: ma mei 16 2005 14:23:01 GMT+01:00 FRAMES NO FRAMES |
file stats: | LOC: | 209 |  | Methods: | 13
| NCLOC: | 110 |  | Classes: | 1
Source file | Conditionals | Statements | Methods | TOTAL
BackedList.java | 37,5% | 49,1% | 61,5% | 47,9% |

1 |  | /*
2 |  | * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
3 |  | *
4 |  | * This software is open source.
5 |  | * See the bottom of this file for the licence.
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 | 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 |  | * 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 | 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 |  | * Redistribution and use of this software and associated documentation
176 |  | * ("Software"), with or without modification, are permitted provided that the
177 |  | * following conditions are met:
178 |  | *
179 |  | * 1. Redistributions of source code must retain copyright statements and
180 |  | * notices. Redistributions must also contain a copy of this document.
181 |  | *
182 |  | * 2. Redistributions in binary form must reproduce the above copyright notice,
183 |  | * this list of conditions and the following disclaimer in the documentation
184 |  | * and/or other materials provided with the distribution.
185 |  | *
186 |  | * 3. The name "DOM4J" must not be used to endorse or promote products derived
187 |  | * from this Software without prior written permission of MetaStuff, Ltd. For
188 |  | * written permission, please contact dom4j-info@metastuff.com.
189 |  | *
190 |  | * 4. Products derived from this Software may not be called "DOM4J" nor may
191 |  | * "DOM4J" appear in their names without prior written permission of MetaStuff,
192 |  | * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
193 |  | *
194 |  | * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
195 |  | *
196 |  | * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
197 |  | * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
198 |  | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
199 |  | * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
200 |  | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
201 |  | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
202 |  | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
203 |  | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
204 |  | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
205 |  | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
206 |  | * POSSIBILITY OF SUCH DAMAGE.
207 |  | *
208 |  | * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
209 |  | */