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.Set;
021:
022: import org.springframework.beans.BeanUtils;
023: import org.springframework.beans.TypeConverter;
024: import org.springframework.core.CollectionFactory;
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: * If no linked Set implementation is available, a plain HashSet will
055: * be used as fallback (not keeping the registration order).
056: * @see org.springframework.core.CollectionFactory#createLinkedSetIfPossible
057: */
058: public void setTargetSetClass(Class targetSetClass) {
059: if (targetSetClass == null) {
060: throw new IllegalArgumentException(
061: "'targetSetClass' must not be null");
062: }
063: if (!Set.class.isAssignableFrom(targetSetClass)) {
064: throw new IllegalArgumentException(
065: "'targetSetClass' must implement [java.util.Set]");
066: }
067: this .targetSetClass = targetSetClass;
068: }
069:
070: public Class getObjectType() {
071: return Set.class;
072: }
073:
074: protected Object createInstance() {
075: if (this .sourceSet == null) {
076: throw new IllegalArgumentException(
077: "'sourceSet' is required");
078: }
079: Set result = null;
080: if (this .targetSetClass != null) {
081: result = (Set) BeanUtils
082: .instantiateClass(this .targetSetClass);
083: } else {
084: result = CollectionFactory
085: .createLinkedSetIfPossible(this .sourceSet.size());
086: }
087: Class valueType = null;
088: if (this .targetSetClass != null && JdkVersion.isAtLeastJava15()) {
089: valueType = GenericCollectionTypeResolver
090: .getCollectionType(this .targetSetClass);
091: }
092: if (valueType != null) {
093: TypeConverter converter = getBeanTypeConverter();
094: for (Iterator it = this.sourceSet.iterator(); it.hasNext();) {
095: result.add(converter.convertIfNecessary(it.next(),
096: valueType));
097: }
098: } else {
099: result.addAll(this.sourceSet);
100: }
101: return result;
102: }
103:
104: }
|