KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > beans > factory > config > SetFactoryBean


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.config;
18
19 import java.util.Iterator JavaDoc;
20 import java.util.Set JavaDoc;
21
22 import org.springframework.beans.BeanUtils;
23 import org.springframework.beans.TypeConverter;
24 import org.springframework.core.CollectionFactory;
25 import org.springframework.core.GenericCollectionTypeResolver;
26 import org.springframework.core.JdkVersion;
27
28 /**
29  * Simple factory for shared Set instances. Allows for central setup
30  * of Sets via the "set" element in XML bean definitions.
31  *
32  * @author Juergen Hoeller
33  * @since 09.12.2003
34  * @see ListFactoryBean
35  * @see MapFactoryBean
36  */

37 public class SetFactoryBean extends AbstractFactoryBean {
38
39     private Set JavaDoc sourceSet;
40
41     private Class JavaDoc targetSetClass;
42
43
44     /**
45      * Set the source Set, typically populated via XML "set" elements.
46      */

47     public void setSourceSet(Set JavaDoc sourceSet) {
48         this.sourceSet = sourceSet;
49     }
50
51     /**
52      * Set the class to use for the target Set. Can be populated with a fully
53      * qualified class name when defined in a Spring application context.
54      * <p>Default is a linked HashSet, keeping the registration order.
55      * If no linked Set implementation is available, a plain HashSet will
56      * be used as fallback (not keeping the registration order).
57      * @see org.springframework.core.CollectionFactory#createLinkedSetIfPossible
58      */

59     public void setTargetSetClass(Class JavaDoc targetSetClass) {
60         if (targetSetClass == null) {
61             throw new IllegalArgumentException JavaDoc("'targetSetClass' must not be null");
62         }
63         if (!Set JavaDoc.class.isAssignableFrom(targetSetClass)) {
64             throw new IllegalArgumentException JavaDoc("'targetSetClass' must implement [java.util.Set]");
65         }
66         this.targetSetClass = targetSetClass;
67     }
68
69
70     public Class JavaDoc getObjectType() {
71         return Set JavaDoc.class;
72     }
73
74     protected Object JavaDoc createInstance() {
75         if (this.sourceSet == null) {
76             throw new IllegalArgumentException JavaDoc("'sourceSet' is required");
77         }
78         Set JavaDoc result = null;
79         if (this.targetSetClass != null) {
80             result = (Set JavaDoc) BeanUtils.instantiateClass(this.targetSetClass);
81         }
82         else {
83             result = CollectionFactory.createLinkedSetIfPossible(this.sourceSet.size());
84         }
85         Class JavaDoc valueType = null;
86         if (this.targetSetClass != null && JdkVersion.isAtLeastJava15()) {
87             valueType = GenericCollectionTypeResolver.getCollectionType(this.targetSetClass);
88         }
89         if (valueType != null) {
90             TypeConverter converter = getBeanTypeConverter();
91             for (Iterator JavaDoc it = this.sourceSet.iterator(); it.hasNext();) {
92                 result.add(converter.convertIfNecessary(it.next(), valueType));
93             }
94         }
95         else {
96             result.addAll(this.sourceSet);
97         }
98         return result;
99     }
100
101 }
102
Popular Tags