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