001: /*
002: * Copyright 2002-2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.beans.factory.config;
018:
019: import java.util.ArrayList;
020: import java.util.Iterator;
021: import java.util.List;
022:
023: import org.springframework.beans.BeanUtils;
024: import org.springframework.beans.TypeConverter;
025: import org.springframework.core.GenericCollectionTypeResolver;
026: import org.springframework.core.JdkVersion;
027:
028: /**
029: * Simple factory for shared List instances. Allows for central setup
030: * of Lists via the "list" element in XML bean definitions.
031: *
032: * @author Juergen Hoeller
033: * @since 09.12.2003
034: * @see SetFactoryBean
035: * @see MapFactoryBean
036: */
037: public class ListFactoryBean extends AbstractFactoryBean {
038:
039: private List sourceList;
040:
041: private Class targetListClass;
042:
043: /**
044: * Set the source List, typically populated via XML "list" elements.
045: */
046: public void setSourceList(List sourceList) {
047: this .sourceList = sourceList;
048: }
049:
050: /**
051: * Set the class to use for the target List. Can be populated with a fully
052: * qualified class name when defined in a Spring application context.
053: * <p>Default is a <code>java.util.ArrayList</code>.
054: * @see java.util.ArrayList
055: */
056: public void setTargetListClass(Class targetListClass) {
057: if (targetListClass == null) {
058: throw new IllegalArgumentException(
059: "'targetListClass' must not be null");
060: }
061: if (!List.class.isAssignableFrom(targetListClass)) {
062: throw new IllegalArgumentException(
063: "'targetListClass' must implement [java.util.List]");
064: }
065: this .targetListClass = targetListClass;
066: }
067:
068: public Class getObjectType() {
069: return List.class;
070: }
071:
072: protected Object createInstance() {
073: if (this .sourceList == null) {
074: throw new IllegalArgumentException(
075: "'sourceList' is required");
076: }
077: List result = null;
078: if (this .targetListClass != null) {
079: result = (List) BeanUtils
080: .instantiateClass(this .targetListClass);
081: } else {
082: result = new ArrayList(this .sourceList.size());
083: }
084: Class valueType = null;
085: if (this .targetListClass != null
086: && JdkVersion.isAtLeastJava15()) {
087: valueType = GenericCollectionTypeResolver
088: .getCollectionType(this .targetListClass);
089: }
090: if (valueType != null) {
091: TypeConverter converter = getBeanTypeConverter();
092: for (Iterator it = this.sourceList.iterator(); it.hasNext();) {
093: result.add(converter.convertIfNecessary(it.next(),
094: valueType));
095: }
096: } else {
097: result.addAll(this.sourceList);
098: }
099: return result;
100: }
101:
102: }
|