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.ArrayList;
20 import java.util.Collection;
21 import java.util.HashMap;
22 import java.util.Map;
23
24 import org.apache.commons.beanutils.DynaBean;
25 import org.apache.commons.beanutils.DynaClass;
26 import org.apache.commons.beanutils.DynaProperty;
27 import org.apache.commons.configuration2.beanutils.BeanHelper;
28
29 /**
30 * <p>
31 * An implementation of the {@code DynaBean} interfaces which wraps multiple other beans.
32 * </p>
33 * <p>
34 * An instance of this class is constructed with a collection of beans to be wrapped. When reading or writing a property
35 * the wrapped bean which defines this property is determined, and the operation is executed on this bean.
36 * </p>
37 * <p>
38 * The wrapped beans should have disjunct properties. Otherwise, it is undefined which bean property is read or written.
39 * </p>
40 *
41 * @since 2.0
42 */
43 final class MultiWrapDynaBean implements DynaBean {
44 /**
45 * Creates a {@code DynaBean} object for the given bean.
46 *
47 * @param bean the bean
48 * @return the {@code DynaBean} for this bean
49 */
50 private static DynaBean createDynaBean(final Object bean) {
51 if (bean instanceof DynaBean) {
52 return (DynaBean) bean;
53 }
54 return BeanHelper.createWrapDynaBean(bean);
55 }
56
57 /** Stores the class of this DynaBean. */
58 private final DynaClass dynaClass;
59
60 /** A map which associates property names with their defining beans. */
61 private final Map<String, DynaBean> propsToBeans;
62
63 /**
64 * Creates a new instance of {@code MultiWrapDynaBean} and initializes it with the given collections of beans to be
65 * wrapped.
66 *
67 * @param beans the wrapped beans
68 */
69 public MultiWrapDynaBean(final Collection<?> beans) {
70 propsToBeans = new HashMap<>();
71 final Collection<DynaClass> beanClasses = new ArrayList<>(beans.size());
72
73 beans.forEach(bean -> {
74 final DynaBean dynaBean = createDynaBean(bean);
75 final DynaClass beanClass = dynaBean.getDynaClass();
76 for (final DynaProperty prop : beanClass.getDynaProperties()) {
77 // ensure an order of properties
78 propsToBeans.putIfAbsent(prop.getName(), dynaBean);
79 }
80 beanClasses.add(beanClass);
81 });
82
83 dynaClass = new MultiWrapDynaClass(beanClasses);
84 }
85
86 /**
87 * {@inheritDoc} This operation is not supported by the {@code WrapDynaBean} objects used internally by this class.
88 * Therefore, just an exception is thrown.
89 */
90 @Override
91 public boolean contains(final String name, final String key) {
92 throw new UnsupportedOperationException("contains() operation not supported!");
93 }
94
95 /**
96 * Returns the bean instance to which the given property belongs. If no such bean is found, an arbitrary bean is
97 * returned. (This causes the operation on this bean to fail with a meaningful error message.)
98 *
99 * @param property the property name
100 * @return the bean defining this property
101 */
102 private DynaBean fetchBean(final String property) {
103 DynaBean dynaBean = propsToBeans.get(property);
104 if (dynaBean == null) {
105 dynaBean = propsToBeans.values().iterator().next();
106 }
107 return dynaBean;
108 }
109
110 @Override
111 public Object get(final String name) {
112 return fetchBean(name).get(name);
113 }
114
115 @Override
116 public Object get(final String name, final int index) {
117 return fetchBean(name).get(name, index);
118 }
119
120 @Override
121 public Object get(final String name, final String key) {
122 return fetchBean(name).get(name, key);
123 }
124
125 /**
126 * {@inheritDoc} This implementation returns an instance of {@code MultiWrapDynaClass}.
127 */
128 @Override
129 public DynaClass getDynaClass() {
130 return dynaClass;
131 }
132
133 /**
134 * {@inheritDoc} This operation is not supported by the {@code WrapDynaBean} objects used internally by this class.
135 * Therefore, just an exception is thrown.
136 */
137 @Override
138 public void remove(final String name, final String key) {
139 throw new UnsupportedOperationException("remove() operation not supported!");
140 }
141
142 @Override
143 public void set(final String name, final int index, final Object value) {
144 fetchBean(name).set(name, index, value);
145 }
146
147 @Override
148 public void set(final String name, final Object value) {
149 fetchBean(name).set(name, value);
150 }
151
152 @Override
153 public void set(final String name, final String key, final Object value) {
154 fetchBean(name).set(name, key, value);
155 }
156 }