View Javadoc

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      public BackedList(AbstractBranch branch,List branchContent) { 
38          this( branch, branchContent, branchContent.size() );
39      }
40      
41      public BackedList(AbstractBranch branch,List branchContent,int capacity) { 
42          super( capacity );
43          this.branch = branch;
44          this.branchContent = branchContent;
45      }
46      
47      public BackedList(AbstractBranch branch,List branchContent,List initialContent) { 
48          super( initialContent );
49          this.branch = branch;
50          this.branchContent = branchContent;
51      }
52      
53      public boolean add(Object object) {
54          branch.addNode( asNode( object ) );
55          return super.add(object);
56      }
57      
58      public void add(int index, Object object) {
59          int size = size();
60          if ( index < 0 ) {
61              throw new IndexOutOfBoundsException( "Index value: " + index + " is less than zero" );
62          }
63          else if ( index > size ) {
64              throw new IndexOutOfBoundsException( "Index value: " + index + " cannot be greater than the size: " + size );
65          }
66          
67          int realIndex = size == 0
68                  ? branchContent.size()                   // Insert at the end of branch
69                  : 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          branch.addNode(realIndex, asNode( object ) );
74          super.add(index, object);
75      }
76      
77      public Object set(int index, Object object) {
78          int realIndex = branchContent.indexOf( get(index) );
79          if ( realIndex < 0 ) {
80              realIndex = ( index == 0 ) ? 0 : Integer.MAX_VALUE;
81          }
82          if ( realIndex < branchContent.size() ) {
83              branch.removeNode( asNode( get(index) ) );
84              branch.addNode(realIndex, asNode( object ) );
85          }
86          else {
87              branch.removeNode( asNode( get(index) ) );
88              branch.addNode( asNode( object ) );
89          }
90          branch.childAdded( asNode( object ) );
91          return super.set(index, object);
92      }
93      
94      public boolean remove(Object object) {
95          branch.removeNode( asNode( object ) );
96          return super.remove(object);
97      }
98  
99      public Object remove(int index) {
100         Object object = super.remove(index);
101         if ( object != null ) {
102             branch.removeNode( asNode( object ) );
103         }
104         return object;
105     }
106 
107     public boolean addAll(Collection collection) {
108         ensureCapacity(size() + collection.size());
109         
110         int count = size();
111         for (Iterator iter = collection.iterator(); iter.hasNext(); count-- ) {
112             add(iter.next());
113         }
114         return count != 0;
115     }
116     
117     public boolean addAll(int index, Collection collection) {
118         ensureCapacity(size() + collection.size());
119         
120         int count = size();
121         for (Iterator iter = collection.iterator(); iter.hasNext(); count-- ) {
122             add(index++, iter.next());
123         }
124         return count != 0;
125     }
126     
127     public void clear() {
128         for ( Iterator iter = iterator(); iter.hasNext(); ) {
129             Object object = iter.next();
130             branchContent.remove(object);
131             branch.childRemoved( asNode( object ) );
132         }
133         super.clear();
134     }
135 
136     /*** Performs a local addition which is not forward through to the 
137       * Branch or backing list 
138       */
139     public void addLocal(Object object) {
140         super.add(object);
141     }
142     
143     protected Node asNode(Object object) {
144         if (object instanceof Node) {
145             return (Node) object;
146         }
147         else {
148             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  */