You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2017/11/07 09:58:46 UTC

[sling-org-apache-sling-resourcemerger] 06/24: SLING-2986 : Fix value map and property type handling

This is an automated email from the ASF dual-hosted git repository.

rombert pushed a commit to annotated tag org.apache.sling.resourcemerger-1.0.0
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-resourcemerger.git

commit a5af9099ddb7d4bf02aadcedaef8112bb6850afd
Author: Carsten Ziegeler <cz...@apache.org>
AuthorDate: Wed Jan 29 15:28:12 2014 +0000

    SLING-2986 : Fix value map and property type handling
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/contrib/extensions/resourcemerger@1562481 13f79535-47bb-0310-9956-ffa450edef68
---
 .../sling/resourcemerger/impl/MergedValueMap.java  | 137 +++++----------------
 1 file changed, 33 insertions(+), 104 deletions(-)

diff --git a/src/main/java/org/apache/sling/resourcemerger/impl/MergedValueMap.java b/src/main/java/org/apache/sling/resourcemerger/impl/MergedValueMap.java
index a9606fe..3a888fa 100644
--- a/src/main/java/org/apache/sling/resourcemerger/impl/MergedValueMap.java
+++ b/src/main/java/org/apache/sling/resourcemerger/impl/MergedValueMap.java
@@ -18,21 +18,20 @@
  */
 package org.apache.sling.resourcemerger.impl;
 
-import java.util.Collection;
-import java.util.Collections;
+import java.util.HashMap;
 import java.util.HashSet;
-import java.util.LinkedHashMap;
-import java.util.Map;
 import java.util.Set;
 
+import org.apache.sling.api.resource.Resource;
 import org.apache.sling.api.resource.ResourceUtil;
 import org.apache.sling.api.resource.ValueMap;
+import org.apache.sling.api.wrappers.ValueMapDecorator;
 
 /**
  * A <code>MergedValueMap</code> is a {@link ValueMap} aggregated from the
  * different resources mapped to a {@link MergedResource}.
  */
-public class MergedValueMap implements ValueMap {
+public class MergedValueMap extends ValueMapDecorator {
 
     /**
      * Set of properties to exclude from override
@@ -47,120 +46,50 @@ public class MergedValueMap implements ValueMap {
     }
 
     /**
-     * Final properties
-     */
-    private Map<String, Object> properties = new LinkedHashMap<String, Object>();
-
-    /**
      * Constructor
      *
      * @param resource The merged resource to get properties from
      */
-    public MergedValueMap(MergedResource resource) {
+    public MergedValueMap(final MergedResource resource) {
+        super(new HashMap<String, Object>());
         // Iterate over physical resources
-        for (String r : resource.getMappedResources()) {
-            ValueMap vm = ResourceUtil.getValueMap(resource.getResourceResolver().getResource(r));
-            if (properties.isEmpty()) {
-                // Add all properties
-                properties.putAll(vm);
-            } else {
-                // Get properties to add or override
-                for (String key : vm.keySet()) {
-                    if (!isExcludedProperty(key)) {
-                        properties.put(key, vm.get(key));
+        for (final String r : resource.getMappedResources()) {
+            final Resource rsrc = resource.getResourceResolver().getResource(r);
+            if ( rsrc != null ) {
+                final ValueMap vm = ResourceUtil.getValueMap(rsrc);
+                if (this.isEmpty()) {
+                    // Add all properties
+                    this.putAll(vm);
+                } else {
+                    // Get properties to add or override
+                    for (final String key : vm.keySet()) {
+                        if (!isExcludedProperty(key)) {
+                            this.put(key, vm.get(key));
+                        }
                     }
-                }
 
-                // Get properties to hide
-                String[] propertiesToHide = vm.get(MergedResourceConstants.PN_HIDE_PROPERTIES, new String[0]);
-                if (propertiesToHide.length == 0) {
-                    String propertyToHide = vm.get(MergedResourceConstants.PN_HIDE_PROPERTIES, String.class);
-                    if (propertyToHide != null) {
-                        propertiesToHide = new String[]{propertyToHide};
-                    }
-                }
-                for (String propName : propertiesToHide) {
-                    if (propName.equals("*")) {
-                        properties.clear();
-                        break;
-                    } else {
-                        properties.remove(propName);
+                    // Get properties to hide
+                    final String[] propertiesToHide = vm.get(MergedResourceConstants.PN_HIDE_PROPERTIES, String[].class);
+                    if ( propertiesToHide != null ) {
+                        for (final String propName : propertiesToHide) {
+                            if (propName.equals("*")) {
+                                this.clear();
+                                break;
+                            } else {
+                                this.remove(propName);
+                            }
+                        }
                     }
                 }
             }
-
-            // Hide excluded properties
-            for (String excludedProperty : EXCLUDED_PROPERTIES) {
-                properties.remove(excludedProperty);
-            }
-        }
-    }
-
-    @SuppressWarnings("unchecked")
-    public <T> T get(String name, Class<T> type) {
-        return (T) properties.get(name);
-    }
-
-    @SuppressWarnings("unchecked")
-    public <T> T get(String name, T defaultValue) {
-        Object o = properties.get(name);
-        return o == null ? defaultValue : (T) o;
-    }
-
-    public int size() {
-        return properties != null ? properties.size() : 0;
-    }
-
-    public boolean isEmpty() {
-        return properties == null || properties.isEmpty();
-    }
-
-    public boolean containsKey(Object o) {
-        return properties != null && properties.containsKey(o);
-    }
-
-    public boolean containsValue(Object o) {
-        return properties != null && properties.containsValue(o);
-    }
-
-    public Object get(Object o) {
-        return properties != null ? properties.get(o) : null;
-    }
-
-    public Object put(String s, Object o) {
-        return properties != null ? properties.put(s, o) : null;
-    }
-
-    public Object remove(Object o) {
-        return properties != null ? properties.remove(o) : null;
-    }
-
-    public void putAll(Map<? extends String, ?> map) {
-        if (properties != null) {
-            properties.putAll(map);
         }
-    }
-
-    public void clear() {
-        if (properties != null) {
-            properties.clear();
+        // Hide excluded properties
+        for (final String excludedProperty : EXCLUDED_PROPERTIES) {
+            this.remove(excludedProperty);
         }
     }
 
-    public Set<String> keySet() {
-        return properties != null ? properties.keySet() : Collections.<String>emptySet();
-    }
-
-    public Collection<Object> values() {
-        return properties != null ? properties.values() : Collections.emptyList();
-    }
-
-    public Set<Entry<String, Object>> entrySet() {
-        return properties != null ? properties.entrySet() : Collections.<Entry<String, Object>>emptySet();
-    }
-
     private boolean isExcludedProperty(String key) {
         return EXCLUDED_PROPERTIES.contains(key);
     }
-
 }

-- 
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.