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.beanutils2;
18
19 /**
20 * <p>
21 * Decorates a {@link DynaBean} to provide {@code Map} behavior.
22 * </p>
23 *
24 * <p>
25 * The motivation for this implementation is to provide access to {@link DynaBean} properties in technologies that are unaware of BeanUtils and
26 * {@link DynaBean}s - such as the expression languages of JSTL and JSF.
27 * </p>
28 *
29 * <p>
30 * This can be achieved either by wrapping the {@link DynaBean} prior to providing it to the technology to process or by providing a {@code Map} accessor method
31 * on the DynaBean implementation:
32 * </p>
33 *
34 * <pre>
35 * <code>
36 * public Map<String, Object> getMap() {
37 * return new DynaBeanPropertyMapDecorator(this);
38 * }</code>
39 * </pre>
40 *
41 * <p>
42 * This, for example, could be used in JSTL in the following way to access a DynaBean's {@code fooProperty}:
43 * </p>
44 * <ul>
45 * <li>{@code ${myDynaBean.<strong>map</strong>.fooProperty}}</li>
46 * </ul>
47 *
48 * <h2>Usage</h2>
49 *
50 * <p>
51 * To decorate a {@link DynaBean} simply instantiate this class with the target {@link DynaBean}:
52 * </p>
53 *
54 * <ul>
55 * <li>{@code Map<String, Object> fooMap = new DynaBeanPropertyMapDecorator(fooDynaBean);}</li>
56 * </ul>
57 *
58 * <p>
59 * The above example creates a <strong><em>read only</em></strong> {@code Map}. To create a {@code Map} which can be modified, construct a
60 * {@code DynaBeanPropertyMapDecorator} with the <strong><em>read only</em></strong> attribute set to {@code false}:
61 * </p>
62 *
63 * <ul>
64 * <li>{@code Map<String, Object> fooMap =
65 * new DynaBeanPropertyMapDecorator(fooDynaBean, false);}</li>
66 * </ul>
67 *
68 * <h2>Limitations</h2>
69 * <p>
70 * In this implementation the {@code entrySet()</code>, <code>keySet()}
71 * and {@code values()} methods create an <strong><em>unmodifiable</em></strong>
72 * {@code Set</code> and it does not support the Map's <code>clear()} and {@code remove()} operations.
73 * </p>
74 *
75 * @since 1.9.0
76 */
77 public class DynaBeanPropertyMapDecorator extends BaseDynaBeanMapDecorator<String> {
78
79 /**
80 * Constructs a read only Map for the specified {@link DynaBean}.
81 *
82 * @param dynaBean The dyna bean being decorated
83 * @throws IllegalArgumentException if the {@link DynaBean} is null.
84 */
85 public DynaBeanPropertyMapDecorator(final DynaBean dynaBean) {
86 super(dynaBean);
87 }
88
89 /**
90 * Constructs a Map for the specified {@link DynaBean}.
91 *
92 * @param dynaBean The dyna bean being decorated
93 * @param readOnly {@code true} if the Map is read only otherwise {@code false}
94 * @throws IllegalArgumentException if the {@link DynaBean} is null.
95 */
96 public DynaBeanPropertyMapDecorator(final DynaBean dynaBean, final boolean readOnly) {
97 super(dynaBean, readOnly);
98 }
99
100 @Override
101 protected String convertKey(final String propertyName) {
102 return propertyName;
103 }
104 }