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
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 /**
58 * {@inheritDoc} The name of this class is not relevant.
59 */
60 @Override
61 public String getName() {
62 return null;
63 }
64
65 @Override
66 public DynaProperty getDynaProperty(final String name) {
67 return namedProperties.get(name);
68 }
69
70 @Override
71 public DynaProperty[] getDynaProperties() {
72 return properties.toArray(EMPTY_PROPS);
73 }
74
75 /**
76 * {@inheritDoc} This implementation always throws an exception because it is not possible to instantiate a bean of
77 * multiple classes.
78 */
79 @Override
80 public DynaBean newInstance() throws IllegalAccessException, InstantiationException {
81 throw new UnsupportedOperationException("Cannot create an instance of MultiWrapDynaBean!");
82 }
83
84 /**
85 * Initializes the members related to the properties of the wrapped classes.
86 *
87 * @param wrappedCls the collection with the wrapped classes
88 */
89 private void initProperties(final Collection<? extends DynaClass> wrappedCls) {
90 wrappedCls.forEach(cls -> Stream.of(cls.getDynaProperties()).forEach(p -> {
91 properties.add(p);
92 namedProperties.put(p.getName(), p);
93 }));
94 }
95 }