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