Source code
Description: source: package: org.apache.commons.configuration2.beanutils, class: CombinedBeanDeclaration

    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 *     https://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 <strong>null</strong>
    057     */
    058    public CombinedBeanDeclaration(final BeanDeclaration... decl) {
    059        childDeclarations = new ArrayList<>(Arrays.asList(decl));
    060    }
    061
    062    private <T> T findFirst(final Function<? super BeanDeclaration, ? extends T> mapper) {
    063        return childDeclarations.stream().map(mapper).filter(Objects::nonNull).findFirst().orElse(null);
    064    }
    065
    066    private Map<String, Object> get(final Function<? super BeanDeclaration, ? extends Map<String, Object>> mapper) {
    067        @SuppressWarnings("unchecked")
    068        final ArrayList<BeanDeclaration> temp = (ArrayList<BeanDeclaration>) childDeclarations.clone();
    069        Collections.reverse(temp);
    070        return temp.stream().map(mapper).filter(Objects::nonNull).collect(HashMap::new, HashMap::putAll, HashMap::putAll);
    071    }
    072
    073    /**
    074     * {@inheritDoc} This implementation iterates over the list of child declarations and asks them for the bean class name.
    075     * The first non-<strong>null</strong> value is returned. If none of the child declarations have a defined bean class, result is
    076     * <strong>null</strong>.
    077     */
    078    @Override
    079    public String getBeanClassName() {
    080        return findFirst(BeanDeclaration::getBeanClassName);
    081    }
    082
    083    /**
    084     * {@inheritDoc} This implementation iterates over the list of child declarations and asks them for a bean factory name.
    085     * The first non-<strong>null</strong> value is returned. If none of the child declarations have a defined bean factory name,
    086     * result is <strong>null</strong>.
    087     */
    088    @Override
    089    public String getBeanFactoryName() {
    090        return findFirst(BeanDeclaration::getBeanFactoryName);
    091    }
    092
    093    /**
    094     * {@inheritDoc} This implementation iterates over the list of child declarations and asks them for a bean factory
    095     * parameter. The first non-<strong>null</strong> value is returned. If none of the child declarations have a defined bean factory
    096     * parameter, result is <strong>null</strong>.
    097     */
    098    @Override
    099    public Object getBeanFactoryParameter() {
    100        return findFirst(BeanDeclaration::getBeanFactoryParameter);
    101    }
    102
    103    /**
    104     * {@inheritDoc} This implementation creates a union of the properties returned by all child declarations. If a property
    105     * is defined in multiple child declarations, the declaration that comes before in the list of children takes
    106     * precedence.
    107     */
    108    @Override
    109    public Map<String, Object> getBeanProperties() {
    110        return get(BeanDeclaration::getBeanProperties);
    111    }
    112
    113    /**
    114     * {@inheritDoc} This implementation iterates over the list of child declarations and asks them for constructor
    115     * arguments. The first non-<strong>null</strong> and non empty collection is returned. If none of the child declarations provide
    116     * constructor arguments, result is an empty collection.
    117     */
    118    @Override
    119    public Collection<ConstructorArg> getConstructorArgs() {
    120        for (final BeanDeclaration d : childDeclarations) {
    121            final Collection<ConstructorArg> args = d.getConstructorArgs();
    122            if (args != null && !args.isEmpty()) {
    123                return args;
    124            }
    125        }
    126        return Collections.emptyList();
    127    }
    128
    129    /**
    130     * {@inheritDoc} This implementation creates a union of the nested bean declarations returned by all child declarations.
    131     * If a complex property is defined in multiple child declarations, the declaration that comes before in the list of
    132     * children takes precedence.
    133     */
    134    @Override
    135    public Map<String, Object> getNestedBeanDeclarations() {
    136        return get(BeanDeclaration::getNestedBeanDeclarations);
    137    }
    138}