DynaBeanPropertyMapDecorator.java

  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.beanutils2;

  18. /**
  19.  * <p>
  20.  * Decorates a {@link DynaBean} to provide {@code Map} behavior.
  21.  * </p>
  22.  *
  23.  * <p>
  24.  * The motivation for this implementation is to provide access to {@link DynaBean} properties in technologies that are unaware of BeanUtils and
  25.  * {@link DynaBean}s - such as the expression languages of JSTL and JSF.
  26.  * </p>
  27.  *
  28.  * <p>
  29.  * 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
  30.  * on the DynaBean implementation:
  31.  * </p>
  32.  *
  33.  * <pre>
  34.  * <code>
  35.  *         public Map&lt;String, Object&gt; getMap() {
  36.  *             return new DynaBeanPropertyMapDecorator(this);
  37.  *         }</code>
  38.  * </pre>
  39.  *
  40.  * <p>
  41.  * This, for example, could be used in JSTL in the following way to access a DynaBean's {@code fooProperty}:
  42.  * </p>
  43.  * <ul>
  44.  * <li>{@code ${myDynaBean.<strong>map</strong>.fooProperty}}</li>
  45.  * </ul>
  46.  *
  47.  * <h2>Usage</h2>
  48.  *
  49.  * <p>
  50.  * To decorate a {@link DynaBean} simply instantiate this class with the target {@link DynaBean}:
  51.  * </p>
  52.  *
  53.  * <ul>
  54.  * <li>{@code Map&lt;String, Object&gt; fooMap = new DynaBeanPropertyMapDecorator(fooDynaBean);}</li>
  55.  * </ul>
  56.  *
  57.  * <p>
  58.  * The above example creates a <strong><em>read only</em></strong> {@code Map}. To create a {@code Map} which can be modified, construct a
  59.  * {@code DynaBeanPropertyMapDecorator} with the <strong><em>read only</em></strong> attribute set to {@code false}:
  60.  * </p>
  61.  *
  62.  * <ul>
  63.  * <li>{@code Map<String, Object> fooMap =
  64.  *   new DynaBeanPropertyMapDecorator(fooDynaBean, false);}</li>
  65.  * </ul>
  66.  *
  67.  * <h2>Limitations</h2>
  68.  * <p>
  69.  * In this implementation the {@code entrySet()</code>, <code>keySet()}
  70.  *    and {@code values()} methods create an <strong><em>unmodifiable</em></strong>
  71.  *    {@code Set</code> and it does not support the Map's <code>clear()} and {@code remove()} operations.
  72.  * </p>
  73.  *
  74.  * @since 1.9.0
  75.  */
  76. public class DynaBeanPropertyMapDecorator extends BaseDynaBeanMapDecorator<String> {

  77.     /**
  78.      * Constructs a read only Map for the specified {@link DynaBean}.
  79.      *
  80.      * @param dynaBean The dyna bean being decorated
  81.      * @throws IllegalArgumentException if the {@link DynaBean} is null.
  82.      */
  83.     public DynaBeanPropertyMapDecorator(final DynaBean dynaBean) {
  84.         super(dynaBean);
  85.     }

  86.     /**
  87.      * Constructs a Map for the specified {@link DynaBean}.
  88.      *
  89.      * @param dynaBean The dyna bean being decorated
  90.      * @param readOnly {@code true} if the Map is read only otherwise {@code false}
  91.      * @throws IllegalArgumentException if the {@link DynaBean} is null.
  92.      */
  93.     public DynaBeanPropertyMapDecorator(final DynaBean dynaBean, final boolean readOnly) {
  94.         super(dynaBean, readOnly);
  95.     }

  96.     @Override
  97.     protected String convertKey(final String propertyName) {
  98.         return propertyName;
  99.     }
  100. }