You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by oh...@apache.org on 2013/10/27 10:59:04 UTC

svn commit: r1536076 - /commons/proper/beanutils/branches/java5/src/main/java/org/apache/commons/beanutils/DynaBeanMapDecorator.java

Author: oheger
Date: Sun Oct 27 09:59:03 2013
New Revision: 1536076

URL: http://svn.apache.org/r1536076
Log:
DynaBeanMapDecorator now extends the new abstract decorator base class.

It still uses Object as key type and thus will be of limited use.

Modified:
    commons/proper/beanutils/branches/java5/src/main/java/org/apache/commons/beanutils/DynaBeanMapDecorator.java

Modified: commons/proper/beanutils/branches/java5/src/main/java/org/apache/commons/beanutils/DynaBeanMapDecorator.java
URL: http://svn.apache.org/viewvc/commons/proper/beanutils/branches/java5/src/main/java/org/apache/commons/beanutils/DynaBeanMapDecorator.java?rev=1536076&r1=1536075&r2=1536076&view=diff
==============================================================================
--- commons/proper/beanutils/branches/java5/src/main/java/org/apache/commons/beanutils/DynaBeanMapDecorator.java (original)
+++ commons/proper/beanutils/branches/java5/src/main/java/org/apache/commons/beanutils/DynaBeanMapDecorator.java Sun Oct 27 09:59:03 2013
@@ -16,13 +16,6 @@
  */
 package org.apache.commons.beanutils;
 
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
 
 /**
  * <p>Decorates a {@link DynaBean} to provide <code>Map</code> behaviour.</p>
@@ -72,325 +65,32 @@ import java.util.Set;
  * @since BeanUtils 1.8.0
  * @version $Id$
  */
-public class DynaBeanMapDecorator implements Map<Object, Object> {
-
-    private final DynaBean dynaBean;
-    private final boolean readOnly;
-    private transient Set<Object> keySet;
-
-    // ------------------- Constructors ----------------------------------
-
-    /**
-     * Constructs a  read only Map for the specified
-     * {@link DynaBean}.
-     *
-     * @param dynaBean The dyna bean being decorated
-     * @throws IllegalArgumentException if the {@link DynaBean} is null.
-     */
-    public DynaBeanMapDecorator(DynaBean dynaBean) {
-        this(dynaBean, true);
-    }
-
+public class DynaBeanMapDecorator extends BaseDynaBeanMapDecorator<Object> {
     /**
      * Construct a Map for the specified {@link DynaBean}.
      *
      * @param dynaBean The dyna bean being decorated
-     * @param readOnly <code>true</code> if the Mpa is read only
+     * @param readOnly <code>true</code> if the Map is read only
      * otherwise <code>false</code>
      * @throws IllegalArgumentException if the {@link DynaBean} is null.
      */
     public DynaBeanMapDecorator(DynaBean dynaBean, boolean readOnly) {
-        if (dynaBean == null) {
-            throw new IllegalArgumentException("DynaBean is null");
-        }
-        this.dynaBean = dynaBean;
-        this.readOnly = readOnly;
-    }
-
-
-    // ------------------- public Methods --------------------------------
-
-
-    /**
-     * Indicate whether the Map is read only.
-     *
-     * @return <code>true</code> if the Map is read only,
-     * otherwise <code>false</code>.
-     */
-    public boolean isReadOnly() {
-        return readOnly;
-    }
-
-    // ------------------- java.util.Map Methods -------------------------
-
-    /**
-     * clear() operation is not supported.
-     *
-     * @throws UnsupportedOperationException
-     */
-    public void clear() {
-        throw new UnsupportedOperationException();
-    }
-
-    /**
-     * Indicate whether the {@link DynaBean} contains a specified
-     * value for one (or more) of its properties.
-     *
-     * @param key The {@link DynaBean}'s property name
-     * @return <code>true</code> if one of the {@link DynaBean}'s
-     * properties contains a specified value.
-     */
-    public boolean containsKey(Object key) {
-        DynaClass dynaClass = getDynaBean().getDynaClass();
-        DynaProperty dynaProperty = dynaClass.getDynaProperty(toString(key));
-        return (dynaProperty == null ? false : true);
-    }
-
-    /**
-     * Indicates whether the decorated {@link DynaBean} contains
-     * a specified value.
-     *
-     * @param value The value to check for.
-     * @return <code>true</code> if one of the the {@link DynaBean}'s
-     * properties contains the specified value, otherwise
-     * <code>false</code>.
-     */
-    public boolean containsValue(Object value) {
-        DynaProperty[] properties = getDynaProperties();
-        for (int i = 0; i < properties.length; i++) {
-            String key = properties[i].getName();
-            Object prop = getDynaBean().get(key);
-            if (value == null) {
-                if (prop == null) {
-                    return true;
-                }
-            } else {
-                if (value.equals(prop)) {
-                    return true;
-                }
-            }
-        }
-        return false;
-    }
-
-    /**
-     * <p>Returns the Set of the property/value mappings
-     * in the decorated {@link DynaBean}.</p>
-     *
-     * <p>Each element in the Set is a <code>Map.Entry</code>
-     * type.</p>
-     *
-     * @return An unmodifiable set of the DynaBean
-     * property name/value pairs
-     */
-    public Set<Map.Entry<Object, Object>> entrySet() {
-        DynaProperty[] properties = getDynaProperties();
-        Set<Map.Entry<Object, Object>> set = new HashSet<Map.Entry<Object, Object>>(properties.length);
-        for (int i = 0; i < properties.length; i++) {
-            String key = properties[i].getName();
-            Object value = getDynaBean().get(key);
-            set.add(new MapEntry(key, value));
-        }
-        return Collections.unmodifiableSet(set);
-    }
-
-    /**
-     * Return the value for the specified key from
-     * the decorated {@link DynaBean}.
-     *
-     * @param key The {@link DynaBean}'s property name
-     * @return The value for the specified property.
-     */
-    public Object get(Object key) {
-        return getDynaBean().get(toString(key));
-    }
-
-    /**
-     * Indicate whether the decorated {@link DynaBean} has
-     * any properties.
-     *
-     * @return <code>true</code> if the {@link DynaBean} has
-     * no properties, otherwise <code>false</code>.
-     */
-    public boolean isEmpty() {
-        return (getDynaProperties().length == 0);
+        super(dynaBean, readOnly);
     }
 
     /**
-     * <p>Returns the Set of the property
-     * names in the decorated {@link DynaBean}.</p>
-     *
-     * <p><b>N.B.</b>For {@link DynaBean}s whose associated {@link DynaClass}
-     * is a {@link MutableDynaClass} a new Set is created every
-     * time, otherwise the Set is created only once and cached.</p>
-     *
-     * @return An unmodifiable set of the {@link DynaBean}s
-     * property names.
-     */
-    public Set<Object> keySet() {
-        if (keySet != null) {
-            return keySet;
-        }
-
-        // Create a Set of the keys
-        DynaProperty[] properties = getDynaProperties();
-        Set<Object> set = new HashSet<Object>(properties.length);
-        for (int i = 0; i < properties.length; i++) {
-            set.add(properties[i].getName());
-        }
-        set = Collections.unmodifiableSet(set);
-
-        // Cache the keySet if Not a MutableDynaClass
-        DynaClass dynaClass = getDynaBean().getDynaClass();
-        if (!(dynaClass instanceof MutableDynaClass)) {
-            keySet = set;
-        }
-
-        return set;
-
-    }
-
-    /**
-     * Set the value for the specified property in
-     * the decorated {@link DynaBean}.
-     *
-     * @param key The {@link DynaBean}'s property name
-     * @param value The value for the specified property.
-     * @return The previous property's value.
-     * @throws UnsupportedOperationException if
-     * <code>isReadOnly()</code> is true.
-     */
-    public Object put(Object key, Object value) {
-        if (isReadOnly()) {
-            throw new UnsupportedOperationException("Map is read only");
-        }
-        String property = toString(key);
-        Object previous = getDynaBean().get(property);
-        getDynaBean().set(property, value);
-        return previous;
-    }
-
-    /**
-     * Copy the contents of a Map to the decorated {@link DynaBean}.
-     *
-     * @param map The Map of values to copy.
-     * @throws UnsupportedOperationException if
-     * <code>isReadOnly()</code> is true.
-     */
-    public void putAll(Map<? extends Object, ? extends Object> map) {
-        if (isReadOnly()) {
-            throw new UnsupportedOperationException("Map is read only");
-        }
-        for (Map.Entry<?, ?> e : map.entrySet()) {
-            put(e.getKey(), e.getValue());
-        }
-    }
-
-    /**
-     * remove() operation is not supported.
-     *
-     * @param key The {@link DynaBean}'s property name
-     * @return the value removed
-     * @throws UnsupportedOperationException
-     */
-    public Object remove(Object key) {
-        throw new UnsupportedOperationException();
-    }
-
-    /**
-     * Returns the number properties in the decorated
+     * Constructs a read only Map for the specified
      * {@link DynaBean}.
-     * @return The number of properties.
-     */
-    public int size() {
-        return getDynaProperties().length;
-    }
-
-    /**
-     * Returns the set of property values in the
-     * decorated {@link DynaBean}.
-     *
-     * @return Unmodifiable collection of values.
-     */
-    public Collection<Object> values() {
-        DynaProperty[] properties = getDynaProperties();
-        List<Object> values = new ArrayList<Object>(properties.length);
-        for (int i = 0; i < properties.length; i++) {
-            String key = properties[i].getName();
-            Object value = getDynaBean().get(key);
-            values.add(value);
-        }
-        return Collections.unmodifiableList(values);
-    }
-
-    // ------------------- protected Methods -----------------------------
-
-    /**
-     * Provide access to the underlying {@link DynaBean}
-     * this Map decorates.
-     *
-     * @return the decorated {@link DynaBean}.
-     */
-    public DynaBean getDynaBean() {
-        return dynaBean;
-    }
-
-    // ------------------- private Methods -------------------------------
-
-    /**
-     * Convenience method to retrieve the {@link DynaProperty}s
-     * for this {@link DynaClass}.
-     *
-     * @return The an array of the {@link DynaProperty}s.
-     */
-    private DynaProperty[] getDynaProperties() {
-        return getDynaBean().getDynaClass().getDynaProperties();
-    }
-
-    /**
-     * Convenience method to convert an Object
-     * to a String.
      *
-     * @param obj The Object to convert
-     * @return String representation of the object
+     * @param dynaBean The dyna bean being decorated
+     * @throws IllegalArgumentException if the {@link DynaBean} is null.
      */
-    private String toString(Object obj) {
-        return (obj == null ? null : obj.toString());
+    public DynaBeanMapDecorator(DynaBean dynaBean) {
+        super(dynaBean);
     }
 
-    /**
-     * Map.Entry implementation.
-     */
-    private static class MapEntry implements Map.Entry<Object, Object> {
-        private final Object key;
-        private final Object value;
-        MapEntry(Object key, Object value) {
-            this.key = key;
-            this.value = value;
-        }
-        @Override
-        public boolean equals(Object o) {
-            if (!(o instanceof Map.Entry)) {
-                return false;
-            }
-            Map.Entry<?, ?> e = (Map.Entry<?, ?>)o;
-            return ((key.equals(e.getKey())) &&
-                    (value == null ? e.getValue() == null
-                                   : value.equals(e.getValue())));
-        }
-        @Override
-        public int hashCode() {
-            return key.hashCode() + (value == null ? 0 : value.hashCode());
-        }
-        public Object getKey() {
-            return key;
-        }
-        public Object getValue() {
-            return value;
-        }
-        public Object setValue(Object value) {
-            throw new UnsupportedOperationException();
-        }
+    @Override
+    protected Object convertKey(String propertyName) {
+        return propertyName;
     }
-
 }