KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > mdr > storagemodel > CompositeCollection


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.mdr.storagemodel;
20
21 import java.util.*;
22 import java.io.*;
23 import java.lang.reflect.Array JavaDoc;
24
25 import javax.jmi.reflect.*;
26
27 import org.netbeans.mdr.handlers.BaseObjectHandler;
28 import org.netbeans.mdr.persistence.*;
29 import org.netbeans.mdr.util.*;
30
31 /**
32  * Instances of <code>CompositeCollection</code> are immutable collections uniting
33  * several other collections. The methods of this class must be called in the following
34  * order:
35  *
36  * <p><ol>
37  * <li>the constructor {@link #CompositeCollection()} to create an instance</li>
38  * <li>{@link addCollection(Collection} to add a collection forming a part of
39  * the current collection</li>
40  * <li>any accessor method</li>
41  * </ol></p>
42  *
43  * <p>It is not allowed to add further collections after any of the accessors has
44  * been called.</p>
45  *
46  * <p>Because the collections added using @link addCollection(Collection} are added
47  * as is and their members are accessed only, when to be returned by methods of
48  * this class resp. its {@link Iterator}, instances of <code>CompositeCollection</code>
49  * are live if and only if all of the composed collections are live.</p>
50  *
51  * <p><b>[XXX]:</b> Put this class into <code>org.netbeans.mdr.util</code>
52  * package.</p>
53  *
54  * @author Martin Matula
55  */

56 public class CompositeCollection implements Collection {
57
58     private final ArrayList innerCollections = new ArrayList();
59     private volatile boolean canChange = true;
60     
61     public CompositeCollection() {
62     }
63     
64     /**
65      * Adds a new collection. Can only be called before any accessor call.
66      */

67     public void addCollection(Collection collection) {
68         if (canChange) {
69             innerCollections.add(collection);
70         } else {
71             throw new DebugException("Set of collections cannot be changed after it was accessed.");
72         }
73     }
74     
75     public int size() {
76         int size = 0;
77         canChange = false;
78
79         for (Iterator it = innerCollections.iterator(); it.hasNext();) {
80             size += ((Collection) it.next()).size();
81         }
82         
83         return size;
84     }
85     
86     public boolean contains(Object JavaDoc obj) {
87         canChange = false;
88
89         for (Iterator it = innerCollections.iterator(); it.hasNext();) {
90             if (((Collection) it.next()).contains(obj)) return true;
91         }
92         
93         return false;
94     }
95     
96     public Iterator iterator() {
97         canChange = false;
98         ArrayList innerIterators = new ArrayList();
99         
100         for (Iterator it = innerCollections.iterator(); it.hasNext();) {
101             innerIterators.add(((Collection) it.next()).iterator());
102         }
103         
104         return new CompositeIterator(innerIterators.iterator());
105     }
106     
107     public boolean isEmpty() {
108         canChange = false;
109
110         for (Iterator it = innerCollections.iterator(); it.hasNext();) {
111             if (!((Collection) it.next()).isEmpty()) return false;
112         }
113         
114         return true;
115     }
116     
117     public boolean containsAll(Collection collection) {
118         for (Iterator it = collection.iterator(); it.hasNext();) {
119             if (!contains(it.next())) return false;
120         }
121         
122         return true;
123     }
124     
125     public Object JavaDoc[] toArray(Object JavaDoc[] obj) {
126         canChange = false;
127         ArrayList value = new ArrayList();
128         
129         for (Iterator it = innerCollections.iterator(); it.hasNext();) {
130             value.addAll((Collection) it.next());
131         }
132         
133         return value.toArray(obj);
134     }
135     
136     public Object JavaDoc[] toArray() {
137         return toArray(new Object JavaDoc[0]);
138     }
139
140     /**
141      * operation not supported
142      */

143     public void clear() {
144         throw new UnsupportedOperationException JavaDoc();
145     }
146     
147     /**
148      * operation not supported
149      */

150     public boolean addAll(Collection collection) {
151         throw new UnsupportedOperationException JavaDoc();
152     }
153     
154     /**
155      * operation not supported
156      */

157     public boolean remove(Object JavaDoc obj) {
158         throw new UnsupportedOperationException JavaDoc();
159     }
160     
161     /**
162      * operation not supported
163      */

164     public boolean add(Object JavaDoc obj) {
165         throw new UnsupportedOperationException JavaDoc();
166     }
167     
168     /**
169      * operation not supported
170      */

171     public boolean retainAll(Collection collection) {
172         throw new UnsupportedOperationException JavaDoc();
173     }
174     
175     /**
176      * operation not supported
177      */

178     public boolean removeAll(Collection collection) {
179         throw new UnsupportedOperationException JavaDoc();
180     }
181
182     /* --------------------------------------------------------------------- */
183     /* -- CompositeIterator (inner class) ---------------------------------- */
184     /* --------------------------------------------------------------------- */
185     
186     protected class CompositeIterator implements Iterator {
187         private final Iterator innerIterators;
188         private Iterator currentIterator;
189         
190         protected CompositeIterator(Iterator innerIterators) {
191             this.innerIterators = innerIterators;
192             this.currentIterator = (Iterator) innerIterators.next();
193             hasNext();
194         }
195         
196         public boolean hasNext() {
197             while (innerIterators.hasNext()) {
198                 if (currentIterator.hasNext()) {
199                     return true;
200                 } else {
201                     currentIterator = (Iterator) innerIterators.next();
202                 }
203             }
204             return currentIterator.hasNext();
205         }
206         
207         public Object JavaDoc next() {
208             Object JavaDoc result = currentIterator.next();
209             hasNext();
210             return result;
211         }
212         
213        /**
214         * operation not supported
215         */

216         public void remove() {
217             throw new UnsupportedOperationException JavaDoc();
218         }
219     }
220 }
221
Popular Tags