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.parsing;
018
019import java.util.LinkedList;
020import java.util.List;
021
022import org.springframework.util.Assert;
023
024/**
025 * {@link ComponentDefinition} implementation that holds one or more nested
026 * {@link ComponentDefinition} instances, aggregating them into a named group
027 * of components.
028 *
029 * @author Juergen Hoeller
030 * @since 2.0.1
031 * @see #getNestedComponents()
032 */
033public class CompositeComponentDefinition extends AbstractComponentDefinition {
034
035        private final String name;
036
037        private final Object source;
038
039        private final List<ComponentDefinition> nestedComponents = new LinkedList<ComponentDefinition>();
040
041
042        /**
043         * Create a new CompositeComponentDefinition.
044         * @param name the name of the composite component
045         * @param source the source element that defines the root of the composite component
046         */
047        public CompositeComponentDefinition(String name, Object source) {
048                Assert.notNull(name, "Name must not be null");
049                this.name = name;
050                this.source = source;
051        }
052
053
054        @Override
055        public String getName() {
056                return this.name;
057        }
058
059        @Override
060        public Object getSource() {
061                return this.source;
062        }
063
064
065        /**
066         * Add the given component as nested element of this composite component.
067         * @param component the nested component to add
068         */
069        public void addNestedComponent(ComponentDefinition component) {
070                Assert.notNull(component, "ComponentDefinition must not be null");
071                this.nestedComponents.add(component);
072        }
073
074        /**
075         * Return the nested components that this composite component holds.
076         * @return the array of nested components, or an empty array if none
077         */
078        public ComponentDefinition[] getNestedComponents() {
079                return this.nestedComponents.toArray(new ComponentDefinition[this.nestedComponents.size()]);
080        }
081
082}