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