Fixed a possible race condition during Hudson start up.

git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@34294 71c3de6d-444a-0410-be80-ed276b4c234a
This commit is contained in:
kohsuke committed 2010-09-01 22:09:28 +00:00
1 parent 4abc357d33
commit 02fbfd9ed5
2 files changed
+22 -3

No files matched your search

+14 -2
View File
@@ -24,7 +24,6 @@
package hudson;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
@@ -33,7 +32,7 @@ import java.util.concurrent.ConcurrentHashMap;
* @author Kohsuke Kawaguchi
*/
public class Lookup {
private final Map<Class,Object> data = new ConcurrentHashMap<Class,Object>();
private final ConcurrentHashMap<Class,Object> data = new ConcurrentHashMap<Class,Object>();
public <T> T get(Class<T> type) {
return type.cast(data.get(type));
@@ -42,4 +41,17 @@ public class Lookup {
public <T> T set(Class<T> type, T instance) {
return type.cast(data.put(type,instance));
}
/**
* Overwrites the value only if the current value is null.
*
* @return
* If the value was null, return the {@code instance} value.
* Otherwise return the current value, which is non-null.
*/
public <T> T setIfNull(Class<T> type, T instance) {
Object o = data.putIfAbsent(type, instance);
if (o!=null) return type.cast(o);
return instance;
}
}