CombinedBeanDeclaration.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.configuration2.beanutils;

  18. import java.util.ArrayList;
  19. import java.util.Arrays;
  20. import java.util.Collection;
  21. import java.util.Collections;
  22. import java.util.HashMap;
  23. import java.util.Map;
  24. import java.util.Objects;
  25. import java.util.function.Function;

  26. /**
  27.  * <p>
  28.  * A special implementation of the {@code BeanDeclaration} interface which allows combining multiple
  29.  * {@code BeanDeclaration} objects.
  30.  * </p>
  31.  * <p>
  32.  * An instance of this class can be used if a bean is defined using multiple sources. For instance, there can be one
  33.  * definition with default values and one with actual values; if actual values are provided, they are used; otherwise,
  34.  * the default values apply.
  35.  * </p>
  36.  * <p>
  37.  * When constructing an instance an arbitrary number of child {@code BeanDeclaration} objects can be specified. The
  38.  * implementations of the {@code BeanDeclaration} methods implement a logical combination of the data returned by these
  39.  * child declarations. The order in which child declarations are added is relevant; first entries take precedence over
  40.  * later ones. The comments of the single methods explain in which way a combination of the child declarations is built.
  41.  * </p>
  42.  *
  43.  * @since 2.0
  44.  */
  45. public class CombinedBeanDeclaration implements BeanDeclaration {

  46.     /** A list with the child declarations. */
  47.     private final ArrayList<BeanDeclaration> childDeclarations;

  48.     /**
  49.      * Constructs a new instance of {@code CombinedBeanDeclaration} and initializes it with the given child declarations.
  50.      *
  51.      * @param decl the child declarations
  52.      * @throws NullPointerException if the array with child declarations is <strong>null</strong>
  53.      */
  54.     public CombinedBeanDeclaration(final BeanDeclaration... decl) {
  55.         childDeclarations = new ArrayList<>(Arrays.asList(decl));
  56.     }

  57.     private <T> T findFirst(final Function<? super BeanDeclaration, ? extends T> mapper) {
  58.         return childDeclarations.stream().map(mapper).filter(Objects::nonNull).findFirst().orElse(null);
  59.     }

  60.     private Map<String, Object> get(final Function<? super BeanDeclaration, ? extends Map<String, Object>> mapper) {
  61.         @SuppressWarnings("unchecked")
  62.         final ArrayList<BeanDeclaration> temp = (ArrayList<BeanDeclaration>) childDeclarations.clone();
  63.         Collections.reverse(temp);
  64.         return temp.stream().map(mapper).filter(Objects::nonNull).collect(HashMap::new, HashMap::putAll, HashMap::putAll);
  65.     }

  66.     /**
  67.      * {@inheritDoc} This implementation iterates over the list of child declarations and asks them for the bean class name.
  68.      * The first non-<strong>null</strong> value is returned. If none of the child declarations have a defined bean class, result is
  69.      * <strong>null</strong>.
  70.      */
  71.     @Override
  72.     public String getBeanClassName() {
  73.         return findFirst(BeanDeclaration::getBeanClassName);
  74.     }

  75.     /**
  76.      * {@inheritDoc} This implementation iterates over the list of child declarations and asks them for a bean factory name.
  77.      * The first non-<strong>null</strong> value is returned. If none of the child declarations have a defined bean factory name,
  78.      * result is <strong>null</strong>.
  79.      */
  80.     @Override
  81.     public String getBeanFactoryName() {
  82.         return findFirst(BeanDeclaration::getBeanFactoryName);
  83.     }

  84.     /**
  85.      * {@inheritDoc} This implementation iterates over the list of child declarations and asks them for a bean factory
  86.      * parameter. The first non-<strong>null</strong> value is returned. If none of the child declarations have a defined bean factory
  87.      * parameter, result is <strong>null</strong>.
  88.      */
  89.     @Override
  90.     public Object getBeanFactoryParameter() {
  91.         return findFirst(BeanDeclaration::getBeanFactoryParameter);
  92.     }

  93.     /**
  94.      * {@inheritDoc} This implementation creates a union of the properties returned by all child declarations. If a property
  95.      * is defined in multiple child declarations, the declaration that comes before in the list of children takes
  96.      * precedence.
  97.      */
  98.     @Override
  99.     public Map<String, Object> getBeanProperties() {
  100.         return get(BeanDeclaration::getBeanProperties);
  101.     }

  102.     /**
  103.      * {@inheritDoc} This implementation iterates over the list of child declarations and asks them for constructor
  104.      * arguments. The first non-<strong>null</strong> and non empty collection is returned. If none of the child declarations provide
  105.      * constructor arguments, result is an empty collection.
  106.      */
  107.     @Override
  108.     public Collection<ConstructorArg> getConstructorArgs() {
  109.         for (final BeanDeclaration d : childDeclarations) {
  110.             final Collection<ConstructorArg> args = d.getConstructorArgs();
  111.             if (args != null && !args.isEmpty()) {
  112.                 return args;
  113.             }
  114.         }
  115.         return Collections.emptyList();
  116.     }

  117.     /**
  118.      * {@inheritDoc} This implementation creates a union of the nested bean declarations returned by all child declarations.
  119.      * If a complex property is defined in multiple child declarations, the declaration that comes before in the list of
  120.      * children takes precedence.
  121.      */
  122.     @Override
  123.     public Map<String, Object> getNestedBeanDeclarations() {
  124.         return get(BeanDeclaration::getNestedBeanDeclarations);
  125.     }
  126. }