KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > beans > factory > support > ManagedSet


1 /*
2  * Copyright 2002-2007 the original author or authors.
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
17 package org.springframework.beans.factory.support;
18
19 import java.util.Collection JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.Set JavaDoc;
22
23 import org.springframework.beans.BeanMetadataElement;
24 import org.springframework.beans.Mergeable;
25 import org.springframework.core.CollectionFactory;
26
27 /**
28  * Tag collection class used to hold managed Set values, which may
29  * include runtime bean references (to be resolved into bean objects).
30  *
31  * <p>Wraps a target Set, which will be a linked set if possible
32  * (that is, if running on JDK 1.4 or if Commons Collections 3.x is available).
33  *
34  * @author Juergen Hoeller
35  * @author Rob Harrop
36  * @since 21.01.2004
37  * @see org.springframework.core.CollectionFactory#createLinkedSetIfPossible
38  */

39 public class ManagedSet implements Set JavaDoc, Mergeable, BeanMetadataElement {
40
41     private final Set JavaDoc targetSet;
42
43     private Object JavaDoc source;
44
45     private boolean mergeEnabled;
46
47
48     public ManagedSet() {
49         this(16);
50     }
51
52     public ManagedSet(int initialCapacity) {
53         this.targetSet = CollectionFactory.createLinkedSetIfPossible(initialCapacity);
54     }
55
56     public ManagedSet(Set JavaDoc targetSet) {
57         this.targetSet = targetSet;
58     }
59
60
61     /**
62      * Set the configuration source <code>Object</code> for this metadata element.
63      * <p>The exact type of the object will depend on the configuration mechanism used.
64      */

65     public void setSource(Object JavaDoc source) {
66         this.source = source;
67     }
68
69     public Object JavaDoc getSource() {
70         return this.source;
71     }
72
73     /**
74      * Set whether merging should be enabled for this collection,
75      * in case of a 'parent' collection value being present.
76      */

77     public void setMergeEnabled(boolean mergeEnabled) {
78         this.mergeEnabled = mergeEnabled;
79     }
80
81     public boolean isMergeEnabled() {
82         return this.mergeEnabled;
83     }
84
85     public Object JavaDoc merge(Object JavaDoc parent) {
86         if (!this.mergeEnabled) {
87             throw new IllegalStateException JavaDoc("Not allowed to merge when the 'mergeEnabled' property is set to 'false'");
88         }
89         if (parent == null) {
90             return this;
91         }
92         if (!(parent instanceof Set JavaDoc)) {
93             throw new IllegalArgumentException JavaDoc("Cannot merge with object of type [" + parent.getClass() + "]");
94         }
95         Set JavaDoc merged = new ManagedSet();
96         merged.addAll((Set JavaDoc) parent);
97         merged.addAll(this);
98         return merged;
99     }
100
101
102     public int size() {
103         return this.targetSet.size();
104     }
105
106     public boolean isEmpty() {
107         return this.targetSet.isEmpty();
108     }
109
110     public boolean contains(Object JavaDoc obj) {
111         return this.targetSet.contains(obj);
112     }
113
114     public Iterator JavaDoc iterator() {
115         return this.targetSet.iterator();
116     }
117
118     public Object JavaDoc[] toArray() {
119         return this.targetSet.toArray();
120     }
121
122     public Object JavaDoc[] toArray(Object JavaDoc[] arr) {
123         return this.targetSet.toArray(arr);
124     }
125
126     public boolean add(Object JavaDoc obj) {
127         return this.targetSet.add(obj);
128     }
129
130     public boolean remove(Object JavaDoc obj) {
131         return this.targetSet.remove(obj);
132     }
133
134     public boolean containsAll(Collection JavaDoc coll) {
135         return this.targetSet.containsAll(coll);
136     }
137
138     public boolean addAll(Collection JavaDoc coll) {
139         return this.targetSet.addAll(coll);
140     }
141
142     public boolean retainAll(Collection JavaDoc coll) {
143         return this.targetSet.retainAll(coll);
144     }
145
146     public boolean removeAll(Collection JavaDoc coll) {
147         return this.targetSet.removeAll(coll);
148     }
149
150     public void clear() {
151         this.targetSet.clear();
152     }
153
154     public int hashCode() {
155         return this.targetSet.hashCode();
156     }
157
158     public boolean equals(Object JavaDoc obj) {
159         return this.targetSet.equals(obj);
160     }
161
162     public String JavaDoc toString() {
163         return this.targetSet.toString();
164     }
165
166 }
167
Popular Tags