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 2018/02/24 22:34:00 UTC

[isis] 01/07: ISIS-1880 Internal API: allow _Context to override already presnet singletons

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

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

commit 91f3d779e8fc40209ba387341c21a8e6f564f09a
Author: Andi Huber <ah...@apache.org>
AuthorDate: Sat Feb 24 23:21:45 2018 +0100

    ISIS-1880 Internal API: allow _Context to override already presnet
    singletons
---
 .../isis/applib/internal/context/_Context.java     | 24 ++++++++++++++++++++++
 1 file changed, 24 insertions(+)

diff --git a/core/applib/src/main/java/org/apache/isis/applib/internal/context/_Context.java b/core/applib/src/main/java/org/apache/isis/applib/internal/context/_Context.java
index 589805a..4e7fcfb 100644
--- a/core/applib/src/main/java/org/apache/isis/applib/internal/context/_Context.java
+++ b/core/applib/src/main/java/org/apache/isis/applib/internal/context/_Context.java
@@ -72,6 +72,30 @@ public final class _Context {
 			singletonMap.put(toKey(type), singleton);	
 		}
 	}
+	
+	/**
+	 * Puts a singleton instance onto the current context, that 
+	 * either overrides any already present or ignores the call depending on {@code override}.
+	 * @param type non-null
+	 * @param singleton non-null
+	 * @param override whether to overrides any already present singleton or not
+	 * @return whether the {@code singleton} was put on the context or ignored because there is already one present 
+	 */
+	public static boolean put(Class<?> type, Object singleton, boolean override) {
+		Objects.requireNonNull(type);
+		Objects.requireNonNull(singleton);
+		
+		// let writes to the map be atomic
+		synchronized (singletonMap) {   
+			if(singletonMap.containsKey(toKey(type))) {
+				if(!override)
+					return false;
+			}
+			singletonMap.put(toKey(type), singleton);
+			return true;
+		}
+	}
+	
 
 	/**
 	 * Gets a singleton instance of {@code type} if there is any, null otherwise.

-- 
To stop receiving notification emails like this one, please contact
ahuber@apache.org.