Skip to content
This repository has been archived by the owner on Jul 13, 2020. It is now read-only.

Commit

Permalink
change name of concurrentGetOrPutSlightlyUnsafe to concurrentGetOrPut…
Browse files Browse the repository at this point in the history
…Proxy, and prepare to override it for JDK 8 module to be safe.
  • Loading branch information
apatrida committed Aug 11, 2015
1 parent 6584acd commit 9951615
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import java.util.concurrent.ConcurrentMap

// TODO: this is not perfect, the factory could be called more than once if two threads race to initialize a value
// JDK 8 has a real version of ConcurrentMap.computeIfAbsent
public inline fun <K,V> ConcurrentMap<K, V>.concurrentGetOrPutSlightlyUnsafe(key: K, defaultValue: ()-> V) : V {
public inline fun <K,V> ConcurrentMap<K, V>.concurrentGetOrPutProxy(key: K, defaultValue: ()-> V) : V {
var answer = this.get(key)
if (answer == null) {
answer = defaultValue()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public open class DefaultRegistrar : InjektRegistrar {
}

override fun <R> addSingletonFactory(forClass: Class<R>, factoryCalledOnce: () -> R) {
factories.put(forClass, { existingValues.concurrentGetOrPutSlightlyUnsafe(Instance(forClass, NOKEY), { factoryCalledOnce() }) })
factories.put(forClass, { existingValues.concurrentGetOrPutProxy(Instance(forClass, NOKEY), { factoryCalledOnce() }) })
}

override fun <R> addFactory(forClass: Class<R>, factoryCalledEveryTime: () -> R) {
Expand All @@ -62,23 +62,23 @@ public open class DefaultRegistrar : InjektRegistrar {

override fun <R> addPerThreadFactory(forClass: Class<R>, factoryCalledOncePerThread: () -> R) {
factories.put(forClass, {
existingValues.concurrentGetOrPutSlightlyUnsafe(Instance(forClass, ThreadKey(Thread.currentThread(), NOKEY)), { factoryCalledOncePerThread() })
existingValues.concurrentGetOrPutProxy(Instance(forClass, ThreadKey(Thread.currentThread(), NOKEY)), { factoryCalledOncePerThread() })
})
}

@suppress("UNCHECKED_CAST")
override fun <R, K> addPerKeyFactory(forClass: Class<R>, forKeyClass: Class<K>, factoryCalledPerKey: (K) -> R) {
keyedFactories.put(forClass, {
key ->
existingValues.concurrentGetOrPutSlightlyUnsafe(Instance(forClass, key), { factoryCalledPerKey(key as K) })
existingValues.concurrentGetOrPutProxy(Instance(forClass, key), { factoryCalledPerKey(key as K) })
})
}

@suppress("UNCHECKED_CAST")
override fun <R, K> addPerThreadPerKeyFactory(forClass: Class<R>, forKeyClass: Class<K>, factoryCalledPerKeyPerThread: (K) -> R) {
keyedFactories.put(forClass, {
key ->
existingValues.concurrentGetOrPutSlightlyUnsafe(Instance(forClass, ThreadKey(Thread.currentThread(), key)), { factoryCalledPerKeyPerThread(key as K) })
existingValues.concurrentGetOrPutProxy(Instance(forClass, ThreadKey(Thread.currentThread(), key)), { factoryCalledPerKeyPerThread(key as K) })
})
}

Expand Down

0 comments on commit 9951615

Please sign in to comment.