diff --git a/commons-provider/src/main/kotlin/xyz/xenondevs/commons/provider/CollectionMappingProviders.kt b/commons-provider/src/main/kotlin/xyz/xenondevs/commons/provider/CollectionMappingProviders.kt new file mode 100644 index 0000000..15c4993 --- /dev/null +++ b/commons-provider/src/main/kotlin/xyz/xenondevs/commons/provider/CollectionMappingProviders.kt @@ -0,0 +1,198 @@ +package xyz.xenondevs.commons.provider + +import java.lang.ref.WeakReference + +/** + * Creates and returns a new [Provider] that maps each element of the [Collection] obtained from [this][Provider] + * using the [transform] function. + * + * [transform] should be a pure function. + */ +inline fun Provider>.mapEach(crossinline transform: (T) -> R): Provider> = + 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. + * + * [transform] should be a pure function. + * + * The returned provider will only be stored in a [WeakReference] in the parent provider ([this][MutableProvider]). + */ +inline fun Provider>.weakMapEach(crossinline transform: (T) -> R): Provider> = + weakMapEachTo({ 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 > Provider>.mapEachTo( + crossinline makeCollection: (size: Int) -> C, + crossinline transform: (T) -> R +): Provider = 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 adds the results to a collection created by [makeCollection]. + * + * [makeCollection] and [transform] should be pure functions. + * + * The returned provider will only be stored in a [WeakReference] in the parent provider ([this][MutableProvider]). + */ +inline fun > Provider>.weakMapEachTo( + crossinline makeCollection: (size: Int) -> C, + crossinline transform: (T) -> R +): Provider = weakMap { 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 Provider>.mapEachNotNull(crossinline transform: (T) -> R?): Provider> = + mapEachNotNullTo(::ArrayList, 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. + * + * The returned provider will only be stored in a [WeakReference] in the parent provider ([this][MutableProvider]). + */ +inline fun Provider>.weakMapEachNotNull(crossinline transform: (T) -> R?): Provider> = + weakMapEachNotNullTo(::ArrayList, 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. + * The results are added to a collection created by [makeCollection]. + * + * [makeCollection] and [transform] should be pure functions. + */ +inline fun > Provider>.mapEachNotNullTo( + crossinline makeCollection: (size: Int) -> C, + crossinline transform: (T) -> R? +): Provider = map { it.mapNotNullTo(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. + * The results are added to a collection created by [makeCollection]. + * + * [makeCollection] and [transform] should be pure functions. + * + * The returned provider will only be stored in a [WeakReference] in the parent provider ([this][MutableProvider]). + */ +inline fun > Provider>.weakMapEachNotNullTo( + crossinline makeCollection: (size: Int) -> C, + crossinline transform: (T) -> R? +): Provider = weakMap { it.mapNotNullTo(makeCollection(it.size), transform) } + + +/** + * Creates and returns a new [Provider] that flat-maps the elements of the [Collection] obtained from [this][Provider] + * into a list using the [transform] function. + * + * [transform] should be a pure function. + */ +@JvmName("flatMapCollection") +inline fun Provider>.flatMapCollection(crossinline transform: (T) -> Iterable): Provider> = + 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 list using the [transform] function. + * + * [transform] should be a pure function. + * + * The returned provider will only be stored in a [WeakReference] in the parent provider ([this][MutableProvider]). + */ +@JvmName("weakFlatMapCollection") +inline fun Provider>.weakFlatMapCollection(crossinline transform: (T) -> Iterable): Provider> = + weakFlatMapCollectionTo({ 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 > Provider>.flatMapCollectionTo( + crossinline makeCollection: (size: Int) -> C, + crossinline transform: (T) -> Iterable +): Provider = map { it.flatMapTo(makeCollection(it.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. + * + * The returned provider will only be stored in a [WeakReference] in the parent provider ([this][MutableProvider]). + */ +inline fun > Provider>.weakFlatMapCollectionTo( + crossinline makeCollection: (size: Int) -> C, + crossinline transform: (T) -> Iterable +): Provider = weakMap { it.flatMapTo(makeCollection(it.size), transform) } + +/** + * Creates and returns a new [Provider] that flattens the [List] of [Lists][List] obtained from [this][Provider]. + */ +fun Provider>>.flattenIterables(): Provider> = + map { 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 Provider>>.weakFlattenIterables(): Provider> = + weakMap { it.flatten() } + +/** + * Creates and returns a new [Provider] that merges all [Maps][Map] obtained from [this][Provider] into a single [Map]. + */ +fun Provider>>.mergeMaps(): Provider> = + mergeMapsTo(::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 Provider>>.weakMergeMaps(): Provider> = + weakMergeMapsTo(::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 > Provider>>.mergeMapsTo(makeMap: (size: Int) -> M): Provider = + map { maps -> + val size = maps.sumOf { it.size } + val map = makeMap(size) + maps.forEach(map::putAll) + map + } + +/** + * 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. + * + * The returned provider will only be stored in a [WeakReference] in the parent provider ([this][MutableProvider]). + */ +fun > Provider>>.weakMergeMapsTo(makeMap: (size: Int) -> M): Provider = + weakMap { maps -> + val size = maps.sumOf { it.size } + val map = makeMap(size) + maps.forEach(map::putAll) + map + } diff --git a/commons-provider/src/main/kotlin/xyz/xenondevs/commons/provider/MappingProviders.kt b/commons-provider/src/main/kotlin/xyz/xenondevs/commons/provider/MappingProviders.kt index af9da05..479b9b6 100644 --- a/commons-provider/src/main/kotlin/xyz/xenondevs/commons/provider/MappingProviders.kt +++ b/commons-provider/src/main/kotlin/xyz/xenondevs/commons/provider/MappingProviders.kt @@ -97,261 +97,6 @@ inline fun Provider.mapNonNull(crossinline transform: (T) -> R) inline fun Provider.weakMapNonNull(crossinline transform: (T) -> R): Provider = weakMap { it?.let(transform) } -/** - * Creates and returns a new [Provider] that maps each element of the [Collection] obtained from [this][Provider] - * using the [transform] function. - * - * [transform] should be a pure function. - */ -inline fun Provider>.mapEach(crossinline transform: (T) -> R): Provider> = - 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. - * - * [transform] should be a pure function. - * - * The returned provider will only be stored in a [WeakReference] in the parent provider ([this][MutableProvider]). - */ -inline fun Provider>.weakMapEach(crossinline transform: (T) -> R): Provider> = - weakMapEachTo({ 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 > Provider>.mapEachTo( - crossinline makeCollection: (size: Int) -> C, - crossinline transform: (T) -> R -): Provider = 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 adds the results to a collection created by [makeCollection]. - * - * [makeCollection] and [transform] should be pure functions. - * - * The returned provider will only be stored in a [WeakReference] in the parent provider ([this][MutableProvider]). - */ -inline fun > Provider>.weakMapEachTo( - crossinline makeCollection: (size: Int) -> C, - crossinline transform: (T) -> R -): Provider = weakMap { 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 Provider>.mapEachNotNull(crossinline transform: (T) -> R?): Provider> = - mapEachNotNullTo(::ArrayList, 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. - * - * The returned provider will only be stored in a [WeakReference] in the parent provider ([this][MutableProvider]). - */ -inline fun Provider>.weakMapEachNotNull(crossinline transform: (T) -> R?): Provider> = - weakMapEachNotNullTo(::ArrayList, 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. - * The results are added to a collection created by [makeCollection]. - * - * [makeCollection] and [transform] should be pure functions. - */ -inline fun > Provider>.mapEachNotNullTo( - crossinline makeCollection: (size: Int) -> C, - crossinline transform: (T) -> R? -): Provider = map { it.mapNotNullTo(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. - * The results are added to a collection created by [makeCollection]. - * - * [makeCollection] and [transform] should be pure functions. - * - * The returned provider will only be stored in a [WeakReference] in the parent provider ([this][MutableProvider]). - */ -inline fun > Provider>.weakMapEachNotNullTo( - crossinline makeCollection: (size: Int) -> C, - crossinline transform: (T) -> R? -): Provider = weakMap { it.mapNotNullTo(makeCollection(it.size), transform) } - - -/** - * Creates and returns a new [Provider] that flat-maps the elements of the [Collection] obtained from [this][Provider] - * into a list using the [transform] function. - * - * [transform] should be a pure function. - */ -@JvmName("flatMapCollection") -inline fun Provider>.flatMapCollection(crossinline transform: (T) -> Iterable): Provider> = - 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 list using the [transform] function. - * - * [transform] should be a pure function. - * - * The returned provider will only be stored in a [WeakReference] in the parent provider ([this][MutableProvider]). - */ -@JvmName("weakFlatMapCollection") -inline fun Provider>.weakFlatMapCollection(crossinline transform: (T) -> Iterable): Provider> = - weakFlatMapCollectionTo({ 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 > Provider>.flatMapCollectionTo( - crossinline makeCollection: (size: Int) -> C, - crossinline transform: (T) -> Iterable -): Provider = map { it.flatMapTo(makeCollection(it.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. - * - * The returned provider will only be stored in a [WeakReference] in the parent provider ([this][MutableProvider]). - */ -inline fun > Provider>.weakFlatMapCollectionTo( - crossinline makeCollection: (size: Int) -> C, - crossinline transform: (T) -> Iterable -): Provider = weakMap { it.flatMapTo(makeCollection(it.size), transform) } - -/** - * Creates and returns a new [Provider] that flattens the [List] of [Lists][List] obtained from [this][Provider]. - */ -fun Provider>>.flattenIterables(): Provider> = - map { 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 Provider>>.weakFlattenIterables(): Provider> = - weakMap { it.flatten() } - -/** - * Creates and returns a new [Provider] that merges all [Maps][Map] obtained from [this][Provider] into a single [Map]. - */ -fun Provider>>.mergeMaps(): Provider> = - mergeMapsTo(::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 Provider>>.weakMergeMaps(): Provider> = - weakMergeMapsTo(::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 > Provider>>.mergeMapsTo(makeMap: (size: Int) -> M): Provider = - map { maps -> - val size = maps.sumOf { it.size } - val map = makeMap(size) - maps.forEach(map::putAll) - map - } - -/** - * 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. - * - * The returned provider will only be stored in a [WeakReference] in the parent provider ([this][MutableProvider]). - */ -fun > Provider>>.weakMergeMapsTo(makeMap: (size: Int) -> M): Provider = - weakMap { maps -> - val size = maps.sumOf { it.size } - val map = makeMap(size) - maps.forEach(map::putAll) - map - } - -/** - * Creates and returns a new [Provider] that throws an [IllegalArgumentException] - * with a message generated by [message] if [condition] fails. - * - * [condition] and [message] should be pure functions. - */ -inline fun Provider.require( - crossinline condition: (T) -> Boolean, - crossinline message: (T) -> String -): Provider = map { require(condition(it)) { message(it) }; it } - -/** - * Creates and returns a new [Provider] that throws an [IllegalArgumentException] - * with a message generated by [message] if [condition] fails. - * - * [condition] and [message] should be pure functions. - * - * The returned provider will only be stored in a [WeakReference] in the parent provider ([this][MutableProvider]). - */ -inline fun Provider.weakRequire( - crossinline condition: (T) -> Boolean, - crossinline message: (T) -> String -): Provider = weakMap { require(condition(it)) { message(it) }; it } - -/** - * Creates and returns a new [Provider] that throws an [IllegalArgumentException] - * with [message] if the value is `null`. - */ -fun Provider.requireNotNull(message: String = "Required value was null."): Provider = - requireNotNull { message } - -/** - * Creates and returns a new [Provider] that throws an [IllegalArgumentException] - * with [message] if the value is `null`. - * - * The returned provider will only be stored in a [WeakReference] in the parent provider ([this][MutableProvider]). - */ -fun Provider.weakRequireNotNull(message: String = "Required value was null."): Provider = - weakRequireNotNull { message } - -/** - * Creates and returns a new [Provider] that throws an [IllegalArgumentException] - * with a message generated by [message] if the value is `null`. - * - * [message] should be a pure function. - */ -inline fun Provider.requireNotNull(crossinline message: () -> String): Provider = - map { requireNotNull(it, message); it } - -/** - * Creates and returns a new [Provider] that throws an [IllegalArgumentException] - * with a message generated by [message] if the value is `null`. - * - * [message] should be a pure function. - * - * The returned provider will only be stored in a [WeakReference] in the parent provider ([this][MutableProvider]). - */ -inline fun Provider.weakRequireNotNull(crossinline message: () -> String): Provider = - weakMap { requireNotNull(it, message); it } - private class MappingProvider( private val parent: AbstractProvider

, private val transform: (P) -> T, diff --git a/commons-provider/src/main/kotlin/xyz/xenondevs/commons/provider/RequirementProviders.kt b/commons-provider/src/main/kotlin/xyz/xenondevs/commons/provider/RequirementProviders.kt new file mode 100644 index 0000000..424133e --- /dev/null +++ b/commons-provider/src/main/kotlin/xyz/xenondevs/commons/provider/RequirementProviders.kt @@ -0,0 +1,63 @@ +package xyz.xenondevs.commons.provider + +import java.lang.ref.WeakReference + +/** + * Creates and returns a new [Provider] that throws an [IllegalArgumentException] + * with a message generated by [message] if [condition] fails. + * + * [condition] and [message] should be pure functions. + */ +inline fun Provider.require( + crossinline condition: (T) -> Boolean, + crossinline message: (T) -> String +): Provider = map { require(condition(it)) { message(it) }; it } + +/** + * Creates and returns a new [Provider] that throws an [IllegalArgumentException] + * with a message generated by [message] if [condition] fails. + * + * [condition] and [message] should be pure functions. + * + * The returned provider will only be stored in a [WeakReference] in the parent provider ([this][MutableProvider]). + */ +inline fun Provider.weakRequire( + crossinline condition: (T) -> Boolean, + crossinline message: (T) -> String +): Provider = weakMap { require(condition(it)) { message(it) }; it } + +/** + * Creates and returns a new [Provider] that throws an [IllegalArgumentException] + * with [message] if the value is `null`. + */ +fun Provider.requireNotNull(message: String = "Required value was null."): Provider = + requireNotNull { message } + +/** + * Creates and returns a new [Provider] that throws an [IllegalArgumentException] + * with [message] if the value is `null`. + * + * The returned provider will only be stored in a [WeakReference] in the parent provider ([this][MutableProvider]). + */ +fun Provider.weakRequireNotNull(message: String = "Required value was null."): Provider = + weakRequireNotNull { message } + +/** + * Creates and returns a new [Provider] that throws an [IllegalArgumentException] + * with a message generated by [message] if the value is `null`. + * + * [message] should be a pure function. + */ +inline fun Provider.requireNotNull(crossinline message: () -> String): Provider = + map { requireNotNull(it, message); it } + +/** + * Creates and returns a new [Provider] that throws an [IllegalArgumentException] + * with a message generated by [message] if the value is `null`. + * + * [message] should be a pure function. + * + * The returned provider will only be stored in a [WeakReference] in the parent provider ([this][MutableProvider]). + */ +inline fun Provider.weakRequireNotNull(crossinline message: () -> String): Provider = + weakMap { requireNotNull(it, message); it } \ No newline at end of file