From 7a9b7d98a1c7c70dfa19df91d6d701837e9dff3f Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 14 Jan 2022 13:22:04 +0600 Subject: [PATCH 1/4] start 0.9.4 --- CHANGELOG.md | 2 ++ gradle.properties | 6 +++--- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d687aa19f7c..d2c6891c6f5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ # Changelog +## 0.9.4 + ## 0.9.3 * `Versions`: diff --git a/gradle.properties b/gradle.properties index adb21b0bcf0..df9c509b06e 100644 --- a/gradle.properties +++ b/gradle.properties @@ -40,10 +40,10 @@ crypto_js_version=4.1.1 # Dokka -dokka_version=1.6.0 +dokka_version=1.6.10 # Project data group=dev.inmo -version=0.9.3 -android_code_version=93 +version=0.9.4 +android_code_version=94 From 85b3e48d18431d2c2d98112f49c9b6d15c26c555 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Mon, 17 Jan 2022 15:38:23 +0600 Subject: [PATCH 2/4] add optionallyReverse extensions --- CHANGELOG.md | 4 ++++ .../pagination/utils/IterableExtensions.kt | 14 ++++++++++++++ .../pagination/utils/PaginationReversing.kt | 12 ++++++++++++ 3 files changed, 30 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d2c6891c6f5..8951c64d5ef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## 0.9.4 +* `Pagination`: + * `Common`: + * Add several `optionallyReverse` functions + ## 0.9.3 * `Versions`: diff --git a/pagination/common/src/commonMain/kotlin/dev/inmo/micro_utils/pagination/utils/IterableExtensions.kt b/pagination/common/src/commonMain/kotlin/dev/inmo/micro_utils/pagination/utils/IterableExtensions.kt index 8c9829a150a..1ec7ced0b3b 100644 --- a/pagination/common/src/commonMain/kotlin/dev/inmo/micro_utils/pagination/utils/IterableExtensions.kt +++ b/pagination/common/src/commonMain/kotlin/dev/inmo/micro_utils/pagination/utils/IterableExtensions.kt @@ -38,3 +38,17 @@ fun Set.paginate(with: Pagination): PaginationResult { size.toLong() ) } + +fun Iterable.optionallyReverse(reverse: Boolean) = if (reverse) { + reversed() +} else { + this +} + +inline fun Array.optionallyReverse(reverse: Boolean) = if (reverse) { + Array(size) { + get(lastIndex - it) + } +} else { + this +} diff --git a/pagination/common/src/commonMain/kotlin/dev/inmo/micro_utils/pagination/utils/PaginationReversing.kt b/pagination/common/src/commonMain/kotlin/dev/inmo/micro_utils/pagination/utils/PaginationReversing.kt index b2761565296..a1b58660f1d 100644 --- a/pagination/common/src/commonMain/kotlin/dev/inmo/micro_utils/pagination/utils/PaginationReversing.kt +++ b/pagination/common/src/commonMain/kotlin/dev/inmo/micro_utils/pagination/utils/PaginationReversing.kt @@ -26,3 +26,15 @@ fun Pagination.reverse(datasetSize: Long): SimplePagination { * Shortcut for [reverse] */ fun Pagination.reverse(objectsCount: Int) = reverse(objectsCount.toLong()) + +fun Pagination.optionallyReverse(objectsCount: Int, reverse: Boolean) = if (reverse) { + reverse(objectsCount) +} else { + this +} + +fun Pagination.optionallyReverse(objectsCount: Long, reverse: Boolean) = if (reverse) { + reverse(objectsCount) +} else { + this +} From d619d599476cc81cddca2c1ac029a23f922dd5ff Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Mon, 17 Jan 2022 15:56:48 +0600 Subject: [PATCH 3/4] Either updates --- CHANGELOG.md | 5 ++ .../dev/inmo/micro_utils/common/Either.kt | 46 +++++++++++++------ 2 files changed, 38 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8951c64d5ef..19e43bf97fb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,11 @@ * `Pagination`: * `Common`: * Add several `optionallyReverse` functions +* `Common`: + * Changes in `Either`: + * Now `Either` uses `optionalT1` and `optionalT2` as main properties + * `Either#t1` and `Either#t2` are deprecated + * New extensions `Either#mapOnFirst` and `Either#mapOnSecond` ## 0.9.3 diff --git a/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/Either.kt b/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/Either.kt index 0ec4dd895fd..2bf9c57ade4 100644 --- a/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/Either.kt +++ b/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/Either.kt @@ -6,7 +6,7 @@ import kotlinx.serialization.descriptors.* import kotlinx.serialization.encoding.* /** - * Realization of this interface will contains at least one not null - [t1] or [t2] + * Realization of this interface will contains at least one not null - [optionalT1] or [optionalT2] * * @see EitherFirst * @see EitherSecond @@ -14,11 +14,19 @@ import kotlinx.serialization.encoding.* * @see Either.Companion.second * @see Either.onFirst * @see Either.onSecond + * @see Either.mapOnFirst + * @see Either.mapOnSecond */ @Serializable(EitherSerializer::class) sealed interface Either { + val optionalT1: Optional + val optionalT2: Optional + @Deprecated("Use optionalT1 instead", ReplaceWith("optionalT1")) val t1: T1? + get() = optionalT1.dataOrNull() + @Deprecated("Use optionalT2 instead", ReplaceWith("optionalT2")) val t2: T2? + get() = optionalT2.dataOrNull() companion object { fun serializer( @@ -93,25 +101,25 @@ class EitherSerializer( } /** - * This type [Either] will always have not nullable [t1] + * This type [Either] will always have not nullable [optionalT1] */ @Serializable data class EitherFirst( override val t1: T1 ) : Either { - override val t2: T2? - get() = null + override val optionalT1: Optional = t1.optional + override val optionalT2: Optional = Optional.absent() } /** - * This type [Either] will always have not nullable [t2] + * This type [Either] will always have not nullable [optionalT2] */ @Serializable data class EitherSecond( override val t2: T2 ) : Either { - override val t1: T1? - get() = null + override val optionalT1: Optional = Optional.absent() + override val optionalT2: Optional = t2.optional } /** @@ -124,23 +132,35 @@ inline fun Either.Companion.first(t1: T1): Either = EitherFirst inline fun Either.Companion.second(t2: T2): Either = EitherSecond(t2) /** - * Will call [block] in case when [Either.t1] of [this] is not null + * Will call [block] in case when [this] is [EitherFirst] */ inline fun > E.onFirst(block: (T1) -> Unit): E { - val t1 = t1 - t1 ?.let(block) + optionalT1.onPresented(block) return this } /** - * Will call [block] in case when [Either.t2] of [this] is not null + * Will call [block] in case when [this] is [EitherSecond] */ inline fun > E.onSecond(block: (T2) -> Unit): E { - val t2 = t2 - t2 ?.let(block) + optionalT2.onPresented(block) return this } +/** + * @return Result of [block] if [this] is [EitherFirst] + */ +inline fun Either.mapOnFirst(block: (T1) -> R): R? { + return optionalT1.mapOnPresented(block) +} + +/** + * @return Result of [block] if [this] is [EitherSecond] + */ +inline fun Either<*, T2>.mapOnSecond(block: (T2) -> R): R? { + return optionalT2.mapOnPresented(block) +} + inline fun Any.either() = when (this) { is T1 -> Either.first(this) is T2 -> Either.second(this) From 0202988cae1bde92530dc7d464616e9f787df23a Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Mon, 17 Jan 2022 16:11:55 +0600 Subject: [PATCH 4/4] several fixes in optionallyReverse --- .../pagination/utils/IterableExtensions.kt | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/pagination/common/src/commonMain/kotlin/dev/inmo/micro_utils/pagination/utils/IterableExtensions.kt b/pagination/common/src/commonMain/kotlin/dev/inmo/micro_utils/pagination/utils/IterableExtensions.kt index 1ec7ced0b3b..f31e5b1c3ea 100644 --- a/pagination/common/src/commonMain/kotlin/dev/inmo/micro_utils/pagination/utils/IterableExtensions.kt +++ b/pagination/common/src/commonMain/kotlin/dev/inmo/micro_utils/pagination/utils/IterableExtensions.kt @@ -39,11 +39,25 @@ fun Set.paginate(with: Pagination): PaginationResult { ) } -fun Iterable.optionallyReverse(reverse: Boolean) = if (reverse) { +fun Iterable.optionallyReverse(reverse: Boolean): Iterable = when (this) { + is List -> optionallyReverse(reverse) + is Set -> optionallyReverse(reverse) + else -> if (reverse) { + reversed() + } else { + this + } +} +fun List.optionallyReverse(reverse: Boolean): List = if (reverse) { reversed() } else { this } +fun Set.optionallyReverse(reverse: Boolean): Set = if (reverse) { + reversed().toSet() +} else { + this +} inline fun Array.optionallyReverse(reverse: Boolean) = if (reverse) { Array(size) {