001/*
002 * Copyright 2002-2012 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.support;
018
019import java.util.LinkedHashSet;
020import java.util.Set;
021
022import org.springframework.beans.BeanMetadataElement;
023import org.springframework.beans.Mergeable;
024
025/**
026 * Tag collection class used to hold managed Set values, which may
027 * include runtime bean references (to be resolved into bean objects).
028 *
029 * @author Juergen Hoeller
030 * @author Rob Harrop
031 * @since 21.01.2004
032 */
033@SuppressWarnings("serial")
034public class ManagedSet<E> extends LinkedHashSet<E> implements Mergeable, BeanMetadataElement {
035
036        private Object source;
037
038        private String elementTypeName;
039
040        private boolean mergeEnabled;
041
042
043        public ManagedSet() {
044        }
045
046        public ManagedSet(int initialCapacity) {
047                super(initialCapacity);
048        }
049
050
051        /**
052         * Set the configuration source {@code Object} for this metadata element.
053         * <p>The exact type of the object will depend on the configuration mechanism used.
054         */
055        public void setSource(Object source) {
056                this.source = source;
057        }
058
059        @Override
060        public Object getSource() {
061                return this.source;
062        }
063
064        /**
065         * Set the default element type name (class name) to be used for this set.
066         */
067        public void setElementTypeName(String elementTypeName) {
068                this.elementTypeName = elementTypeName;
069        }
070
071        /**
072         * Return the default element type name (class name) to be used for this set.
073         */
074        public String getElementTypeName() {
075                return this.elementTypeName;
076        }
077
078        /**
079         * Set whether merging should be enabled for this collection,
080         * in case of a 'parent' collection value being present.
081         */
082        public void setMergeEnabled(boolean mergeEnabled) {
083                this.mergeEnabled = mergeEnabled;
084        }
085
086        @Override
087        public boolean isMergeEnabled() {
088                return this.mergeEnabled;
089        }
090
091        @Override
092        @SuppressWarnings("unchecked")
093        public Set<E> merge(Object parent) {
094                if (!this.mergeEnabled) {
095                        throw new IllegalStateException("Not allowed to merge when the 'mergeEnabled' property is set to 'false'");
096                }
097                if (parent == null) {
098                        return this;
099                }
100                if (!(parent instanceof Set)) {
101                        throw new IllegalArgumentException("Cannot merge with object of type [" + parent.getClass() + "]");
102                }
103                Set<E> merged = new ManagedSet<E>();
104                merged.addAll((Set<E>) parent);
105                merged.addAll(this);
106                return merged;
107        }
108
109}