View Javadoc
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    *     https://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  
19  import java.util.Collection;
20  import java.util.HashMap;
21  import java.util.LinkedList;
22  import java.util.Map;
23  import java.util.stream.Stream;
24  
25  import org.apache.commons.beanutils.DynaBean;
26  import org.apache.commons.beanutils.DynaClass;
27  import org.apache.commons.beanutils.DynaProperty;
28  
29  /**
30   * <p>
31   * An implementation of {@code DynaClass} which combines the properties of multiple other {@code DynaClass} instances.
32   * </p>
33   *
34   * @since 2.0
35   */
36  final class MultiWrapDynaClass implements DynaClass {
37  
38      /** An empty array for converting the properties collection to an array. */
39      private static final DynaProperty[] EMPTY_PROPS = {};
40  
41      /** A collection with all properties of this class. */
42      private final Collection<DynaProperty> properties;
43  
44      /** A map for accessing properties by name. */
45      private final Map<String, DynaProperty> namedProperties;
46  
47      /**
48       * Creates a new instance of {@code MultiWrapDynaClass} and initializes it with the collection of classes to be wrapped.
49       *
50       * @param wrappedCls the collection with wrapped classes
51       */
52      public MultiWrapDynaClass(final Collection<? extends DynaClass> wrappedCls) {
53          properties = new LinkedList<>();
54          namedProperties = new HashMap<>();
55          initProperties(wrappedCls);
56      }
57  
58      @Override
59      public DynaProperty[] getDynaProperties() {
60          return properties.toArray(EMPTY_PROPS);
61      }
62  
63      @Override
64      public DynaProperty getDynaProperty(final String name) {
65          return namedProperties.get(name);
66      }
67  
68      /**
69       * {@inheritDoc} The name of this class is not relevant.
70       */
71      @Override
72      public String getName() {
73          return null;
74      }
75  
76      /**
77       * Initializes the members related to the properties of the wrapped classes.
78       *
79       * @param wrappedCls the collection with the wrapped classes
80       */
81      private void initProperties(final Collection<? extends DynaClass> wrappedCls) {
82          wrappedCls.forEach(cls -> Stream.of(cls.getDynaProperties()).forEach(p -> {
83              properties.add(p);
84              namedProperties.put(p.getName(), p);
85          }));
86      }
87  
88      /**
89       * {@inheritDoc} This implementation always throws an exception because it is not possible to instantiate a bean of
90       * multiple classes.
91       */
92      @Override
93      public DynaBean newInstance() throws IllegalAccessException, InstantiationException {
94          throw new UnsupportedOperationException("Cannot create an instance of MultiWrapDynaBean.");
95      }
96  }