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