View Javadoc

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