Project dom4j 1.5.2 [5/2/05 10:13 PM]
 
Coverage - org/dom4j/tree/ContentListFacade.java
  /*
   * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
   * 
   * This software is open source. 
   * See the bottom of this file for the licence.
   * 
   * $Id: ContentListFacade.java,v 1.8 2004/06/25 12:34:50 maartenc Exp $
   */
 
  package org.dom4j.tree;
 
  import java.util.AbstractList;
  import java.util.Collection;
  import java.util.Iterator;
  import java.util.List;
 
  import org.dom4j.IllegalAddException;
  import org.dom4j.Node;
 
  /** <p><code>ContentListFacade</code> represents a facade of the 
    * content of a {@link org.dom4j.Branch} which is returned via calls to the
    * {@link org.dom4j.Branch#content} method to allow users to modify the content
    * of a {@link org.dom4j.Branch} directly using the {@link List} interface. 
    * This list is backed by the branch such that changes to the list will
    * be reflected in the branch and changes to the branch will be reflected
    * be reflected in this list.</p>
    *
    * @author <a href="mailto:james.strachan@metastuff.com">James Strachan</a>
    * @version $Revision: 1.8 $
    */
  public class ContentListFacade extends AbstractList {
 
      /** The content of the Branch which is modified if I am modified */
      private List branchContent;
 
      /** The <code>AbstractBranch</code> instance which owns the content */
      private AbstractBranch branch;
 
      
112x       public ContentListFacade(AbstractBranch branch, List branchContent) { 
112x           this.branch = branch;
112x           this.branchContent = branchContent;
112x       }
      
      public boolean add(Object object) {
0x           branch.childAdded( asNode( object ) );
0x           return branchContent.add(object);
      }
      
      public void add(int index, Object object) {
10x           branch.childAdded( asNode( object ) );
10x           branchContent.add(index, object);
10x       }
      
      public Object set(int index, Object object) {
2x           branch.childAdded( asNode( object ) );
2x           return branchContent.set(index, object);
      }
      
      public boolean remove(Object object) {
0x           branch.childRemoved( asNode( object ) );
0x           return branchContent.remove(object);
      }
 
      public Object remove(int index) {
0x           Object object = branchContent.remove(index);
0/2 0x           if ( object != null ) {
0x               branch.childRemoved( asNode( object ) );
          }
0x           return object;
      }
 
      public boolean addAll(Collection collection) {
0x           int count = branchContent.size();
0/2 0x           for (Iterator iter = collection.iterator(); iter.hasNext(); count++ ) {
0x               add(iter.next());
          }
0/2 0x           return count == branchContent.size();
      }
      
      public boolean addAll(int index, Collection collection) {
0x           int count = branchContent.size();
0/2 0x           for (Iterator iter = collection.iterator(); iter.hasNext(); count-- ) {
0x               add(index++, iter.next());
          }
0/2 0x           return count == branchContent.size();
      }
      
      public void clear() {
0/2 0x           for ( Iterator iter = iterator(); iter.hasNext(); ) {
0x               Object object = iter.next();
0x               branch.childRemoved( asNode( object ) );
          }
0x           branchContent.clear();
0x       }
      
      public boolean removeAll(Collection c) {
0/2 0x           for ( Iterator iter = c.iterator(); iter.hasNext(); ) {
0x               Object object = iter.next();
0x               branch.childRemoved( asNode( object ) );
          }
0x           return branchContent.removeAll(c);
      }
      
      public int size() {
368x           return branchContent.size();
      }
      
      public boolean isEmpty() {
0x           return branchContent.isEmpty();
      }
      
      public boolean contains(Object o) {
0x           return branchContent.contains(o);
      }
      
      public Object[] toArray() {
0x           return branchContent.toArray();
      }
      
      public Object[] toArray(Object[] a) {
0x           return branchContent.toArray(a);
      }
      
      public boolean containsAll(Collection c) {
0x           return branchContent.containsAll(c);
      }
      
      public Object get(int index) {
282x           return branchContent.get(index);
      }
      
      public int indexOf(Object o) {
4x           return branchContent.indexOf(o);
      }
      
      public int lastIndexOf(Object o) {
0x           return branchContent.lastIndexOf(o);
      }
      
      protected Node asNode(Object object) {
1/2 12x           if (object instanceof Node) {
12x               return (Node) object;
          }
          else {
0x               throw new IllegalAddException( "This list must contain instances of Node. Invalid type: "+ object );
          }
      }    
 
      protected List getBackingList() {
4x           return branchContent;
      }
  }
 
 
 
 
  /*
   * Redistribution and use of this software and associated documentation
   * ("Software"), with or without modification, are permitted provided
   * that the following conditions are met:
   *
   * 1. Redistributions of source code must retain copyright
   *    statements and notices.  Redistributions must also contain a
   *    copy of this document.
   *
   * 2. Redistributions in binary form must reproduce the
   *    above copyright notice, this list of conditions and the
   *    following disclaimer in the documentation and/or other
   *    materials provided with the distribution.
   *
   * 3. The name "DOM4J" must not be used to endorse or promote
   *    products derived from this Software without prior written
   *    permission of MetaStuff, Ltd.  For written permission,
   *    please contact dom4j-info@metastuff.com.
   *
   * 4. Products derived from this Software may not be called "DOM4J"
   *    nor may "DOM4J" appear in their names without prior written
   *    permission of MetaStuff, Ltd. DOM4J is a registered
   *    trademark of MetaStuff, Ltd.
   *
   * 5. Due credit should be given to the DOM4J Project - 
   *    http://www.dom4j.org
   *
   * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
   * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
   * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
   * OF THE POSSIBILITY OF SUCH DAMAGE.
   *
   * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
   *
   * $Id: ContentListFacade.java,v 1.8 2004/06/25 12:34:50 maartenc Exp $
   */