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

[sling-org-apache-sling-api] 01/01: SLING-8116 implement default methods

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

jeb pushed a commit to branch SLING-8116
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-api.git

commit 34d2c211571090eb421ce8bd905deb608b7de7fb
Author: JE Bailey <je...@apache.org>
AuthorDate: Fri Nov 16 13:51:08 2018 -0500

    SLING-8116 implement default methods
---
 pom.xml                                            | 14 ++++++++++-
 .../org/apache/sling/api/resource/ValueMap.java    | 28 ++++++++++++++++++----
 .../apache/sling/api/resource/package-info.java    |  2 +-
 3 files changed, 38 insertions(+), 6 deletions(-)

diff --git a/pom.xml b/pom.xml
index 4637d0c..0aa7d14 100644
--- a/pom.xml
+++ b/pom.xml
@@ -52,7 +52,7 @@
 
     <properties>
         <site.jira.version.id>12314252</site.jira.version.id>
-        <sling.java.version>7</sling.java.version>
+        <sling.java.version>8</sling.java.version>
         <jackrabbit.version>2.13.4</jackrabbit.version>
     </properties>
 
@@ -102,6 +102,18 @@
             <version>3.2</version>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>org.osgi</groupId>
+            <artifactId>org.osgi.util.converter</artifactId>
+            <version>1.0.0</version>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.osgi</groupId>
+            <artifactId>org.osgi.util.function</artifactId>
+            <version>1.0.0</version>
+            <scope>provided</scope>
+        </dependency>
     </dependencies>
 
     <build>
diff --git a/src/main/java/org/apache/sling/api/resource/ValueMap.java b/src/main/java/org/apache/sling/api/resource/ValueMap.java
index 17bbaa3..3645b2f 100644
--- a/src/main/java/org/apache/sling/api/resource/ValueMap.java
+++ b/src/main/java/org/apache/sling/api/resource/ValueMap.java
@@ -25,8 +25,9 @@ import org.jetbrains.annotations.Nullable;
 import org.jetbrains.annotations.NotNull;
 
 import org.apache.sling.api.wrappers.ValueMapDecorator;
-
+import org.apache.sling.api.wrappers.impl.ObjectConverter;
 import org.osgi.annotation.versioning.ConsumerType;
+import org.osgi.util.converter.Converters;
 
 /**
  * The <code>ValueMap</code> is an easy way to access properties of a resource.
@@ -65,7 +66,18 @@ public interface ValueMap extends Map<String, Object> {
      * @return Return named value converted to type T or <code>null</code> if
      *         non existing or can't be converted.
      */
-    @Nullable <T> T get(@NotNull String name, @NotNull Class<T> type);
+    @SuppressWarnings("unchecked")
+    @Nullable
+    default <T> T get(@NotNull String name, @NotNull Class<T> type) {
+        Object value = get(name);
+        if (value == null) {
+            return (T)null;
+        }
+        if (type.isAssignableFrom(value.getClass())) {
+            return (T)value;
+        }
+        return Converters.standardConverter().convert(value).to(type);
+    }
 
     /**
      * Get a named property and convert it into the given type.
@@ -87,5 +99,13 @@ public interface ValueMap extends Map<String, Object> {
      * @return Return named value converted to type T or the default value if
      *         non existing or can't be converted.
      */
-    @NotNull <T> T get(@NotNull String name, @NotNull T defaultValue);
-}
+    @SuppressWarnings("unchecked")
+    @NotNull
+    default <T> T get(@NotNull String name, @NotNull T defaultValue) {
+        T value = (T)get(name, defaultValue.getClass());
+        if (value == null) {
+            return (T)defaultValue;
+        }
+        return value;
+    }
+ }
diff --git a/src/main/java/org/apache/sling/api/resource/package-info.java b/src/main/java/org/apache/sling/api/resource/package-info.java
index a6c739d..a607e45 100644
--- a/src/main/java/org/apache/sling/api/resource/package-info.java
+++ b/src/main/java/org/apache/sling/api/resource/package-info.java
@@ -17,7 +17,7 @@
  * under the License.
  */
 
-@Version("2.11.1")
+@Version("2.12.0")
 package org.apache.sling.api.resource;
 
 import org.osgi.annotation.versioning.Version;