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