You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@isis.apache.org by ah...@apache.org on 2019/01/17 07:28:50 UTC

[isis] branch v2 updated: ISIS-2088: extending internal API: new variant _Context#computeIfAbsent

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

ahuber pushed a commit to branch v2
in repository https://gitbox.apache.org/repos/asf/isis.git


The following commit(s) were added to refs/heads/v2 by this push:
     new da0e81b  ISIS-2088: extending internal API: new variant _Context#computeIfAbsent
da0e81b is described below

commit da0e81b0189400a3dd80b1f1d5cfab1641a2946f
Author: Andi Huber <ah...@apache.org>
AuthorDate: Thu Jan 17 08:28:38 2019 +0100

    ISIS-2088: extending internal API: new variant _Context#computeIfAbsent
    
    Task-Url: https://issues.apache.org/jira/browse/ISIS-2088
---
 .../apache/isis/commons/internal/context/_Context.java    | 15 +++++++++++++++
 .../org/apache/isis/commons/internal/context/_Plugin.java |  2 +-
 .../apache/isis/commons/internal/context/ContextTest.java |  4 ++--
 .../java/org/apache/isis/config/AppConfigLocator.java     |  2 +-
 .../java/org/apache/isis/config/internal/_Config.java     |  2 +-
 .../isis/core/runtime/threadpool/ThreadPoolSupport.java   |  2 +-
 .../ObjectAdapterContext_ServiceLookup.java               |  2 +-
 .../widgets/themepicker/IsisWicketThemeSupport.java       |  2 +-
 8 files changed, 23 insertions(+), 8 deletions(-)

diff --git a/core/commons/src/main/java/org/apache/isis/commons/internal/context/_Context.java b/core/commons/src/main/java/org/apache/isis/commons/internal/context/_Context.java
index 6356fae..8a20dc1 100644
--- a/core/commons/src/main/java/org/apache/isis/commons/internal/context/_Context.java
+++ b/core/commons/src/main/java/org/apache/isis/commons/internal/context/_Context.java
@@ -141,6 +141,21 @@ public final class _Context {
             return t;
         }
     }
+    
+    /**
+     * If the specified key is not already associated with a value (or is mapped to null),
+     * attempts to compute its value using the given factory supplier and enters it into this map unless null.
+     * @param type
+     * @param factory
+     * @return null, if there is no such instance
+     */
+    public static <T> T computeIfAbsent(Class<? super T> type, Supplier<T> factory) {
+        requires(type, "type");
+        requires(factory, "factory");
+        return computeIfAbsent(type, __->factory.get());
+    }
+    
+    
 
     /**
      * Gets a singleton instance of {@code type} if there is any,
diff --git a/core/commons/src/main/java/org/apache/isis/commons/internal/context/_Plugin.java b/core/commons/src/main/java/org/apache/isis/commons/internal/context/_Plugin.java
index 278b7b4..31b34d3 100644
--- a/core/commons/src/main/java/org/apache/isis/commons/internal/context/_Plugin.java
+++ b/core/commons/src/main/java/org/apache/isis/commons/internal/context/_Plugin.java
@@ -89,7 +89,7 @@ public final class _Plugin {
     public static <S> S getOrElse(Class<S> pluginClass, Function<Set<S>, S> onAmbiguity, Supplier<S> onNotFound){
 
         // lookup cache first
-        return _Context.computeIfAbsent(pluginClass, __->{
+        return _Context.computeIfAbsent(pluginClass, ()->{
 
             final Set<S> plugins = loadAll(pluginClass);
 
diff --git a/core/commons/src/test/java/org/apache/isis/commons/internal/context/ContextTest.java b/core/commons/src/test/java/org/apache/isis/commons/internal/context/ContextTest.java
index 2668480..a72af53 100644
--- a/core/commons/src/test/java/org/apache/isis/commons/internal/context/ContextTest.java
+++ b/core/commons/src/test/java/org/apache/isis/commons/internal/context/ContextTest.java
@@ -142,14 +142,14 @@ class ContextTest {
         final AClass instance1 = new AClass();
         final AClass instance2 = new AClass();
         
-        _Context.computeIfAbsent(type, __->instance1);
+        _Context.computeIfAbsent(type, ()->instance1);
         
         {
             final Object actual = _Context.getIfAny(type);
             assertTrue(instance1==actual, "singleton on context is expected to be the same as the 'local' instance1");
         }
         
-        _Context.computeIfAbsent(type, __->instance2); // expected: this call does nothing
+        _Context.computeIfAbsent(type, ()->instance2); // expected: this call does nothing
         
         { 
             final Object actual = _Context.getIfAny(type);
diff --git a/core/config/src/main/java/org/apache/isis/config/AppConfigLocator.java b/core/config/src/main/java/org/apache/isis/config/AppConfigLocator.java
index 2376334..eb671cb 100644
--- a/core/config/src/main/java/org/apache/isis/config/AppConfigLocator.java
+++ b/core/config/src/main/java/org/apache/isis/config/AppConfigLocator.java
@@ -36,7 +36,7 @@ public final class AppConfigLocator {
     private AppConfigLocator() { }
     
     public static AppConfig getAppConfig() {
-        return _Context.computeIfAbsent(AppConfig.class, __->lookupAppConfig());
+        return _Context.computeIfAbsent(AppConfig.class, ()->lookupAppConfig());
     }
     
     // -- HELPER
diff --git a/core/config/src/main/java/org/apache/isis/config/internal/_Config.java b/core/config/src/main/java/org/apache/isis/config/internal/_Config.java
index 054766c..24a8118 100644
--- a/core/config/src/main/java/org/apache/isis/config/internal/_Config.java
+++ b/core/config/src/main/java/org/apache/isis/config/internal/_Config.java
@@ -113,7 +113,7 @@ public class _Config {
     
     private static _Config_LifecycleResource getLifecycleResource() {
         final _Config_LifecycleResource lifecycle = 
-                _Context.computeIfAbsent(_Config_LifecycleResource.class, __->createLifecycleResource());
+                _Context.computeIfAbsent(_Config_LifecycleResource.class, ()->createLifecycleResource());
         return lifecycle;
     }
 
diff --git a/core/metamodel/src/main/java/org/apache/isis/core/runtime/threadpool/ThreadPoolSupport.java b/core/metamodel/src/main/java/org/apache/isis/core/runtime/threadpool/ThreadPoolSupport.java
index 67ee062..2fdb175 100644
--- a/core/metamodel/src/main/java/org/apache/isis/core/runtime/threadpool/ThreadPoolSupport.java
+++ b/core/metamodel/src/main/java/org/apache/isis/core/runtime/threadpool/ThreadPoolSupport.java
@@ -76,7 +76,7 @@ public final class ThreadPoolSupport implements AutoCloseable {
      * @return the application-scoped singleton ThreadPoolSupport instance
      */
     public static ThreadPoolSupport getInstance() {
-        return _Context.computeIfAbsent(ThreadPoolSupport.class, __-> new ThreadPoolSupport());
+        return _Context.computeIfAbsent(ThreadPoolSupport.class, ThreadPoolSupport::new);
     }
     
     ThreadPoolSupport() {
diff --git a/core/runtime/src/main/java/org/apache/isis/core/runtime/system/persistence/adaptermanager/ObjectAdapterContext_ServiceLookup.java b/core/runtime/src/main/java/org/apache/isis/core/runtime/system/persistence/adaptermanager/ObjectAdapterContext_ServiceLookup.java
index 5da9e7e..60a0cef 100644
--- a/core/runtime/src/main/java/org/apache/isis/core/runtime/system/persistence/adaptermanager/ObjectAdapterContext_ServiceLookup.java
+++ b/core/runtime/src/main/java/org/apache/isis/core/runtime/system/persistence/adaptermanager/ObjectAdapterContext_ServiceLookup.java
@@ -56,7 +56,7 @@ class ObjectAdapterContext_ServiceLookup {
     ObjectAdapter lookupServiceAdapterFor(RootOid rootOid) {
         
         final ServicesByIdResource servicesByIdResource =
-                _Context.computeIfAbsent(ServicesByIdResource.class, cls->initLookupResource());
+                _Context.computeIfAbsent(ServicesByIdResource.class, this::initLookupResource);
         
         final Object serviceInstance = servicesByIdResource.lookupServiceInstance(rootOid);
         if(serviceInstance==null) {
diff --git a/core/viewer-wicket-ui/src/main/java/org/apache/isis/viewer/wicket/ui/components/widgets/themepicker/IsisWicketThemeSupport.java b/core/viewer-wicket-ui/src/main/java/org/apache/isis/viewer/wicket/ui/components/widgets/themepicker/IsisWicketThemeSupport.java
index b3734f7..251bd6e 100644
--- a/core/viewer-wicket-ui/src/main/java/org/apache/isis/viewer/wicket/ui/components/widgets/themepicker/IsisWicketThemeSupport.java
+++ b/core/viewer-wicket-ui/src/main/java/org/apache/isis/viewer/wicket/ui/components/widgets/themepicker/IsisWicketThemeSupport.java
@@ -54,7 +54,7 @@ public interface IsisWicketThemeSupport {
     // -- LOOKUP
     
     static IsisWicketThemeSupport getInstance() {
-        return _Context.computeIfAbsent(IsisWicketThemeSupport.class, __->createInstance());
+        return _Context.computeIfAbsent(IsisWicketThemeSupport.class, ()->createInstance());
     }
     
     // -- FACTORY