-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9e7d679
commit bee7289
Showing
2 changed files
with
169 additions
and
0 deletions.
There are no files selected for viewing
79 changes: 79 additions & 0 deletions
79
...provider/src/main/kotlin/xyz/xenondevs/commons/provider/immutable/SynchronizedProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package xyz.xenondevs.commons.provider.immutable | ||
|
||
import xyz.xenondevs.commons.provider.AbstractProvider | ||
import xyz.xenondevs.commons.provider.Provider | ||
|
||
/** | ||
* Creates and returns a new [Provider] that wraps [this][Provider], | ||
* makes all methods synchronized and fields volatile. | ||
*/ | ||
fun <T> Provider<T>.synchronized(): Provider<T> = | ||
SynchronizedVolatileProvider(this) | ||
|
||
private class SynchronizedVolatileProvider<T>(private val delegate: Provider<T>) : AbstractProvider<T>() { | ||
|
||
@Volatile | ||
override var isInitialized = false | ||
|
||
@Volatile | ||
override var value: T? = null | ||
|
||
init { | ||
delegate.addChild(this) | ||
} | ||
|
||
@Synchronized | ||
override fun loadValue(): T { | ||
return delegate.get() | ||
} | ||
|
||
@Synchronized | ||
override fun get(): T { | ||
return super.get() | ||
} | ||
|
||
@Synchronized | ||
override fun update() { | ||
super.update() | ||
} | ||
|
||
@Synchronized | ||
override fun addChild(provider: Provider<*>) { | ||
super.addChild(provider) | ||
} | ||
|
||
@Synchronized | ||
override fun subscribe(action: (T) -> Unit) { | ||
super.subscribe(action) | ||
} | ||
|
||
@Synchronized | ||
override fun <R : Any> subscribeWeak(owner: R, action: (R, T) -> Unit) { | ||
super.subscribeWeak(owner, action) | ||
} | ||
|
||
@Synchronized | ||
override fun removeChild(provider: Provider<*>) { | ||
super.removeChild(provider) | ||
} | ||
|
||
@Synchronized | ||
override fun unsubscribe(action: (T) -> Unit) { | ||
super.unsubscribe(action) | ||
} | ||
|
||
@Synchronized | ||
override fun <R : Any> unsubscribeWeak(owner: R, action: (R, T) -> Unit) { | ||
super.unsubscribeWeak(owner, action) | ||
} | ||
|
||
@Synchronized | ||
override fun <R : Any> unsubscribeWeak(owner: R) { | ||
super.unsubscribeWeak(owner) | ||
} | ||
|
||
override fun set(value: T, ignoredChildren: Set<Provider<*>>) { | ||
throw UnsupportedOperationException() | ||
} | ||
|
||
} |
90 changes: 90 additions & 0 deletions
90
...s-provider/src/main/kotlin/xyz/xenondevs/commons/provider/mutable/SynchronizedProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package xyz.xenondevs.commons.provider.mutable | ||
|
||
import xyz.xenondevs.commons.provider.AbstractProvider | ||
import xyz.xenondevs.commons.provider.Provider | ||
|
||
/** | ||
* Creates and returns a new [MutableProvider] that wraps [this][MutableProvider], | ||
* makes all methods synchronized and fields volatile. | ||
*/ | ||
fun <T> MutableProvider<T>.synchronized(): MutableProvider<T> = | ||
SynchronizedVolatileMutableProvider(this) | ||
|
||
private class SynchronizedVolatileMutableProvider<T>(private val delegate: MutableProvider<T>) : AbstractProvider<T>() { | ||
|
||
@Volatile | ||
override var children: MutableSet<Provider<*>>? = null | ||
|
||
@Volatile | ||
override var subscribers: MutableList<(T) -> Unit>? = null | ||
|
||
@Volatile | ||
override var weakSubscribers: MutableMap<Any, MutableList<(Any, T) -> Unit>>? = null | ||
|
||
@Volatile | ||
override var isInitialized = false | ||
|
||
@Volatile | ||
override var value: T? = null | ||
|
||
init { | ||
delegate.addChild(this) | ||
} | ||
|
||
@Synchronized | ||
override fun loadValue(): T { | ||
return delegate.get() | ||
} | ||
|
||
@Synchronized | ||
override fun set(value: T, ignoredChildren: Set<Provider<*>>) { | ||
super.set(value, ignoredChildren) | ||
delegate.set(value, setOf(this)) | ||
} | ||
|
||
@Synchronized | ||
override fun get(): T { | ||
return super.get() | ||
} | ||
|
||
@Synchronized | ||
override fun update() { | ||
super.update() | ||
} | ||
|
||
@Synchronized | ||
override fun addChild(provider: Provider<*>) { | ||
super.addChild(provider) | ||
} | ||
|
||
@Synchronized | ||
override fun subscribe(action: (T) -> Unit) { | ||
super.subscribe(action) | ||
} | ||
|
||
@Synchronized | ||
override fun <R : Any> subscribeWeak(owner: R, action: (R, T) -> Unit) { | ||
super.subscribeWeak(owner, action) | ||
} | ||
|
||
@Synchronized | ||
override fun removeChild(provider: Provider<*>) { | ||
super.removeChild(provider) | ||
} | ||
|
||
@Synchronized | ||
override fun unsubscribe(action: (T) -> Unit) { | ||
super.unsubscribe(action) | ||
} | ||
|
||
@Synchronized | ||
override fun <R : Any> unsubscribeWeak(owner: R, action: (R, T) -> Unit) { | ||
super.unsubscribeWeak(owner, action) | ||
} | ||
|
||
@Synchronized | ||
override fun <R : Any> unsubscribeWeak(owner: R) { | ||
super.unsubscribeWeak(owner) | ||
} | ||
|
||
} |