Clover coverage report - dom4j - 1.5
Coverage timestamp: vr sep 3 2004 20:47:03 GMT+01:00
file stats: LOC: 199   Methods: 13
NCLOC: 106   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
BackedList.java 37,5% 46,2% 61,5% 46,1%
coverage coverage
 1    /*
 2    * Copyright 2001-2004 (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    * $Id: BackedList.java,v 1.12 2004/08/10 11:18:49 maartenc Exp $
 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    /** <p><code>BackedList</code> represents a list of content
 21    * of a {@link org.dom4j.Branch}. Changes to the list will
 22    * be reflected in the branch, though changes to the branch will not
 23    * be reflected in this list.</p>
 24    *
 25    * @author <a href="mailto:james.strachan@metastuff.com">James Strachan</a>
 26    * @version $Revision: 1.12 $
 27    */
 28    public class BackedList extends ArrayList {
 29   
 30    /** The content of the Branch which is modified if I am modified */
 31    private List branchContent;
 32   
 33    /** The <code>AbstractBranch</code> instance which owns the content */
 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() // Insert at the end of branch
 69  16 : index < size
 70    ? branchContent.indexOf(get(index)) // Normal case: get position of element in branch
 71    : (branchContent.indexOf(get(size - 1)) + 1); // Insert after last item
 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    /** Performs a local addition which is not forward through to the
 137    * Branch or backing list
 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    * Redistribution and use of this software and associated documentation
 158    * ("Software"), with or without modification, are permitted provided
 159    * that the following conditions are met:
 160    *
 161    * 1. Redistributions of source code must retain copyright
 162    * statements and notices. Redistributions must also contain a
 163    * copy of this document.
 164    *
 165    * 2. Redistributions in binary form must reproduce the
 166    * above copyright notice, this list of conditions and the
 167    * following disclaimer in the documentation and/or other
 168    * materials provided with the distribution.
 169    *
 170    * 3. The name "DOM4J" must not be used to endorse or promote
 171    * products derived from this Software without prior written
 172    * permission of MetaStuff, Ltd. For written permission,
 173    * please contact dom4j-info@metastuff.com.
 174    *
 175    * 4. Products derived from this Software may not be called "DOM4J"
 176    * nor may "DOM4J" appear in their names without prior written
 177    * permission of MetaStuff, Ltd. DOM4J is a registered
 178    * trademark of MetaStuff, Ltd.
 179    *
 180    * 5. Due credit should be given to the DOM4J Project -
 181    * http://www.dom4j.org
 182    *
 183    * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
 184    * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
 185    * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 186    * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 187    * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 188    * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 189    * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 190    * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 191    * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 192    * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 193    * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 194    * OF THE POSSIBILITY OF SUCH DAMAGE.
 195    *
 196    * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
 197    *
 198    * $Id: BackedList.java,v 1.12 2004/08/10 11:18:49 maartenc Exp $
 199    */