KickJava   Java API By Example, From Geeks To Geeks.

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


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: ContentListFacade.java,v 1.3 2003/07/07 10:30:29 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.AbstractList JavaDoc;
16 import java.util.Collection JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.List JavaDoc;
19
20 /** <p><code>ContentListFacade</code> represents a facade of the
21  * content of a {@link org.dom4j.Branch} which is returned via calls to the
22  * {@link org.dom4j.Branch#content} method to allow users to modify the content
23  * of a {@link org.dom4j.Branch} directly using the {@link List} interface.
24  * This list is backed by the branch such that changes to the list will
25  * be reflected in the branch and changes to the branch will be reflected
26  * be reflected in this list.</p>
27  *
28  * @author <a HREF="mailto:james.strachan@metastuff.com">James Strachan</a>
29  * @version $Revision: 1.3 $
30  */

31 public class ContentListFacade extends AbstractList JavaDoc {
32
33     /** The content of the Branch which is modified if I am modified */
34     private List JavaDoc branchContent;
35
36     /** The <code>AbstractBranch</code> instance which owns the content */
37     private AbstractBranch branch;
38
39     protected ContentListFacade() {
40     }
41
42     public ContentListFacade(AbstractBranch branch, List JavaDoc branchContent) {
43         this.branch = branch;
44         this.branchContent = branchContent;
45     }
46
47     public boolean add(Object JavaDoc object) {
48         branch.childAdded(asNode(object));
49         return branchContent.add(object);
50     }
51
52     public void add(int index, Object JavaDoc object) {
53         branch.childAdded(asNode(object));
54         branchContent.add(index, object);
55     }
56
57     public Object JavaDoc set(int index, Object JavaDoc object) {
58         branch.childAdded(asNode(object));
59         return branchContent.set(index, object);
60     }
61
62     public boolean remove(Object JavaDoc object) {
63         branch.childRemoved(asNode(object));
64         return branchContent.remove(object);
65     }
66
67     public Object JavaDoc remove(int index) {
68         Object JavaDoc object = branchContent.remove(index);
69         if (object != null) {
70             branch.childRemoved(asNode(object));
71         }
72         return object;
73     }
74
75     public boolean addAll(Collection JavaDoc collection) {
76         int count = branchContent.size();
77         for (Iterator JavaDoc iter = collection.iterator(); iter.hasNext(); count++) {
78             add(iter.next());
79         }
80         return count == branchContent.size();
81     }
82
83     public boolean addAll(int index, Collection JavaDoc collection) {
84         int count = branchContent.size();
85         for (Iterator JavaDoc iter = collection.iterator(); iter.hasNext(); count--) {
86             add(index++, iter.next());
87         }
88         return count == branchContent.size();
89     }
90
91     public void clear() {
92         for (Iterator JavaDoc iter = iterator(); iter.hasNext();) {
93             Object JavaDoc object = iter.next();
94             branch.childRemoved(asNode(object));
95         }
96         branchContent.clear();
97     }
98
99     public boolean removeAll(Collection JavaDoc c) {
100         for (Iterator JavaDoc iter = c.iterator(); iter.hasNext();) {
101             Object JavaDoc object = iter.next();
102             branch.childRemoved(asNode(object));
103         }
104         return branchContent.removeAll(c);
105     }
106
107     public int size() {
108         return branchContent.size();
109     }
110
111     public boolean isEmpty() {
112         return branchContent.isEmpty();
113     }
114
115     public boolean contains(Object JavaDoc o) {
116         return branchContent.contains(o);
117     }
118
119     public Object JavaDoc[] toArray() {
120         return branchContent.toArray();
121     }
122
123     public Object JavaDoc[] toArray(Object JavaDoc[] a) {
124         return branchContent.toArray(a);
125     }
126
127     public boolean containsAll(Collection JavaDoc c) {
128         return branchContent.containsAll(c);
129     }
130
131     public Object JavaDoc get(int index) {
132         return branchContent.get(index);
133     }
134
135     public int indexOf(Object JavaDoc o) {
136         return branchContent.indexOf(o);
137     }
138
139     public int lastIndexOf(Object JavaDoc o) {
140         return branchContent.lastIndexOf(o);
141     }
142
143     protected Node asNode(Object JavaDoc object) {
144         if (object instanceof Node) {
145             return (Node) object;
146         } else {
147             throw new IllegalAddException("This list must contain instances of Node. Invalid type: " + object);
148         }
149     }
150
151     protected List JavaDoc getBackingList() {
152         return branchContent;
153     }
154 }
155
156
157 /*
158  * Redistribution and use of this software and associated documentation
159  * ("Software"), with or without modification, are permitted provided
160  * that the following conditions are met:
161  *
162  * 1. Redistributions of source code must retain copyright
163  * statements and notices. Redistributions must also contain a
164  * copy of this document.
165  *
166  * 2. Redistributions in binary form must reproduce the
167  * above copyright notice, this list of conditions and the
168  * following disclaimer in the documentation and/or other
169  * materials provided with the distribution.
170  *
171  * 3. The name "DOM4J" must not be used to endorse or promote
172  * products derived from this Software without prior written
173  * permission of MetaStuff, Ltd. For written permission,
174  * please contact dom4j-info@metastuff.com.
175  *
176  * 4. Products derived from this Software may not be called "DOM4J"
177  * nor may "DOM4J" appear in their names without prior written
178  * permission of MetaStuff, Ltd. DOM4J is a registered
179  * trademark of MetaStuff, Ltd.
180  *
181  * 5. Due credit should be given to the DOM4J Project
182  * (http://dom4j.org/).
183  *
184  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
185  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
186  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
187  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
188  * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
189  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
190  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
191  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
192  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
193  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
194  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
195  * OF THE POSSIBILITY OF SUCH DAMAGE.
196  *
197  * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved.
198  *
199  * $Id: ContentListFacade.java,v 1.3 2003/07/07 10:30:29 per_nyfelt Exp $
200  */

201
Popular Tags