You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by cz...@apache.org on 2008/08/24 07:00:23 UTC

svn commit: r688448 - /incubator/sling/trunk/extensions/event/src/main/java/org/apache/sling/event/EventPropertiesMap.java

Author: cziegeler
Date: Sat Aug 23 22:00:22 2008
New Revision: 688448

URL: http://svn.apache.org/viewvc?rev=688448&view=rev
Log:
Improve wrapper implementation

Modified:
    incubator/sling/trunk/extensions/event/src/main/java/org/apache/sling/event/EventPropertiesMap.java

Modified: incubator/sling/trunk/extensions/event/src/main/java/org/apache/sling/event/EventPropertiesMap.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/extensions/event/src/main/java/org/apache/sling/event/EventPropertiesMap.java?rev=688448&r1=688447&r2=688448&view=diff
==============================================================================
--- incubator/sling/trunk/extensions/event/src/main/java/org/apache/sling/event/EventPropertiesMap.java (original)
+++ incubator/sling/trunk/extensions/event/src/main/java/org/apache/sling/event/EventPropertiesMap.java Sat Aug 23 22:00:22 2008
@@ -18,8 +18,10 @@
  */
 package org.apache.sling.event;
 
+import java.util.Arrays;
 import java.util.Collection;
-import java.util.HashMap;
+import java.util.Collections;
+import java.util.HashSet;
 import java.util.Map;
 import java.util.Set;
 
@@ -30,82 +32,184 @@
  */
 public class EventPropertiesMap implements Map<String, Object> {
 
-    private final Map<String, Object> delegatee = new HashMap<String, Object>();
+    private final boolean isEmpty;
+
+    private final Event event;
 
     public EventPropertiesMap(final Event event) {
-        if ( event.getPropertyNames() != null ) {
-            final String[] names = event.getPropertyNames();
-            for(final String key : names) {
-                this.delegatee.put(key, event.getProperty(key));
-            }
-        }
+        this.event = event;
+        this.isEmpty = (event.getPropertyNames() == null || event.getPropertyNames().length == 0);
     }
 
     /**
      * @see java.util.Map#clear()
      */
     public void clear() {
-        throw new UnsupportedOperationException("Clear is not supported.");
+        throw new UnsupportedOperationException("clear is not supported.");
     }
 
     /**
      * @see java.util.Map#put(java.lang.Object, java.lang.Object)
      */
     public Object put(String key, Object value) {
-        throw new UnsupportedOperationException("Clear is not supported.");
+        throw new UnsupportedOperationException("put is not supported.");
     }
 
     /**
      * @see java.util.Map#putAll(java.util.Map)
      */
     public void putAll(Map<? extends String, ? extends Object> t) {
-        throw new UnsupportedOperationException("Clear is not supported.");
+        throw new UnsupportedOperationException("putAll is not supported.");
     }
 
     /**
      * @see java.util.Map#remove(java.lang.Object)
      */
     public Object remove(Object key) {
-        throw new UnsupportedOperationException("Clear is not supported.");
+        throw new UnsupportedOperationException("remove is not supported.");
     }
 
+    /**
+     * @see java.util.Map#containsKey(java.lang.Object)
+     */
     public boolean containsKey(Object key) {
-        return delegatee.containsKey(key);
+        if ( this.isEmpty || key == null ) {
+            return false;
+        }
+        final String[] names = this.event.getPropertyNames();
+        for(final String name : names) {
+            if ( name.equals(key) ) {
+                return true;
+            }
+        }
+        return false;
     }
 
+    /**
+     * @see java.util.Map#containsValue(java.lang.Object)
+     */
     public boolean containsValue(Object value) {
-        return delegatee.containsValue(value);
+        if ( this.isEmpty ) {
+            return false;
+        }
+        final String[] names = this.event.getPropertyNames();
+        for(final String name : names) {
+            if ( this.event.getProperty(name).equals(value) ) {
+                return true;
+            }
+        }
+        return false;
     }
 
-    public Set<java.util.Map.Entry<String, Object>> entrySet() {
-        return delegatee.entrySet();
+    /**
+     * @see java.util.Map#entrySet()
+     */
+    public Set<Map.Entry<String, Object>> entrySet() {
+        if ( this.isEmpty ) {
+            return Collections.emptySet();
+        }
+        final Set<Map.Entry<String, Object>> set = new HashSet<Map.Entry<String,Object>>();
+        final String[] names = event.getPropertyNames();
+        for(final String key : names) {
+            set.add(new PropertyEntry(key, this.event.getProperty(key)));
+        }
+        return set;
     }
 
+    /**
+     * @see java.lang.Object#equals(java.lang.Object)
+     */
     public boolean equals(Object o) {
-        return delegatee.equals(o);
+        if ( o instanceof EventPropertiesMap ) {
+            return this.event.equals(((EventPropertiesMap)o).event);
+        }
+        return false;
     }
 
+    /**
+     * @see java.util.Map#get(java.lang.Object)
+     */
     public Object get(Object key) {
-        return delegatee.get(key);
+        return (this.isEmpty || key == null ? null : this.event.getProperty(key.toString()));
     }
 
+    /**
+     * @see java.lang.Object#hashCode()
+     */
     public int hashCode() {
-        return delegatee.hashCode();
+        return this.event.hashCode();
     }
 
+    /**
+     * @see java.util.Map#isEmpty()
+     */
     public boolean isEmpty() {
-        return delegatee.isEmpty();
+        return this.isEmpty;
     }
 
+    /**
+     * @see java.util.Map#keySet()
+     */
     public Set<String> keySet() {
-        return delegatee.keySet();
+        if ( this.isEmpty ) {
+            return Collections.emptySet();
+        }
+        final Set<String> set = new HashSet<String>();
+        set.addAll(Arrays.asList(this.event.getPropertyNames()));
+        return set;
     }
 
+    /**
+     * @see java.util.Map#size()
+     */
     public int size() {
-        return delegatee.size();
+        return (this.isEmpty ? 0 : this.event.getPropertyNames().length);
     }
 
+    /**
+     * @see java.util.Map#values()
+     */
     public Collection<Object> values() {
-        return delegatee.values();
+        if ( this.isEmpty ) {
+            return Collections.emptySet();
+        }
+        final Set<Object> set = new HashSet<Object>();
+        final String[] names = this.event.getPropertyNames();
+        for(final String name : names) {
+            set.add(this.event.getProperty(name));
+        }
+        return set;
+    }
+
+    protected static final class PropertyEntry implements Map.Entry<String, Object> {
+
+        private final String key;
+        private final Object value;
+
+        public PropertyEntry(final String key, final Object value) {
+            this.key = key;
+            this.value = value;
+        }
+
+        /**
+         * @see java.util.Map.Entry#getKey()
+         */
+        public String getKey() {
+            return this.key;
+        }
+
+        /**
+         * @see java.util.Map.Entry#getValue()
+         */
+        public Object getValue() {
+            return this.value;
+        }
+
+        /**
+         * @see java.util.Map.Entry#setValue(java.lang.Object)
+         */
+        public Object setValue(Object value) {
+            throw new UnsupportedOperationException("setValue is not supported.");
+        }
     }
 }