    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *     http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017package org.apache.commons.configuration2.beanutils;
    018
    019import java.util.ArrayList;
    020import java.util.Arrays;
    021import java.util.Collection;
    022import java.util.Collections;
    023import java.util.HashMap;
    024import java.util.Map;
    025import java.util.Objects;
    026import java.util.function.Function;
    027
    028/**
    029 * <p>
    030 * A special implementation of the {@code BeanDeclaration} interface which allows combining multiple
    031 * {@code BeanDeclaration} objects.
    032 * </p>
    033 * <p>
    034 * An instance of this class can be used if a bean is defined using multiple sources. For instance, there can be one
    035 * definition with default values and one with actual values; if actual values are provided, they are used; otherwise,
    036 * the default values apply.
    037 * </p>
    038 * <p>
    039 * When constructing an instance an arbitrary number of child {@code BeanDeclaration} objects can be specified. The
    040 * implementations of the {@code BeanDeclaration} methods implement a logical combination of the data returned by these
    041 * child declarations. The order in which child declarations are added is relevant; first entries take precedence over
    042 * later ones. The comments of the single methods explain in which way a combination of the child declarations is built.
    043 * </p>
    044 *
    045 * @since 2.0
    046 */
    047public class CombinedBeanDeclaration implements BeanDeclaration {
    048
    049    /** A list with the child declarations. */
    050    private final ArrayList<BeanDeclaration> childDeclarations;
    051
    052    /**
    053     * Constructs a new instance of {@code CombinedBeanDeclaration} and initializes it with the given child declarations.
    054     *
    055     * @param decl the child declarations
    056     * @throws NullPointerException if the array with child declarations is <b>null</b>
    057     */
    058    public CombinedBeanDeclaration(final BeanDeclaration... decl) {
    059        childDeclarations = new ArrayList<>(Arrays.asList(decl));
    060    }
    061
    062    /**
    063     * {@inheritDoc} This implementation iterates over the list of child declarations and asks them for a bean factory name.
    064     * The first non-<b>null</b> value is returned. If none of the child declarations have a defined bean factory name,
    065     * result is <b>null</b>.
    066     */
    067    @Override
    068    public String getBeanFactoryName() {
    069        return findFirst(BeanDeclaration::getBeanFactoryName);
    070    }
    071
    072    private <T> T findFirst(final Function<? super BeanDeclaration, ? extends T> mapper) {
    073        return childDeclarations.stream().map(mapper).filter(Objects::nonNull).findFirst().orElse(null);
    074    }
    075
    076    /**
    077     * {@inheritDoc} This implementation iterates over the list of child declarations and asks them for a bean factory
    078     * parameter. The first non-<b>null</b> value is returned. If none of the child declarations have a defined bean factory
    079     * parameter, result is <b>null</b>.
    080     */
    081    @Override
    082    public Object getBeanFactoryParameter() {
    083        return findFirst(BeanDeclaration::getBeanFactoryParameter);
    084    }
    085
    086    /**
    087     * {@inheritDoc} This implementation iterates over the list of child declarations and asks them for the bean class name.
    088     * The first non-<b>null</b> value is returned. If none of the child declarations have a defined bean class, result is
    089     * <b>null</b>.
    090     */
    091    @Override
    092    public String getBeanClassName() {
    093        return findFirst(BeanDeclaration::getBeanClassName);
    094    }
    095
    096    /**
    097     * {@inheritDoc} This implementation creates a union of the properties returned by all child declarations. If a property
    098     * is defined in multiple child declarations, the declaration that comes before in the list of children takes
    099     * precedence.
    100     */
    101    @Override
    102    public Map<String, Object> getBeanProperties() {
    103        return get(BeanDeclaration::getBeanProperties);
    104    }
    105
    106    private Map<String, Object> get(final Function<? super BeanDeclaration, ? extends Map<String, Object>> mapper) {
    107        @SuppressWarnings("unchecked")
    108        final ArrayList<BeanDeclaration> temp = (ArrayList<BeanDeclaration>) childDeclarations.clone();
    109        Collections.reverse(temp);
    110        return temp.stream().map(mapper).filter(Objects::nonNull).collect(HashMap::new, HashMap::putAll, HashMap::putAll);
    111    }
    112
    113    /**
    114     * {@inheritDoc} This implementation creates a union of the nested bean declarations returned by all child declarations.
    115     * If a complex property is defined in multiple child declarations, the declaration that comes before in the list of
    116     * children takes precedence.
    117     */
    118    @Override
    119    public Map<String, Object> getNestedBeanDeclarations() {
    120        return get(BeanDeclaration::getNestedBeanDeclarations);
    121    }
    122
    123    /**
    124     * {@inheritDoc} This implementation iterates over the list of child declarations and asks them for constructor
    125     * arguments. The first non-<b>null</b> and non empty collection is returned. If none of the child declarations provide
    126     * constructor arguments, result is an empty collection.
    127     */
    128    @Override
    129    public Collection<ConstructorArg> getConstructorArgs() {
    130        for (final BeanDeclaration d : childDeclarations) {
    131            final Collection<ConstructorArg> args = d.getConstructorArgs();
    132            if (args != null && !args.isEmpty()) {
    133                return args;
    134            }
    135        }
    136        return Collections.emptyList();
    137    }
    138}