MultiWrapDynaClass.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.builder.combined;

  18. import java.util.Collection;
  19. import java.util.HashMap;
  20. import java.util.LinkedList;
  21. import java.util.Map;
  22. import java.util.stream.Stream;

  23. import org.apache.commons.beanutils.DynaBean;
  24. import org.apache.commons.beanutils.DynaClass;
  25. import org.apache.commons.beanutils.DynaProperty;

  26. /**
  27.  * <p>
  28.  * An implementation of {@code DynaClass} which combines the properties of multiple other {@code DynaClass} instances.
  29.  * </p>
  30.  *
  31.  * @since 2.0
  32.  */
  33. final class MultiWrapDynaClass implements DynaClass {
  34.     /** An empty array for converting the properties collection to an array. */
  35.     private static final DynaProperty[] EMPTY_PROPS = {};

  36.     /** A collection with all properties of this class. */
  37.     private final Collection<DynaProperty> properties;

  38.     /** A map for accessing properties by name. */
  39.     private final Map<String, DynaProperty> namedProperties;

  40.     /**
  41.      * Creates a new instance of {@code MultiWrapDynaClass} and initializes it with the collection of classes to be wrapped.
  42.      *
  43.      * @param wrappedCls the collection with wrapped classes
  44.      */
  45.     public MultiWrapDynaClass(final Collection<? extends DynaClass> wrappedCls) {
  46.         properties = new LinkedList<>();
  47.         namedProperties = new HashMap<>();
  48.         initProperties(wrappedCls);
  49.     }

  50.     @Override
  51.     public DynaProperty[] getDynaProperties() {
  52.         return properties.toArray(EMPTY_PROPS);
  53.     }

  54.     @Override
  55.     public DynaProperty getDynaProperty(final String name) {
  56.         return namedProperties.get(name);
  57.     }

  58.     /**
  59.      * {@inheritDoc} The name of this class is not relevant.
  60.      */
  61.     @Override
  62.     public String getName() {
  63.         return null;
  64.     }

  65.     /**
  66.      * Initializes the members related to the properties of the wrapped classes.
  67.      *
  68.      * @param wrappedCls the collection with the wrapped classes
  69.      */
  70.     private void initProperties(final Collection<? extends DynaClass> wrappedCls) {
  71.         wrappedCls.forEach(cls -> Stream.of(cls.getDynaProperties()).forEach(p -> {
  72.             properties.add(p);
  73.             namedProperties.put(p.getName(), p);
  74.         }));
  75.     }

  76.     /**
  77.      * {@inheritDoc} This implementation always throws an exception because it is not possible to instantiate a bean of
  78.      * multiple classes.
  79.      */
  80.     @Override
  81.     public DynaBean newInstance() throws IllegalAccessException, InstantiationException {
  82.         throw new UnsupportedOperationException("Cannot create an instance of MultiWrapDynaBean!");
  83.     }
  84. }