Skip to content

Commit f658a12

Browse files
committed
enable explicit api mode
1 parent c875159 commit f658a12

File tree

12 files changed

+80
-78
lines changed

12 files changed

+80
-78
lines changed

build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,7 @@ dependencies {
2121

2222
testImplementation "org.assertj:assertj-core:${versions.assertj}"
2323
}
24+
25+
kotlin {
26+
explicitApi()
27+
}

src/main/kotlin/ru/kontur/kinfra/commons/Either.kt

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,64 +12,64 @@ import ru.kontur.kinfra.commons.Either.Right
1212
* * [Left] represents an error description ([Left.error])
1313
* * [Right] represents a result of a successful computation ([Right.value])
1414
*/
15-
sealed class Either<out L, out R> {
15+
public sealed class Either<out L, out R> {
1616
// Sources of inspiration:
1717
// - Arrow's Either
1818
// - kotlin.Result
1919

20-
data class Left<out L> internal constructor(val error: L) : Either<L, Nothing>()
20+
public data class Left<out L> internal constructor(val error: L) : Either<L, Nothing>()
2121

22-
data class Right<out R> internal constructor(val value: R) : Either<Nothing, R>()
22+
public data class Right<out R> internal constructor(val value: R) : Either<Nothing, R>()
2323

24-
inline fun <T> fold(onFailure: (L) -> T, onSuccess: (R) -> T): T = when (this) {
24+
public inline fun <T> fold(onFailure: (L) -> T, onSuccess: (R) -> T): T = when (this) {
2525
is Left -> onFailure(error)
2626
is Right -> onSuccess(value)
2727
}
2828

29-
companion object {
29+
public companion object {
3030

31-
fun <L> left(error: L): Either<L, Nothing> = Left(error)
31+
public fun <L> left(error: L): Either<L, Nothing> = Left(error)
3232

33-
fun <R> right(value: R): Either<Nothing, R> = Right(value)
33+
public fun <R> right(value: R): Either<Nothing, R> = Right(value)
3434

3535
}
3636

3737
}
3838

39-
val <L, R> Either<L, R>.isLeft: Boolean
39+
public val <L, R> Either<L, R>.isLeft: Boolean
4040
get() = this is Left
4141

42-
val <L, R> Either<L, R>.isRight: Boolean
42+
public val <L, R> Either<L, R>.isRight: Boolean
4343
get() = this is Right
4444

45-
fun <L, R> Either<L, R>.leftOrNull(): L? = fold({ it }, { null })
45+
public fun <L, R> Either<L, R>.leftOrNull(): L? = fold({ it }, { null })
4646

47-
inline fun <L, R, T> Either<L, R>.mapLeft(transform: (L) -> T): Either<T, R> = when (this) {
47+
public inline fun <L, R, T> Either<L, R>.mapLeft(transform: (L) -> T): Either<T, R> = when (this) {
4848
is Left -> left(transform(error))
4949
is Right -> this
5050
}
5151

52-
inline fun <L, R, T> Either<L, R>.map(transform: (R) -> T): Either<L, T> = when (this) {
52+
public inline fun <L, R, T> Either<L, R>.map(transform: (R) -> T): Either<L, T> = when (this) {
5353
is Left -> this
5454
is Right -> right(transform(value))
5555
}
5656

57-
inline fun <L, R, T> Either<L, R>.flatMap(transform: (R) -> Either<L, T>): Either<L, T> = when (this) {
57+
public inline fun <L, R, T> Either<L, R>.flatMap(transform: (R) -> Either<L, T>): Either<L, T> = when (this) {
5858
is Left -> this
5959
is Right -> transform(value)
6060
}
6161

62-
fun <R> Either<*, R>.getOrNull(): R? = getOrDefault(null)
62+
public fun <R> Either<*, R>.getOrNull(): R? = getOrDefault(null)
6363

64-
fun <R> Either<*, R>.getOrDefault(default: R): R = getOrElse { default }
64+
public fun <R> Either<*, R>.getOrDefault(default: R): R = getOrElse { default }
6565

66-
inline fun <L, R> Either<L, R>.getOrThrow(exceptionSource: (L) -> Exception): R {
66+
public inline fun <L, R> Either<L, R>.getOrThrow(exceptionSource: (L) -> Exception): R {
6767
return getOrElse { throw exceptionSource(it) }
6868
}
6969

70-
inline fun <L, R> Either<L, R>.getOrElse(transform: (L) -> R): R = when (this) {
70+
public inline fun <L, R> Either<L, R>.getOrElse(transform: (L) -> R): R = when (this) {
7171
is Left -> transform(error)
7272
is Right -> value
7373
}
7474

75-
fun <R> Either<*, R>.ensureSuccess(): R = getOrThrow { IllegalStateException("Result is error: $it") }
75+
public fun <R> Either<*, R>.ensureSuccess(): R = getOrThrow { IllegalStateException("Result is error: $it") }

src/main/kotlin/ru/kontur/kinfra/commons/Encoding.kt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ import kotlin.reflect.KClass
99
* @param L type of "plain" ("decoded") object
1010
* @param R type of "encoded" object
1111
*/
12-
interface Encoding<L, R> {
12+
public interface Encoding<L, R> {
1313

14-
fun encode(value: L): R
14+
public fun encode(value: L): R
1515

16-
fun decode(value: R): L
16+
public fun decode(value: R): L
1717

18-
companion object {
18+
public companion object {
1919

2020
/**
2121
* Creates an encoding for an [Enum] using an [encoding function][encoder].
@@ -25,11 +25,11 @@ interface Encoding<L, R> {
2525
*
2626
* @throws IllegalStateException if there are duplicate mappings
2727
*/
28-
inline fun <reified L : Enum<L>, R> enum(noinline encoder: (L) -> R): Encoding<L, R> {
28+
public inline fun <reified L : Enum<L>, R> enum(noinline encoder: (L) -> R): Encoding<L, R> {
2929
return enum(L::class, encoder)
3030
}
3131

32-
fun <E : Enum<E>, T> enum(enumClass: KClass<E>, encoder: (E) -> T): Encoding<E, T> {
32+
public fun <E : Enum<E>, T> enum(enumClass: KClass<E>, encoder: (E) -> T): Encoding<E, T> {
3333
val values = enumClass.java.enumConstants
3434
val directMapping = EnumMap<E, T>(enumClass.java)
3535
val reverseMapping = mutableMapOf<T, E>()
@@ -51,14 +51,14 @@ interface Encoding<L, R> {
5151
*
5252
* @throws IllegalStateException if there are duplicate or missing mappings
5353
*/
54-
inline fun <reified L : Enum<L>, reified R : Enum<R>> enumBijection(
54+
public inline fun <reified L : Enum<L>, reified R : Enum<R>> enumBijection(
5555
noinline mapper: (L) -> R
5656
): Encoding<L, R> {
5757

5858
return enumBijection(L::class, R::class, mapper)
5959
}
6060

61-
fun <L : Enum<L>, R : Enum<R>> enumBijection(
61+
public fun <L : Enum<L>, R : Enum<R>> enumBijection(
6262
leftEnumClass: KClass<L>,
6363
rightEnumClass: KClass<R>,
6464
mapper: (L) -> R
@@ -89,11 +89,11 @@ interface Encoding<L, R> {
8989
*
9090
* @throws IllegalStateException if constants of the enum classes are named differently
9191
*/
92-
inline fun <reified L : Enum<L>, reified R : Enum<R>> enumBijectionByName(): Encoding<L, R> {
92+
public inline fun <reified L : Enum<L>, reified R : Enum<R>> enumBijectionByName(): Encoding<L, R> {
9393
return enumBijectionByName(L::class, R::class)
9494
}
9595

96-
fun <L : Enum<L>, R : Enum<R>> enumBijectionByName(
96+
public fun <L : Enum<L>, R : Enum<R>> enumBijectionByName(
9797
leftClass: KClass<L>,
9898
rightClass: KClass<R>
9999
): Encoding<L, R> {
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package ru.kontur.kinfra.commons
22

3-
import java.util.*
4-
53
/**
64
* Returns name of this enum constant in lower case.
75
*/
8-
val Enum<*>.lowerCaseName: String
9-
get() = name.toLowerCase(Locale.ROOT)
6+
public val Enum<*>.lowerCaseName: String
7+
get() = name.lowercase()

src/main/kotlin/ru/kontur/kinfra/commons/JdkExtensions.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ import java.util.*
88
*
99
* This method is safer alternative to `orElse(null)`, whose return type is `T!`.
1010
*/
11-
fun <T> Optional<T>.unwrap(): T? = orElse(null)
11+
public fun <T> Optional<T>.unwrap(): T? = orElse(null)
1212

1313
@Deprecated(message = "for compatibility", level = DeprecationLevel.HIDDEN)
1414
@JvmName("instant")
15-
fun UUID.instantCompatibilityBridge(): Instant {
15+
public fun UUID.instantCompatibilityBridge(): Instant {
1616
return instant()
1717
}

src/main/kotlin/ru/kontur/kinfra/commons/PrimitiveExtensions.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ package ru.kontur.kinfra.commons
44
* If this boolean is `true`, calls specified [block] and returns its result.
55
* Otherwise, just returns `null`.
66
*/
7-
inline fun <R> Boolean.thenTake(block: () -> R): R? = if (this) block() else null
7+
public inline fun <R> Boolean.thenTake(block: () -> R): R? = if (this) block() else null
88

99
/**
1010
* If this string is not empty, returns specified [prefix] followed by this string.
1111
* Otherwise, returns this string.
1212
*/
13-
fun String.prefixNotEmpty(prefix: String): String {
13+
public fun String.prefixNotEmpty(prefix: String): String {
1414
return if (isNotEmpty()) prefix + this else this
1515
}

src/main/kotlin/ru/kontur/kinfra/commons/TimeBasedUuid.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import kotlin.random.Random
1313
/**
1414
* Determines if this UUID is time-based (version 1)
1515
*/
16-
val UUID.isTimeBased: Boolean
16+
public val UUID.isTimeBased: Boolean
1717
get() = version() == 1
1818

1919
/**
@@ -22,14 +22,14 @@ val UUID.isTimeBased: Boolean
2222
* @throws UnsupportedOperationException if this UUID is not time-based
2323
* @see UUID.timestamp
2424
*/
25-
fun UUID.instant(): Instant {
25+
public fun UUID.instant(): Instant {
2626
return TimeTicks.UuidTimestamp(timestamp()).toInstant()
2727
}
2828

2929
/**
3030
* Creates a new time-based (version 1) UUID.
3131
*/
32-
fun generateTimeBasedUuid(): UUID {
32+
public fun generateTimeBasedUuid(): UUID {
3333
return TimeBasedUuidGenerator.generate()
3434
}
3535

src/main/kotlin/ru/kontur/kinfra/commons/binary/Conversions.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ package ru.kontur.kinfra.commons.binary
33
/**
44
* Returns numeric value of this byte treating it as unsigned.
55
*/
6-
fun Byte.asUnsigned(): Int {
6+
public fun Byte.asUnsigned(): Int {
77
return toInt() and 0xff
88
}
99

1010
/**
1111
* Returns numeric value of this short treating it as unsigned.
1212
*/
13-
fun Short.asUnsigned(): Int {
13+
public fun Short.asUnsigned(): Int {
1414
return toInt() and 0xffff
1515
}

src/main/kotlin/ru/kontur/kinfra/commons/binary/Hex.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ private val hexDigitsUpper = "0123456789ABCDEF".toCharArray()
66
/**
77
* Returns this byte in form of two hexadecimal digits.
88
*/
9-
fun Byte.toHexString(upperCase: Boolean = false): String {
9+
public fun Byte.toHexString(upperCase: Boolean = false): String {
1010
val unsigned = asUnsigned()
1111
val hexDigits = if (upperCase) hexDigitsUpper else hexDigitsLower
1212
val chars = CharArray(2)
@@ -16,7 +16,7 @@ fun Byte.toHexString(upperCase: Boolean = false): String {
1616
}
1717

1818
@Deprecated(message = "for compatibility", level = DeprecationLevel.HIDDEN)
19-
fun Byte.toHexString(): String {
19+
public fun Byte.toHexString(): String {
2020
return toHexString(upperCase = false)
2121
}
2222

@@ -25,14 +25,14 @@ fun Byte.toHexString(): String {
2525
*
2626
* Inverse transformation is possible with [byteArrayOfHex].
2727
*/
28-
fun ByteArray.toHexString(upperCase: Boolean = false): String = buildString(size * 2) {
28+
public fun ByteArray.toHexString(upperCase: Boolean = false): String = buildString(size * 2) {
2929
for (byte in this@toHexString) {
3030
appendHexByte(byte, upperCase)
3131
}
3232
}
3333

3434
@Deprecated(message = "for compatibility", level = DeprecationLevel.HIDDEN)
35-
fun ByteArray.toHexString(): String {
35+
public fun ByteArray.toHexString(): String {
3636
return toHexString(upperCase = false)
3737
}
3838

@@ -41,7 +41,7 @@ fun ByteArray.toHexString(): String {
4141
*
4242
* Inverse transformation is possible with [ByteArray.toHexString].
4343
*/
44-
fun byteArrayOfHex(hex: String): ByteArray {
44+
public fun byteArrayOfHex(hex: String): ByteArray {
4545
require(hex.length % 2 == 0) {
4646
"Hex string must have even number of digits (actual is ${hex.length})"
4747
}
@@ -59,14 +59,14 @@ fun byteArrayOfHex(hex: String): ByteArray {
5959
* Append hexadecimal representation of a [byte] to this builder.
6060
* Exactly two hexadecimal digits are appended.
6161
*/
62-
fun StringBuilder.appendHexByte(byte: Byte, upperCase: Boolean = false) {
62+
public fun StringBuilder.appendHexByte(byte: Byte, upperCase: Boolean = false) {
6363
val unsigned = byte.asUnsigned()
6464
val hexDigits = if (upperCase) hexDigitsUpper else hexDigitsLower
6565
append(hexDigits[unsigned shr 4])
6666
append(hexDigits[unsigned and 0xF])
6767
}
6868

6969
@Deprecated(message = "for compatibility", level = DeprecationLevel.HIDDEN)
70-
fun StringBuilder.appendHexByte(byte: Byte) {
70+
public fun StringBuilder.appendHexByte(byte: Byte) {
7171
appendHexByte(byte, upperCase = false)
7272
}

src/main/kotlin/ru/kontur/kinfra/commons/time/Deadline.kt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,25 +39,25 @@ import kotlin.coroutines.coroutineContext
3939
* }
4040
* ```
4141
*/
42-
class Deadline private constructor(
42+
public class Deadline private constructor(
4343
private val time: MonotonicInstant
4444
) : CoroutineContext.Element, Comparable<Deadline> {
4545

46-
fun isPassed(): Boolean = timeLeft() <= Duration.ZERO
46+
public fun isPassed(): Boolean = timeLeft() <= Duration.ZERO
4747

4848
/**
4949
* Returns an amount of time before this deadline will [pass][isPassed].
5050
*
5151
* Negative values represent the time elapsed since this deadline is passed.
5252
*/
53-
fun timeLeft(): Duration {
53+
public fun timeLeft(): Duration {
5454
// Result is truncated because withTimeout's precision is a millisecond
5555
return (time - now()).truncatedTo(ChronoUnit.MILLIS)
5656
}
5757

58-
operator fun plus(offset: Duration) = Deadline(time + offset)
58+
public operator fun plus(offset: Duration): Deadline = Deadline(time + offset)
5959

60-
operator fun minus(offset: Duration) = Deadline(time - offset)
60+
public operator fun minus(offset: Duration): Deadline = Deadline(time - offset)
6161

6262
override fun compareTo(other: Deadline): Int {
6363
return time.compareTo(other.time)
@@ -78,14 +78,14 @@ class Deadline private constructor(
7878
return "Deadline($time)"
7979
}
8080

81-
companion object : CoroutineContext.Key<Deadline> {
81+
public companion object : CoroutineContext.Key<Deadline> {
8282

8383
/**
8484
* Returns a [Deadline] that passes after given [timeout] from now.
8585
*
8686
* The timeout **must** be positive.
8787
*/
88-
fun after(timeout: Duration): Deadline {
88+
public fun after(timeout: Duration): Deadline {
8989
require(timeout > Duration.ZERO) { "Timeout must be positive: $timeout" }
9090
return Deadline(now() + timeout)
9191
}
@@ -100,7 +100,7 @@ class Deadline private constructor(
100100
*
101101
* If current deadline is less than the specified one, it will be used instead.
102102
*/
103-
suspend fun <R> withDeadline(deadline: Deadline, block: suspend CoroutineScope.() -> R): R {
103+
public suspend fun <R> withDeadline(deadline: Deadline, block: suspend CoroutineScope.() -> R): R {
104104
val currentDeadline = coroutineContext[Deadline]
105105
val newDeadline = currentDeadline
106106
?.let { minOf(it, deadline) }
@@ -117,7 +117,7 @@ suspend fun <R> withDeadline(deadline: Deadline, block: suspend CoroutineScope.(
117117
* @see withDeadline
118118
* @see Deadline.after
119119
*/
120-
suspend fun <R> withDeadlineAfter(timeout: Duration, block: suspend CoroutineScope.() -> R): R {
120+
public suspend fun <R> withDeadlineAfter(timeout: Duration, block: suspend CoroutineScope.() -> R): R {
121121
val deadline = Deadline.after(timeout)
122122
return withDeadline(deadline, block)
123123
}

0 commit comments

Comments
 (0)