Skip to content

Commit

Permalink
Make weak provider hierarchies the default again
Browse files Browse the repository at this point in the history
  • Loading branch information
NichtStudioCode committed Nov 1, 2024
1 parent 0a8db09 commit 11c4e9e
Show file tree
Hide file tree
Showing 10 changed files with 186 additions and 173 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import java.lang.ref.WeakReference
*
* [transform] should be a pure function.
*/
inline fun <T, R> Provider<Collection<T>>.mapEach(crossinline transform: (T) -> R): Provider<List<R>> =
mapEachTo({ size -> ArrayList(size) }, transform)
inline fun <T, R> Provider<Collection<T>>.strongMapEach(crossinline transform: (T) -> R): Provider<List<R>> =
strongMapEachTo({ size -> ArrayList(size) }, transform)

/**
* Creates and returns a new [Provider] that maps each element of the [Collection] obtained from [this][Provider]
Expand All @@ -22,19 +22,19 @@ inline fun <T, R> Provider<Collection<T>>.mapEach(crossinline transform: (T) ->
*
* The returned provider will only be stored in a [WeakReference] in the parent provider ([this][MutableProvider]).
*/
inline fun <T, R> Provider<Collection<T>>.weakMapEach(crossinline transform: (T) -> R): Provider<List<R>> =
weakMapEachTo({ size -> ArrayList(size) }, transform)
inline fun <T, R> Provider<Collection<T>>.mapEach(crossinline transform: (T) -> R): Provider<List<R>> =
mapEachTo({ size -> ArrayList(size) }, transform)

/**
* Creates and returns a new [Provider] that maps each element of the [Collection] obtained from [this][Provider]
* using the [transform] function and adds the results to a collection created by [makeCollection].
*
* [makeCollection] and [transform] should be pure functions.
*/
inline fun <T, R, C : MutableCollection<in R>> Provider<Collection<T>>.mapEachTo(
inline fun <T, R, C : MutableCollection<in R>> Provider<Collection<T>>.strongMapEachTo(
crossinline makeCollection: (size: Int) -> C,
crossinline transform: (T) -> R
): Provider<C> = map { it.mapTo(makeCollection(it.size), transform) }
): Provider<C> = strongMap { it.mapTo(makeCollection(it.size), transform) }

/**
* Creates and returns a new [Provider] that maps each element of the [Collection] obtained from [this][Provider]
Expand All @@ -44,19 +44,19 @@ inline fun <T, R, C : MutableCollection<in R>> Provider<Collection<T>>.mapEachTo
*
* The returned provider will only be stored in a [WeakReference] in the parent provider ([this][MutableProvider]).
*/
inline fun <T, R, C : MutableCollection<in R>> Provider<Collection<T>>.weakMapEachTo(
inline fun <T, R, C : MutableCollection<in R>> Provider<Collection<T>>.mapEachTo(
crossinline makeCollection: (size: Int) -> C,
crossinline transform: (T) -> R
): Provider<C> = weakMap { it.mapTo(makeCollection(it.size), transform) }
): Provider<C> = map { it.mapTo(makeCollection(it.size), transform) }

/**
* Creates and returns a new [Provider] that maps each element of the [Collection] obtained from [this][Provider]
* using the [transform] function and filters out all null results.
*
* [transform] should be a pure function.
*/
inline fun <T, R : Any> Provider<Collection<T>>.mapEachNotNull(crossinline transform: (T) -> R?): Provider<List<R>> =
mapEachNotNullTo(::ArrayList, transform)
inline fun <T, R : Any> Provider<Collection<T>>.strongMapEachNotNull(crossinline transform: (T) -> R?): Provider<List<R>> =
strongMapEachNotNullTo(::ArrayList, transform)

/**
* Creates and returns a new [Provider] that maps each element of the [Collection] obtained from [this][Provider]
Expand All @@ -66,8 +66,8 @@ inline fun <T, R : Any> Provider<Collection<T>>.mapEachNotNull(crossinline trans
*
* The returned provider will only be stored in a [WeakReference] in the parent provider ([this][MutableProvider]).
*/
inline fun <T, R : Any> Provider<Collection<T>>.weakMapEachNotNull(crossinline transform: (T) -> R?): Provider<List<R>> =
weakMapEachNotNullTo(::ArrayList, transform)
inline fun <T, R : Any> Provider<Collection<T>>.mapEachNotNull(crossinline transform: (T) -> R?): Provider<List<R>> =
mapEachNotNullTo(::ArrayList, transform)

/**
* Creates and returns a new [Provider] that maps each element of the [Collection] obtained from [this][Provider]
Expand All @@ -76,10 +76,10 @@ inline fun <T, R : Any> Provider<Collection<T>>.weakMapEachNotNull(crossinline t
*
* [makeCollection] and [transform] should be pure functions.
*/
inline fun <T, R : Any, C : MutableCollection<in R>> Provider<Collection<T>>.mapEachNotNullTo(
inline fun <T, R : Any, C : MutableCollection<in R>> Provider<Collection<T>>.strongMapEachNotNullTo(
crossinline makeCollection: (size: Int) -> C,
crossinline transform: (T) -> R?
): Provider<C> = map { it.mapNotNullTo(makeCollection(it.size), transform) }
): Provider<C> = strongMap { it.mapNotNullTo(makeCollection(it.size), transform) }

/**
* Creates and returns a new [Provider] that maps each element of the [Collection] obtained from [this][Provider]
Expand All @@ -90,10 +90,10 @@ inline fun <T, R : Any, C : MutableCollection<in R>> Provider<Collection<T>>.map
*
* The returned provider will only be stored in a [WeakReference] in the parent provider ([this][MutableProvider]).
*/
inline fun <T, R : Any, C : MutableCollection<in R>> Provider<Collection<T>>.weakMapEachNotNullTo(
inline fun <T, R : Any, C : MutableCollection<in R>> Provider<Collection<T>>.mapEachNotNullTo(
crossinline makeCollection: (size: Int) -> C,
crossinline transform: (T) -> R?
): Provider<C> = weakMap { it.mapNotNullTo(makeCollection(it.size), transform) }
): Provider<C> = map { it.mapNotNullTo(makeCollection(it.size), transform) }


/**
Expand All @@ -102,8 +102,8 @@ inline fun <T, R : Any, C : MutableCollection<in R>> Provider<Collection<T>>.wea
*
* [transform] should be a pure function.
*/
inline fun <T, R> Provider<Collection<T>>.flatMapCollection(crossinline transform: (T) -> Iterable<R>): Provider<List<R>> =
flatMapCollectionTo({ size -> ArrayList(size) }, transform)
inline fun <T, R> Provider<Collection<T>>.strongFlatMapCollection(crossinline transform: (T) -> Iterable<R>): Provider<List<R>> =
strongFlatMapCollectionTo({ size -> ArrayList(size) }, transform)

/**
* Creates and returns a new [Provider] that flat-maps the elements of the [Collection] obtained from [this][Provider]
Expand All @@ -113,19 +113,19 @@ inline fun <T, R> Provider<Collection<T>>.flatMapCollection(crossinline transfor
*
* The returned provider will only be stored in a [WeakReference] in the parent provider ([this][MutableProvider]).
*/
inline fun <T, R> Provider<Collection<T>>.weakFlatMapCollection(crossinline transform: (T) -> Iterable<R>): Provider<List<R>> =
weakFlatMapCollectionTo({ size -> ArrayList(size) }, transform)
inline fun <T, R> Provider<Collection<T>>.flatMapCollection(crossinline transform: (T) -> Iterable<R>): Provider<List<R>> =
flatMapCollectionTo({ size -> ArrayList(size) }, transform)

/**
* Creates and returns a new [Provider] that flat-maps the elements of the [Collection] obtained from [this][Provider]
* into a collection created by [makeCollection] using the [transform] function.
*
* [makeCollection] and [transform] should be pure functions.
*/
inline fun <T, R, C : MutableCollection<in R>> Provider<Collection<T>>.flatMapCollectionTo(
inline fun <T, R, C : MutableCollection<in R>> Provider<Collection<T>>.strongFlatMapCollectionTo(
crossinline makeCollection: (size: Int) -> C,
crossinline transform: (T) -> Iterable<R>
): Provider<C> = map { it.flatMapTo(makeCollection(it.size), transform) }
): Provider<C> = strongMap { it.flatMapTo(makeCollection(it.size), transform) }

/**
* Creates and returns a new [Provider] that flat-maps the elements of the [Collection] obtained from [this][Provider]
Expand All @@ -135,47 +135,47 @@ inline fun <T, R, C : MutableCollection<in R>> Provider<Collection<T>>.flatMapCo
*
* The returned provider will only be stored in a [WeakReference] in the parent provider ([this][MutableProvider]).
*/
inline fun <T, R, C : MutableCollection<in R>> Provider<Collection<T>>.weakFlatMapCollectionTo(
inline fun <T, R, C : MutableCollection<in R>> Provider<Collection<T>>.flatMapCollectionTo(
crossinline makeCollection: (size: Int) -> C,
crossinline transform: (T) -> Iterable<R>
): Provider<C> = weakMap { it.flatMapTo(makeCollection(it.size), transform) }
): Provider<C> = map { it.flatMapTo(makeCollection(it.size), transform) }

/**
* Creates and returns a new [Provider] that flattens the [List] of [Lists][List] obtained from [this][Provider].
*/
fun <T> Provider<Iterable<Iterable<T>>>.flattenIterables(): Provider<List<T>> =
map { it.flatten() }
fun <T> Provider<Iterable<Iterable<T>>>.strongFlattenIterables(): Provider<List<T>> =
strongMap { it.flatten() }

/**
* Creates and returns a new [Provider] that flattens the [List] of [Lists][List] obtained from [this][Provider].
*
* The returned provider will only be stored in a [WeakReference] in the parent provider ([this][MutableProvider]).
*/
fun <T> Provider<Iterable<Iterable<T>>>.weakFlattenIterables(): Provider<List<T>> =
weakMap { it.flatten() }
fun <T> Provider<Iterable<Iterable<T>>>.flattenIterables(): Provider<List<T>> =
map { it.flatten() }

/**
* Creates and returns a new [Provider] that merges all [Maps][Map] obtained from [this][Provider] into a single [Map].
*/
fun <K, V> Provider<List<Map<K, V>>>.mergeMaps(): Provider<Map<K, V>> =
mergeMapsTo(::HashMap)
fun <K, V> Provider<List<Map<K, V>>>.strongMergeMaps(): Provider<Map<K, V>> =
strongMergeMapsTo(::HashMap)

/**
* Creates and returns a new [Provider] that merges all [Maps][Map] obtained from [this][Provider] into a single [Map].
*
* The returned provider will only be stored in a [WeakReference] in the parent provider ([this][MutableProvider]).
*/
fun <K, V> Provider<List<Map<K, V>>>.weakMergeMaps(): Provider<Map<K, V>> =
weakMergeMapsTo(::HashMap)
fun <K, V> Provider<List<Map<K, V>>>.mergeMaps(): Provider<Map<K, V>> =
mergeMapsTo(::HashMap)

/**
* Creates and returns a new [Provider] that merges all [Maps][Map] obtained from [this][Provider] into a single [Map],
* which is created by the [makeMap] function.
*
* [makeMap] should be a pure function.
*/
fun <K, V, M : MutableMap<in K, in V>> Provider<List<Map<K, V>>>.mergeMapsTo(makeMap: (size: Int) -> M): Provider<M> =
map { maps ->
fun <K, V, M : MutableMap<in K, in V>> Provider<List<Map<K, V>>>.strongMergeMapsTo(makeMap: (size: Int) -> M): Provider<M> =
strongMap { maps ->
val size = maps.sumOf { it.size }
val map = makeMap(size)
maps.forEach(map::putAll)
Expand All @@ -190,8 +190,8 @@ fun <K, V, M : MutableMap<in K, in V>> Provider<List<Map<K, V>>>.mergeMapsTo(mak
*
* The returned provider will only be stored in a [WeakReference] in the parent provider ([this][MutableProvider]).
*/
fun <K, V, M : MutableMap<in K, in V>> Provider<List<Map<K, V>>>.weakMergeMapsTo(makeMap: (size: Int) -> M): Provider<M> =
weakMap { maps ->
fun <K, V, M : MutableMap<in K, in V>> Provider<List<Map<K, V>>>.mergeMapsTo(makeMap: (size: Int) -> M): Provider<M> =
map { maps ->
val size = maps.sumOf { it.size }
val map = makeMap(size)
maps.forEach(map::putAll)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import kotlin.concurrent.withLock
* [mapValue] should be a pure function.
*/
@Suppress("UNCHECKED_CAST")
fun <T, R> combinedProvider(providers: List<Provider<T>>, mapValue: (List<T>) -> R): Provider<R> =
fun <T, R> strongCombinedProvider(providers: List<Provider<T>>, mapValue: (List<T>) -> R): Provider<R> =
CombinedMappingProvider(providers as List<AbstractProvider<T>>, mapValue, false)

/**
Expand All @@ -24,7 +24,7 @@ fun <T, R> combinedProvider(providers: List<Provider<T>>, mapValue: (List<T>) ->
*
* [mapValue] should be a pure function.
*/
fun <A, B, R> combinedProvider(
fun <A, B, R> strongCombinedProvider(
a: Provider<A>,
b: Provider<B>,
mapValue: (A, B) -> R
Expand All @@ -41,7 +41,7 @@ fun <A, B, R> combinedProvider(
*
* [mapValue] should be a pure function.
*/
fun <A, B, C, R> combinedProvider(
fun <A, B, C, R> strongCombinedProvider(
a: Provider<A>,
b: Provider<B>,
c: Provider<C>,
Expand All @@ -60,7 +60,7 @@ fun <A, B, C, R> combinedProvider(
*
* [mapValue] should be a pure function.
*/
fun <A, B, C, D, R> combinedProvider(
fun <A, B, C, D, R> strongCombinedProvider(
a: Provider<A>,
b: Provider<B>,
c: Provider<C>,
Expand All @@ -81,7 +81,7 @@ fun <A, B, C, D, R> combinedProvider(
*
* [mapValue] should be a pure function.
*/
fun <A, B, C, D, E, R> combinedProvider(
fun <A, B, C, D, E, R> strongCombinedProvider(
a: Provider<A>,
b: Provider<B>,
c: Provider<C>,
Expand All @@ -104,7 +104,7 @@ fun <A, B, C, D, E, R> combinedProvider(
*
* [mapValue] should be a pure function.
*/
fun <A, B, C, D, E, F, R> combinedProvider(
fun <A, B, C, D, E, F, R> strongCombinedProvider(
a: Provider<A>,
b: Provider<B>,
c: Provider<C>,
Expand All @@ -129,7 +129,7 @@ fun <A, B, C, D, E, F, R> combinedProvider(
*
* [mapValue] should be a pure function.
*/
fun <A, B, C, D, E, F, G, R> combinedProvider(
fun <A, B, C, D, E, F, G, R> strongCombinedProvider(
a: Provider<A>,
b: Provider<B>,
c: Provider<C>,
Expand All @@ -156,7 +156,7 @@ fun <A, B, C, D, E, F, G, R> combinedProvider(
*
* [mapValue] should be a pure function.
*/
fun <A, B, C, D, E, F, G, H, R> combinedProvider(
fun <A, B, C, D, E, F, G, H, R> strongCombinedProvider(
a: Provider<A>,
b: Provider<B>,
c: Provider<C>,
Expand Down Expand Up @@ -185,7 +185,7 @@ fun <A, B, C, D, E, F, G, H, R> combinedProvider(
*
* [mapValue] should be a pure function.
*/
fun <A, B, C, D, E, F, G, H, I, R> combinedProvider(
fun <A, B, C, D, E, F, G, H, I, R> strongCombinedProvider(
a: Provider<A>,
b: Provider<B>,
c: Provider<C>,
Expand Down Expand Up @@ -216,7 +216,7 @@ fun <A, B, C, D, E, F, G, H, I, R> combinedProvider(
*
* [mapValue] should be a pure function.
*/
fun <A, B, C, D, E, F, G, H, I, J, R> combinedProvider(
fun <A, B, C, D, E, F, G, H, I, J, R> strongCombinedProvider(
a: Provider<A>,
b: Provider<B>,
c: Provider<C>,
Expand Down Expand Up @@ -252,7 +252,7 @@ fun <A, B, C, D, E, F, G, H, I, J, R> combinedProvider(
* The returned provider will only be stored in a [WeakReference] in the parent providers.
*/
@Suppress("UNCHECKED_CAST")
fun <T, R> weakCombinedProvider(providers: List<Provider<T>>, mapValue: (List<T>) -> R): Provider<R> =
fun <T, R> combinedProvider(providers: List<Provider<T>>, mapValue: (List<T>) -> R): Provider<R> =
CombinedMappingProvider(providers as List<AbstractProvider<T>>, mapValue, true)

/**
Expand All @@ -263,7 +263,7 @@ fun <T, R> weakCombinedProvider(providers: List<Provider<T>>, mapValue: (List<T>
*
* The returned provider will only be stored in a [WeakReference] in the parent providers.
*/
fun <A, B, R> weakCombinedProvider(
fun <A, B, R> combinedProvider(
a: Provider<A>,
b: Provider<B>,
mapValue: (A, B) -> R
Expand All @@ -282,7 +282,7 @@ fun <A, B, R> weakCombinedProvider(
*
* The returned provider will only be stored in a [WeakReference] in the parent providers.
*/
fun <A, B, C, R> weakCombinedProvider(
fun <A, B, C, R> combinedProvider(
a: Provider<A>,
b: Provider<B>,
c: Provider<C>,
Expand All @@ -303,7 +303,7 @@ fun <A, B, C, R> weakCombinedProvider(
*
* The returned provider will only be stored in a [WeakReference] in the parent providers.
*/
fun <A, B, C, D, R> weakCombinedProvider(
fun <A, B, C, D, R> combinedProvider(
a: Provider<A>,
b: Provider<B>,
c: Provider<C>,
Expand All @@ -326,7 +326,7 @@ fun <A, B, C, D, R> weakCombinedProvider(
*
* The returned provider will only be stored in a [WeakReference] in the parent providers.
*/
fun <A, B, C, D, E, R> weakCombinedProvider(
fun <A, B, C, D, E, R> combinedProvider(
a: Provider<A>,
b: Provider<B>,
c: Provider<C>,
Expand All @@ -351,7 +351,7 @@ fun <A, B, C, D, E, R> weakCombinedProvider(
*
* The returned provider will only be stored in a [WeakReference] in the parent providers.
*/
fun <A, B, C, D, E, F, R> weakCombinedProvider(
fun <A, B, C, D, E, F, R> combinedProvider(
a: Provider<A>,
b: Provider<B>,
c: Provider<C>,
Expand All @@ -378,7 +378,7 @@ fun <A, B, C, D, E, F, R> weakCombinedProvider(
*
* The returned provider will only be stored in a [WeakReference] in the parent providers.
*/
fun <A, B, C, D, E, F, G, R> weakCombinedProvider(
fun <A, B, C, D, E, F, G, R> combinedProvider(
a: Provider<A>,
b: Provider<B>,
c: Provider<C>,
Expand Down Expand Up @@ -407,7 +407,7 @@ fun <A, B, C, D, E, F, G, R> weakCombinedProvider(
*
* The returned provider will only be stored in a [WeakReference] in the parent providers.
*/
fun <A, B, C, D, E, F, G, H, R> weakCombinedProvider(
fun <A, B, C, D, E, F, G, H, R> combinedProvider(
a: Provider<A>,
b: Provider<B>,
c: Provider<C>,
Expand Down Expand Up @@ -438,7 +438,7 @@ fun <A, B, C, D, E, F, G, H, R> weakCombinedProvider(
*
* The returned provider will only be stored in a [WeakReference] in the parent providers.
*/
fun <A, B, C, D, E, F, G, H, I, R> weakCombinedProvider(
fun <A, B, C, D, E, F, G, H, I, R> combinedProvider(
a: Provider<A>,
b: Provider<B>,
c: Provider<C>,
Expand Down Expand Up @@ -471,7 +471,7 @@ fun <A, B, C, D, E, F, G, H, I, R> weakCombinedProvider(
*
* The returned provider will only be stored in a [WeakReference] in the parent providers.
*/
fun <A, B, C, D, E, F, G, H, I, J, R> weakCombinedProvider(
fun <A, B, C, D, E, F, G, H, I, J, R> combinedProvider(
a: Provider<A>,
b: Provider<B>,
c: Provider<C>,
Expand Down
Loading

0 comments on commit 11c4e9e

Please sign in to comment.