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.Iterator;
020: import java.util.LinkedHashSet;
021: import java.util.Set;
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 Set instances. Allows for central setup
030: * of Sets via the "set" element in XML bean definitions.
031: *
032: * @author Juergen Hoeller
033: * @since 09.12.2003
034: * @see ListFactoryBean
035: * @see MapFactoryBean
036: */
037: public class SetFactoryBean extends AbstractFactoryBean {
038:
039: private Set sourceSet;
040:
041: private Class targetSetClass;
042:
043: /**
044: * Set the source Set, typically populated via XML "set" elements.
045: */
046: public void setSourceSet(Set sourceSet) {
047: this .sourceSet = sourceSet;
048: }
049:
050: /**
051: * Set the class to use for the target Set. Can be populated with a fully
052: * qualified class name when defined in a Spring application context.
053: * <p>Default is a linked HashSet, keeping the registration order.
054: * @see java.util.LinkedHashSet
055: */
056: public void setTargetSetClass(Class targetSetClass) {
057: if (targetSetClass == null) {
058: throw new IllegalArgumentException(
059: "'targetSetClass' must not be null");
060: }
061: if (!Set.class.isAssignableFrom(targetSetClass)) {
062: throw new IllegalArgumentException(
063: "'targetSetClass' must implement [java.util.Set]");
064: }
065: this .targetSetClass = targetSetClass;
066: }
067:
068: public Class getObjectType() {
069: return Set.class;
070: }
071:
072: protected Object createInstance() {
073: if (this .sourceSet == null) {
074: throw new IllegalArgumentException(
075: "'sourceSet' is required");
076: }
077: Set result = null;
078: if (this .targetSetClass != null) {
079: result = (Set) BeanUtils
080: .instantiateClass(this .targetSetClass);
081: } else {
082: result = new LinkedHashSet(this .sourceSet.size());
083: }
084: Class valueType = null;
085: if (this .targetSetClass != null && JdkVersion.isAtLeastJava15()) {
086: valueType = GenericCollectionTypeResolver
087: .getCollectionType(this .targetSetClass);
088: }
089: if (valueType != null) {
090: TypeConverter converter = getBeanTypeConverter();
091: for (Iterator it = this.sourceSet.iterator(); it.hasNext();) {
092: result.add(converter.convertIfNecessary(it.next(),
093: valueType));
094: }
095: } else {
096: result.addAll(this.sourceSet);
097: }
098: return result;
099: }
100:
101: }
|