KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > set > CompositeSet


1 /*
2  * Copyright 2003-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.collections.set;
17
18 import java.util.Collection JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.Set JavaDoc;
21
22 import org.apache.commons.collections.CollectionUtils;
23 import org.apache.commons.collections.collection.CompositeCollection;
24
25 /**
26  * Decorates a set of other sets to provide a single unified view.
27  * <p>
28  * Changes made to this set will actually be made on the decorated set.
29  * Add and remove operations require the use of a pluggable strategy. If no
30  * strategy is provided then add and remove are unsupported.
31  *
32  * @since Commons Collections 3.0
33  * @version $Revision: 1.3 $ $Date: 2004/02/18 01:14:27 $
34  *
35  * @author Brian McCallister
36  */

37 public class CompositeSet extends CompositeCollection implements Set JavaDoc {
38     /**
39      * Create an empty CompositeSet
40      */

41     public CompositeSet() {
42         super();
43     }
44     
45     /**
46      * Create a CompositeSet with just <code>set</code> composited
47      * @param set The initial set in the composite
48      */

49     public CompositeSet(Set JavaDoc set) {
50         super(set);
51     }
52     
53     /**
54      * Create a composite set with sets as the initial set of composited Sets
55      */

56     public CompositeSet(Set JavaDoc[] sets) {
57         super(sets);
58     }
59     
60     /**
61      * Add a Set to this composite
62      *
63      * @param c Must implement Set
64      * @throws IllegalArgumentException if c does not implement java.util.Set
65      * or if a SetMutator is set, but fails to resolve a collision
66      * @throws UnsupportedOperationException if there is no SetMutator set, or
67      * a CollectionMutator is set instead of a SetMutator
68      * @see org.apache.commons.collections.collection.CompositeCollection.CollectionMutator
69      * @see SetMutator
70      */

71     public synchronized void addComposited(Collection JavaDoc c) {
72         if (!(c instanceof Set JavaDoc)) {
73             throw new IllegalArgumentException JavaDoc("Collections added must implement java.util.Set");
74         }
75         
76         for (Iterator JavaDoc i = this.getCollections().iterator(); i.hasNext();) {
77             Set JavaDoc set = (Set JavaDoc) i.next();
78             Collection JavaDoc intersects = CollectionUtils.intersection(set, c);
79             if (intersects.size() > 0) {
80                 if (this.mutator == null) {
81                     throw new UnsupportedOperationException JavaDoc(
82                         "Collision adding composited collection with no SetMutator set");
83                 }
84                 else if (!(this.mutator instanceof SetMutator)) {
85                     throw new UnsupportedOperationException JavaDoc(
86                         "Collision adding composited collection to a CompositeSet with a CollectionMutator instead of a SetMutator");
87                 }
88                 ((SetMutator) this.mutator).resolveCollision(this, set, (Set JavaDoc) c, intersects);
89                 if (CollectionUtils.intersection(set, c).size() > 0) {
90                     throw new IllegalArgumentException JavaDoc(
91                         "Attempt to add illegal entry unresolved by SetMutator.resolveCollision()");
92                 }
93             }
94         }
95         super.addComposited(new Collection JavaDoc[]{c});
96     }
97     
98     /**
99      * Add two sets to this composite
100      *
101      * @throws IllegalArgumentException if c or d does not implement java.util.Set
102      */

103     public synchronized void addComposited(Collection JavaDoc c, Collection JavaDoc d) {
104         if (!(c instanceof Set JavaDoc)) throw new IllegalArgumentException JavaDoc("Argument must implement java.util.Set");
105         if (!(d instanceof Set JavaDoc)) throw new IllegalArgumentException JavaDoc("Argument must implement java.util.Set");
106         this.addComposited(new Set JavaDoc[]{(Set JavaDoc) c, (Set JavaDoc) d});
107     }
108     
109     /**
110      * Add an array of sets to this composite
111      * @param comps
112      * @throws IllegalArgumentException if any of the collections in comps do not implement Set
113      */

114     public synchronized void addComposited(Collection JavaDoc[] comps) {
115         for (int i = comps.length - 1; i >= 0; --i) {
116             this.addComposited(comps[i]);
117         }
118     }
119     
120     /**
121      * This can receive either a <code>CompositeCollection.CollectionMutator</code>
122      * or a <code>CompositeSet.SetMutator</code>. If a
123      * <code>CompositeCollection.CollectionMutator</code> is used than conflicts when adding
124      * composited sets will throw IllegalArgumentException
125      * <p>
126      */

127     public void setMutator(CollectionMutator mutator) {
128         super.setMutator(mutator);
129     }
130     
131     /* Set operations */
132     
133     /**
134      * If a <code>CollectionMutator</code> is defined for this CompositeSet then this
135      * method will be called anyway.
136      *
137      * @param obj Object to be removed
138      * @return true if the object is removed, false otherwise
139      */

140     public boolean remove(Object JavaDoc obj) {
141         for (Iterator JavaDoc i = this.getCollections().iterator(); i.hasNext();) {
142             Set JavaDoc set = (Set JavaDoc) i.next();
143             if (set.contains(obj)) return set.remove(obj);
144         }
145         return false;
146     }
147     
148     
149     /**
150      * @see Set#equals
151      */

152     public boolean equals(Object JavaDoc obj) {
153         if (obj instanceof Set JavaDoc) {
154             Set JavaDoc set = (Set JavaDoc) obj;
155             if (set.containsAll(this) && set.size() == this.size()) {
156                 return true;
157             }
158         }
159         return false;
160     }
161     
162     /**
163      * @see Set#hashCode
164      */

165     public int hashCode() {
166         int code = 0;
167         for (Iterator JavaDoc i = this.iterator(); i.hasNext();) {
168             Object JavaDoc next = i.next();
169             code += (next != null ? next.hashCode() : 0);
170         }
171         return code;
172     }
173     
174     /**
175      * Define callbacks for mutation operations.
176      * <p>
177      * Defining remove() on implementations of SetMutator is pointless
178      * as they are never called by CompositeSet.
179      */

180     public static interface SetMutator extends CompositeCollection.CollectionMutator {
181         /**
182          * <p>
183          * Called when a Set is added to the CompositeSet and there is a
184          * collision between existing and added sets.
185          * </p>
186          * <p>
187          * If <code>added</code> and <code>existing</code> still have any intersects
188          * after this method returns an IllegalArgumentException will be thrown.
189          * </p>
190          * @param comp The CompositeSet being modified
191          * @param existing The Set already existing in the composite
192          * @param added the Set being added to the composite
193          * @param intersects the intersection of th existing and added sets
194          */

195         public void resolveCollision(CompositeSet comp, Set JavaDoc existing, Set JavaDoc added, Collection JavaDoc intersects);
196     }
197 }
198
Popular Tags