KickJava   Java API By Example, From Geeks To Geeks.

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


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

37 public class ListFactoryBean extends AbstractFactoryBean {
38
39     private List JavaDoc sourceList;
40
41     private Class JavaDoc targetListClass;
42
43
44     /**
45      * Set the source List, typically populated via XML "list" elements.
46      */

47     public void setSourceList(List JavaDoc sourceList) {
48         this.sourceList = sourceList;
49     }
50
51     /**
52      * Set the class to use for the target List. Can be populated with a fully
53      * qualified class name when defined in a Spring application context.
54      * <p>Default is a <code>java.util.ArrayList</code>.
55      * @see java.util.ArrayList
56      */

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