KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > xml > dom4j > o3impl > BackedList


1 /*
2  * Copyright 2001 (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.2 2003/06/10 16:18:36 per_nyfelt Exp $
8  */

9
10 package org.ozoneDB.xml.dom4j.o3impl;
11
12 import org.dom4j.IllegalAddException;
13 import org.dom4j.Node;
14
15 import java.util.ArrayList JavaDoc;
16 import java.util.Collection JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.List JavaDoc;
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.2 $
27  */

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

141     public void addLocal(Object JavaDoc object) {
142         super.add(object);
143     }
144
145     protected Node asNode(Object JavaDoc object) {
146         if (object instanceof Node) {
147             return (Node) object;
148         } else {
149             throw new IllegalAddException("This list must contain instances of Node. Invalid type: " + object);
150         }
151     }
152 }
153
154
155 /*
156  * Redistribution and use of this software and associated documentation
157  * ("Software"), with or without modification, are permitted provided
158  * that the following conditions are met:
159  *
160  * 1. Redistributions of source code must retain copyright
161  * statements and notices. Redistributions must also contain a
162  * copy of this document.
163  *
164  * 2. Redistributions in binary form must reproduce the
165  * above copyright notice, this list of conditions and the
166  * following disclaimer in the documentation and/or other
167  * materials provided with the distribution.
168  *
169  * 3. The name "DOM4J" must not be used to endorse or promote
170  * products derived from this Software without prior written
171  * permission of MetaStuff, Ltd. For written permission,
172  * please contact dom4j-info@metastuff.com.
173  *
174  * 4. Products derived from this Software may not be called "DOM4J"
175  * nor may "DOM4J" appear in their names without prior written
176  * permission of MetaStuff, Ltd. DOM4J is a registered
177  * trademark of MetaStuff, Ltd.
178  *
179  * 5. Due credit should be given to the DOM4J Project
180  * (http://dom4j.org/).
181  *
182  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
183  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
184  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
185  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
186  * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
187  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
188  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
189  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
190  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
191  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
192  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
193  * OF THE POSSIBILITY OF SUCH DAMAGE.
194  *
195  * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved.
196  *
197  * $Id: BackedList.java,v 1.2 2003/06/10 16:18:36 per_nyfelt Exp $
198  */

199
Popular Tags