incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cziege...@apache.org
Subject svn commit: r675889 - in /incubator/sling/trunk/api/src/main/java/org/apache/sling/api: resource/ValueMap.java wrappers/ValueMapDecorator.java
Date Fri, 11 Jul 2008 09:36:24 GMT
Author: cziegeler
Date: Fri Jul 11 02:36:23 2008
New Revision: 675889

URL: http://svn.apache.org/viewvc?rev=675889&view=rev
Log:
SLING-569 - Apply slightly modifed version of patch submitted by Tobias Bocanegra.

Added:
    incubator/sling/trunk/api/src/main/java/org/apache/sling/api/wrappers/ValueMapDecorator.java
  (with props)
Modified:
    incubator/sling/trunk/api/src/main/java/org/apache/sling/api/resource/ValueMap.java

Modified: incubator/sling/trunk/api/src/main/java/org/apache/sling/api/resource/ValueMap.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/api/src/main/java/org/apache/sling/api/resource/ValueMap.java?rev=675889&r1=675888&r2=675889&view=diff
==============================================================================
--- incubator/sling/trunk/api/src/main/java/org/apache/sling/api/resource/ValueMap.java (original)
+++ incubator/sling/trunk/api/src/main/java/org/apache/sling/api/resource/ValueMap.java Fri
Jul 11 02:36:23 2008
@@ -19,8 +19,16 @@
 package org.apache.sling.api.resource;
 
 import java.util.Map;
+import java.util.Collections;
+
+import org.apache.sling.api.wrappers.ValueMapDecorator;
 
 public interface ValueMap extends Map<String, Object> {
+
+    /**
+     * Empty value map
+     */
+    final ValueMap EMPTY = new ValueMapDecorator(Collections.<String, Object>emptyMap());
     
     // return named value converted to type T or
     // null if not existing

Added: incubator/sling/trunk/api/src/main/java/org/apache/sling/api/wrappers/ValueMapDecorator.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/api/src/main/java/org/apache/sling/api/wrappers/ValueMapDecorator.java?rev=675889&view=auto
==============================================================================
--- incubator/sling/trunk/api/src/main/java/org/apache/sling/api/wrappers/ValueMapDecorator.java
(added)
+++ incubator/sling/trunk/api/src/main/java/org/apache/sling/api/wrappers/ValueMapDecorator.java
Fri Jul 11 02:36:23 2008
@@ -0,0 +1,169 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.api.wrappers;
+
+import java.util.Collection;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.sling.api.resource.ValueMap;
+
+/**
+ * <code>ValueMapDecorator</code> decorates another {@link Map}
+ * to provide a basic implementation for the additional methods
+ * of a {@link ValueMap}.
+ */
+public class ValueMapDecorator implements ValueMap {
+
+    /**
+     * underlying map
+     */
+    private final Map<String, Object> base;
+
+    /**
+     * Creates a new wrapper around a given map.
+     * @param base wrapped object
+     */
+    public ValueMapDecorator(Map<String, Object> base) {
+        this.base = base;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @SuppressWarnings("unchecked")
+    public <T> T get(String name, Class<T> type) {
+        try {
+            // todo: do smarter checks
+            Object obj = get(name);
+            if (obj == null) {
+                return null;
+            } else if (type.isAssignableFrom(obj.getClass())) {
+                return (T) obj;
+            } else if (type == String.class) {
+                return (T) String.valueOf(obj);
+            } else if (type == Integer.class) {
+                return (T) (Integer) Integer.parseInt(obj.toString());
+            } else if (type == Long.class) {
+                return (T) (Long) Long.parseLong(obj.toString());
+            } else if (type == Double.class) {
+                return (T) (Double) Double.parseDouble(obj.toString());
+            } else if (type == Boolean.class) {
+                return (T) (Boolean) Boolean.parseBoolean(obj.toString());
+            } else {
+                return null;
+            }
+        } catch (NumberFormatException e) {
+            return null;
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @SuppressWarnings("unchecked")
+    public <T> T get(String name, T defaultValue) {
+        T value = get(name, (Class<T>) defaultValue.getClass());
+        return value == null ? defaultValue : value;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public int size() {
+        return base.size();
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public boolean isEmpty() {
+        return base.isEmpty();
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public boolean containsKey(Object key) {
+        return base.containsKey(key);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public boolean containsValue(Object value) {
+        return base.containsValue(value);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public Object get(Object key) {
+        return base.get(key);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public Object put(String key, Object value) {
+        return base.put(key, value);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public Object remove(Object key) {
+        return base.remove(key);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public void putAll(Map<? extends String, ?> t) {
+        base.putAll(t);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public void clear() {
+        base.clear();
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public Set<String> keySet() {
+        return base.keySet();
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public Collection<Object> values() {
+        return base.values();
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public Set<Entry<String, Object>> entrySet() {
+        return base.entrySet();
+    }
+}
\ No newline at end of file

Propchange: incubator/sling/trunk/api/src/main/java/org/apache/sling/api/wrappers/ValueMapDecorator.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/sling/trunk/api/src/main/java/org/apache/sling/api/wrappers/ValueMapDecorator.java
------------------------------------------------------------------------------
    svn:keywords = author date id revision rev url

Propchange: incubator/sling/trunk/api/src/main/java/org/apache/sling/api/wrappers/ValueMapDecorator.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message