001/*
002 * Copyright 2002-2017 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 *      https://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
017package org.springframework.beans.factory.config;
018
019import java.util.LinkedHashSet;
020import java.util.Set;
021
022import org.springframework.beans.BeanUtils;
023import org.springframework.beans.TypeConverter;
024import org.springframework.core.ResolvableType;
025import org.springframework.lang.Nullable;
026
027/**
028 * Simple factory for shared Set instances. Allows for central setup
029 * of Sets via the "set" element in XML bean definitions.
030 *
031 * @author Juergen Hoeller
032 * @since 09.12.2003
033 * @see ListFactoryBean
034 * @see MapFactoryBean
035 */
036public class SetFactoryBean extends AbstractFactoryBean<Set<Object>> {
037
038        @Nullable
039        private Set<?> sourceSet;
040
041        @SuppressWarnings("rawtypes")
042        @Nullable
043        private Class<? extends Set> targetSetClass;
044
045
046        /**
047         * Set the source Set, typically populated via XML "set" elements.
048         */
049        public void setSourceSet(Set<?> sourceSet) {
050                this.sourceSet = sourceSet;
051        }
052
053        /**
054         * Set the class to use for the target Set. Can be populated with a fully
055         * qualified class name when defined in a Spring application context.
056         * <p>Default is a linked HashSet, keeping the registration order.
057         * @see java.util.LinkedHashSet
058         */
059        @SuppressWarnings("rawtypes")
060        public void setTargetSetClass(@Nullable Class<? extends Set> targetSetClass) {
061                if (targetSetClass == null) {
062                        throw new IllegalArgumentException("'targetSetClass' must not be null");
063                }
064                if (!Set.class.isAssignableFrom(targetSetClass)) {
065                        throw new IllegalArgumentException("'targetSetClass' must implement [java.util.Set]");
066                }
067                this.targetSetClass = targetSetClass;
068        }
069
070
071        @Override
072        @SuppressWarnings("rawtypes")
073        public Class<Set> getObjectType() {
074                return Set.class;
075        }
076
077        @Override
078        @SuppressWarnings("unchecked")
079        protected Set<Object> createInstance() {
080                if (this.sourceSet == null) {
081                        throw new IllegalArgumentException("'sourceSet' is required");
082                }
083                Set<Object> result = null;
084                if (this.targetSetClass != null) {
085                        result = BeanUtils.instantiateClass(this.targetSetClass);
086                }
087                else {
088                        result = new LinkedHashSet<>(this.sourceSet.size());
089                }
090                Class<?> valueType = null;
091                if (this.targetSetClass != null) {
092                        valueType = ResolvableType.forClass(this.targetSetClass).asCollection().resolveGeneric();
093                }
094                if (valueType != null) {
095                        TypeConverter converter = getBeanTypeConverter();
096                        for (Object elem : this.sourceSet) {
097                                result.add(converter.convertIfNecessary(elem, valueType));
098                        }
099                }
100                else {
101                        result.addAll(this.sourceSet);
102                }
103                return result;
104        }
105
106}