Skip to content

Commit 00ba09c

Browse files
author
Lucas Sales
committed
Rename result to sealed result
Migrate remaining use cases
1 parent 1bd14b8 commit 00ba09c

File tree

5 files changed

+107
-105
lines changed

5 files changed

+107
-105
lines changed

app/src/main/java/com/monstarlab/arch/extensions/Result.kt renamed to app/src/main/java/com/monstarlab/arch/extensions/SealedResult.kt

Lines changed: 74 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ import kotlin.contracts.contract
1212
* A discriminated union that encapsulates a successful outcome with a value of type [T]
1313
* or a failure with an arbitrary [Throwable] exception.
1414
*/
15-
sealed class Result<out T> : Serializable {
15+
sealed class SealedResult<out T> : Serializable {
1616

1717
@PublishedApi
18-
internal data class Failure<out T>(val exception: Throwable) : Result<T>()
18+
internal data class Failure<out T>(val exception: Throwable) : SealedResult<T>()
1919

2020
@PublishedApi
21-
internal data class Success<out T>(val value: T) : Result<T>()
21+
internal data class Success<out T>(val value: T) : SealedResult<T>()
2222

2323
/**
2424
* Returns `true` if this instance represents a failed outcome.
@@ -35,8 +35,8 @@ sealed class Result<out T> : Serializable {
3535
get() = this is Success
3636

3737
/**
38-
* Returns the encapsulated value if this instance represents [success][Result.isSuccess] or `null`
39-
* if it is [failure][Result.isFailure].
38+
* Returns the encapsulated value if this instance represents [success][SealedResult.isSuccess] or `null`
39+
* if it is [failure][SealedResult.isFailure].
4040
*
4141
* This function is a shorthand for `getOrElse { null }` (see [getOrElse]) or
4242
* `fold(onSuccess = { it }, onFailure = { null })` (see [fold]).
@@ -58,7 +58,7 @@ sealed class Result<out T> : Serializable {
5858
}
5959

6060
/**
61-
* Returns a string `Success(v)` if this instance represents [success][Result.isSuccess]
61+
* Returns a string `Success(v)` if this instance represents [success][SealedResult.isSuccess]
6262
* where `v` is a string representation of the value or a string `Failure(x)` if
6363
* it is [failure][isFailure] where `x` is a string representation of the exception.
6464
*/
@@ -68,20 +68,20 @@ sealed class Result<out T> : Serializable {
6868
}
6969

7070
/**
71-
* Companion object for [Result] class that contains its constructor functions
71+
* Companion object for [SealedResult] class that contains its constructor functions
7272
* [success] and [failure].
7373
*/
7474
public companion object {
7575

7676
/**
7777
* Returns an instance that encapsulates the given [value] as successful value.
7878
*/
79-
public fun <T> success(value: T): Result<T> = Success(value)
79+
public fun <T> success(value: T): SealedResult<T> = Success(value)
8080

8181
/**
8282
* Returns an instance that encapsulates the given [Throwable] [exception] as failure.
8383
*/
84-
public fun <T> failure(exception: Throwable): Result<T> = Failure(exception)
84+
public fun <T> failure(exception: Throwable): SealedResult<T> = Failure(exception)
8585
}
8686
}
8787

@@ -90,67 +90,67 @@ sealed class Result<out T> : Serializable {
9090
* catching any [Throwable] exception that was thrown from the [block] function execution and encapsulating it as a failure.
9191
*/
9292
@Suppress("FunctionName", "TooGenericExceptionCaught")
93-
public inline fun <T> Result(block: () -> T): Result<T> {
93+
public inline fun <T> SealedResult(block: () -> T): SealedResult<T> {
9494
return try {
95-
Result.success(block())
95+
SealedResult.success(block())
9696
} catch (exception: Throwable) {
97-
Result.failure(exception)
97+
SealedResult.failure(exception)
9898
}
9999
}
100100

101101
/**
102-
* Returns the encapsulated value if this instance represents [success][Result.isSuccess] or throws the encapsulated [Throwable] exception
103-
* if it is [failure][Result.isFailure].
102+
* Returns the encapsulated value if this instance represents [success][SealedResult.isSuccess] or throws the encapsulated [Throwable] exception
103+
* if it is [failure][SealedResult.isFailure].
104104
*
105105
* This function is a shorthand for `getOrElse { throw it }` (see [getOrElse]).
106106
*/
107-
public fun <T> Result<T>.getOrThrow(): T {
107+
public fun <T> SealedResult<T>.getOrThrow(): T {
108108
return when (this) {
109-
is Result.Failure -> throw exception
110-
is Result.Success -> value
109+
is SealedResult.Failure -> throw exception
110+
is SealedResult.Success -> value
111111
}
112112
}
113113

114114
/**
115-
* Returns the encapsulated value if this instance represents [success][Result.isSuccess] or the
116-
* result of [onFailure] function for the encapsulated [Throwable] exception if it is [failure][Result.isFailure].
115+
* Returns the encapsulated value if this instance represents [success][SealedResult.isSuccess] or the
116+
* result of [onFailure] function for the encapsulated [Throwable] exception if it is [failure][SealedResult.isFailure].
117117
*
118118
* Note, that this function rethrows any [Throwable] exception thrown by [onFailure] function.
119119
*
120120
* This function is a shorthand for `fold(onSuccess = { it }, onFailure = onFailure)` (see [fold]).
121121
*/
122122
@OptIn(ExperimentalContracts::class)
123-
public inline fun <R, T : R> Result<T>.getOrElse(onFailure: (exception: Throwable) -> R): R {
123+
public inline fun <R, T : R> SealedResult<T>.getOrElse(onFailure: (exception: Throwable) -> R): R {
124124
contract {
125125
callsInPlace(onFailure, InvocationKind.AT_MOST_ONCE)
126126
}
127127
return when (this) {
128-
is Result.Failure -> onFailure(exception)
129-
is Result.Success -> value
128+
is SealedResult.Failure -> onFailure(exception)
129+
is SealedResult.Success -> value
130130
}
131131
}
132132

133133
/**
134-
* Returns the encapsulated value if this instance represents [success][Result.isSuccess] or the
135-
* [defaultValue] if it is [failure][Result.isFailure].
134+
* Returns the encapsulated value if this instance represents [success][SealedResult.isSuccess] or the
135+
* [defaultValue] if it is [failure][SealedResult.isFailure].
136136
*
137137
* This function is a shorthand for `getOrElse { defaultValue }` (see [getOrElse]).
138138
*/
139-
public fun <R, T : R> Result<T>.getOrDefault(defaultValue: R): R {
139+
public fun <R, T : R> SealedResult<T>.getOrDefault(defaultValue: R): R {
140140
return when (this) {
141-
is Result.Failure -> defaultValue
142-
is Result.Success -> value
141+
is SealedResult.Failure -> defaultValue
142+
is SealedResult.Success -> value
143143
}
144144
}
145145

146146
/**
147-
* Returns the result of [onSuccess] for the encapsulated value if this instance represents [success][Result.isSuccess]
148-
* or the result of [onFailure] function for the encapsulated [Throwable] exception if it is [failure][Result.isFailure].
147+
* Returns the result of [onSuccess] for the encapsulated value if this instance represents [success][SealedResult.isSuccess]
148+
* or the result of [onFailure] function for the encapsulated [Throwable] exception if it is [failure][SealedResult.isFailure].
149149
*
150150
* Note, that this function rethrows any [Throwable] exception thrown by [onSuccess] or by [onFailure] function.
151151
*/
152152
@OptIn(ExperimentalContracts::class)
153-
public inline fun <R, T> Result<T>.fold(
153+
public inline fun <R, T> SealedResult<T>.fold(
154154
onFailure: (exception: Throwable) -> R,
155155
onSuccess: (value: T) -> R
156156
): R {
@@ -159,148 +159,148 @@ public inline fun <R, T> Result<T>.fold(
159159
callsInPlace(onSuccess, InvocationKind.AT_MOST_ONCE)
160160
}
161161
return when (this) {
162-
is Result.Failure -> onFailure(exception)
163-
is Result.Success -> onSuccess(value)
162+
is SealedResult.Failure -> onFailure(exception)
163+
is SealedResult.Success -> onSuccess(value)
164164
}
165165
}
166166

167167
/**
168168
* Returns the encapsulated result of the given [transform] function applied to the encapsulated value
169-
* if this instance represents [success][Result.isSuccess] or the
170-
* original encapsulated [Throwable] exception if it is [failure][Result.isFailure].
169+
* if this instance represents [success][SealedResult.isSuccess] or the
170+
* original encapsulated [Throwable] exception if it is [failure][SealedResult.isFailure].
171171
*
172172
* Note, that this function rethrows any [Throwable] exception thrown by [transform] function.
173173
* See [mapCatching] for an alternative that encapsulates exceptions.
174174
*/
175175
@OptIn(ExperimentalContracts::class)
176-
public inline fun <R, T> Result<T>.map(transform: (value: T) -> R): Result<R> {
176+
public inline fun <R, T> SealedResult<T>.map(transform: (value: T) -> R): SealedResult<R> {
177177
contract {
178178
callsInPlace(transform, InvocationKind.AT_MOST_ONCE)
179179
}
180180
return when (this) {
181-
is Result.Failure -> Result.failure(exception)
182-
is Result.Success -> Result.success(transform(value))
181+
is SealedResult.Failure -> SealedResult.failure(exception)
182+
is SealedResult.Success -> SealedResult.success(transform(value))
183183
}
184184
}
185185

186186
/**
187187
* Returns the encapsulated result of the given [transform] function applied to the encapsulated value
188-
* if this instance represents [success][Result.isSuccess] or the
189-
* original encapsulated [Throwable] exception if it is [failure][Result.isFailure].
188+
* if this instance represents [success][SealedResult.isSuccess] or the
189+
* original encapsulated [Throwable] exception if it is [failure][SealedResult.isFailure].
190190
*
191191
* This function catches any [Throwable] exception thrown by [transform] function and encapsulates it as a failure.
192192
* See [map] for an alternative that rethrows exceptions from `transform` function.
193193
*/
194-
public inline fun <R, T> Result<T>.mapCatching(transform: (value: T) -> R): Result<R> {
194+
public inline fun <R, T> SealedResult<T>.mapCatching(transform: (value: T) -> R): SealedResult<R> {
195195
return when (this) {
196-
is Result.Failure -> Result.failure(exception)
197-
is Result.Success -> Result { transform(value) }
196+
is SealedResult.Failure -> SealedResult.failure(exception)
197+
is SealedResult.Success -> SealedResult { transform(value) }
198198
}
199199
}
200200

201201
/**
202-
* Returns a [Result] of the given [transform] function applied to the encapsulated value
203-
* if this instance represents [success][Result.isSuccess] or the
204-
* original encapsulated [Throwable] exception if it is [failure][Result.isFailure].
202+
* Returns a [SealedResult] of the given [transform] function applied to the encapsulated value
203+
* if this instance represents [success][SealedResult.isSuccess] or the
204+
* original encapsulated [Throwable] exception if it is [failure][SealedResult.isFailure].
205205
*
206206
* Note, that this function rethrows any [Throwable] exception thrown by [transform] function.
207207
* See [flatMapCatching] for an alternative that encapsulates exceptions.
208208
*/
209209
@OptIn(ExperimentalContracts::class)
210-
public inline fun <R, T> Result<T>.flatMap(transform: (value: T) -> Result<R>): Result<R> {
210+
public inline fun <R, T> SealedResult<T>.flatMap(transform: (value: T) -> SealedResult<R>): SealedResult<R> {
211211
contract {
212212
callsInPlace(transform, InvocationKind.AT_MOST_ONCE)
213213
}
214214
return when (this) {
215-
is Result.Failure -> Result.failure(exception)
216-
is Result.Success -> transform(value)
215+
is SealedResult.Failure -> SealedResult.failure(exception)
216+
is SealedResult.Success -> transform(value)
217217
}
218218
}
219219

220220
/**
221-
* Returns a [Result] of the given [transform] function applied to the encapsulated value
222-
* if this instance represents [success][Result.isSuccess] or the
223-
* original encapsulated [Throwable] exception if it is [failure][Result.isFailure].
221+
* Returns a [SealedResult] of the given [transform] function applied to the encapsulated value
222+
* if this instance represents [success][SealedResult.isSuccess] or the
223+
* original encapsulated [Throwable] exception if it is [failure][SealedResult.isFailure].
224224
*
225225
* This function catches any [Throwable] exception thrown by [transform] function and encapsulates it as a failure.
226226
* See [map] for an alternative that rethrows exceptions from `transform` function.
227227
*/
228-
public inline fun <R, T> Result<T>.flatMapCatching(transform: (value: T) -> Result<R>): Result<R> {
228+
public inline fun <R, T> SealedResult<T>.flatMapCatching(transform: (value: T) -> SealedResult<R>): SealedResult<R> {
229229
return when (this) {
230-
is Result.Failure -> Result.failure(exception)
231-
is Result.Success -> Result { transform(value).getOrThrow() }
230+
is SealedResult.Failure -> SealedResult.failure(exception)
231+
is SealedResult.Success -> SealedResult { transform(value).getOrThrow() }
232232
}
233233
}
234234

235235
/**
236236
* Returns the encapsulated result of the given [transform] function applied to the encapsulated [Throwable] exception
237-
* if this instance represents [failure][Result.isFailure] or the
238-
* original encapsulated value if it is [success][Result.isSuccess].
237+
* if this instance represents [failure][SealedResult.isFailure] or the
238+
* original encapsulated value if it is [success][SealedResult.isSuccess].
239239
*
240240
* Note, that this function rethrows any [Throwable] exception thrown by [transform] function.
241241
* See [recoverCatching] for an alternative that encapsulates exceptions.
242242
*/
243243
@OptIn(ExperimentalContracts::class)
244-
public inline fun <R, T : R> Result<T>.recover(transform: (exception: Throwable) -> R): Result<R> {
244+
public inline fun <R, T : R> SealedResult<T>.recover(transform: (exception: Throwable) -> R): SealedResult<R> {
245245
contract {
246246
callsInPlace(transform, InvocationKind.AT_MOST_ONCE)
247247
}
248248
return when (this) {
249-
is Result.Failure -> Result.success(transform(exception))
250-
is Result.Success -> this
249+
is SealedResult.Failure -> SealedResult.success(transform(exception))
250+
is SealedResult.Success -> this
251251
}
252252
}
253253

254254
/**
255255
* Returns the encapsulated result of the given [transform] function applied to the encapsulated [Throwable] exception
256-
* if this instance represents [failure][Result.isFailure] or the
257-
* original encapsulated value if it is [success][Result.isSuccess].
256+
* if this instance represents [failure][SealedResult.isFailure] or the
257+
* original encapsulated value if it is [success][SealedResult.isSuccess].
258258
*
259259
* This function catches any [Throwable] exception thrown by [transform] function and encapsulates it as a failure.
260260
* See [recover] for an alternative that rethrows exceptions.
261261
*/
262-
public inline fun <R, T : R> Result<T>.recoverCatching(transform: (exception: Throwable) -> R): Result<R> {
262+
public inline fun <R, T : R> SealedResult<T>.recoverCatching(transform: (exception: Throwable) -> R): SealedResult<R> {
263263
return when (this) {
264-
is Result.Failure -> Result { transform(exception) }
265-
is Result.Success -> this
264+
is SealedResult.Failure -> SealedResult { transform(exception) }
265+
is SealedResult.Success -> this
266266
}
267267
}
268268

269269
/**
270-
* Performs the given [action] on the encapsulated [Throwable] exception if this instance represents [failure][Result.isFailure].
270+
* Performs the given [action] on the encapsulated [Throwable] exception if this instance represents [failure][SealedResult.isFailure].
271271
* Returns the original `Result` unchanged.
272272
*/
273273
@OptIn(ExperimentalContracts::class)
274-
public inline fun <T> Result<T>.onFailure(action: (exception: Throwable) -> Unit): Result<T> {
274+
public inline fun <T> SealedResult<T>.onFailure(action: (exception: Throwable) -> Unit): SealedResult<T> {
275275
contract {
276276
callsInPlace(action, InvocationKind.AT_MOST_ONCE)
277277
}
278-
if (this is Result.Failure) action(exception)
278+
if (this is SealedResult.Failure) action(exception)
279279
return this
280280
}
281281

282282
/**
283-
* Performs the given [action] on the encapsulated value if this instance represents [success][Result.isSuccess].
283+
* Performs the given [action] on the encapsulated value if this instance represents [success][SealedResult.isSuccess].
284284
* Returns the original `Result` unchanged.
285285
*/
286286
@OptIn(ExperimentalContracts::class)
287-
public inline fun <T> Result<T>.onSuccess(action: (value: T) -> Unit): Result<T> {
287+
public inline fun <T> SealedResult<T>.onSuccess(action: (value: T) -> Unit): SealedResult<T> {
288288
contract {
289289
callsInPlace(action, InvocationKind.AT_MOST_ONCE)
290290
}
291-
if (this is Result.Success) action(value)
291+
if (this is SealedResult.Success) action(value)
292292
return this
293293
}
294294

295295
@OptIn(ExperimentalContracts::class)
296-
fun <T> Flow<Result<T>>.onResult(
296+
fun <T> Flow<SealedResult<T>>.onResult(
297297
onFailure: suspend (exception: Throwable) -> Unit,
298298
onSuccess: suspend (value: T) -> Unit
299-
): Flow<Result<T>> {
299+
): Flow<SealedResult<T>> {
300300
return onEach { result ->
301301
when (result) {
302-
is Result.Failure -> onFailure(result.exception)
303-
is Result.Success -> onSuccess(result.value)
302+
is SealedResult.Failure -> onFailure(result.exception)
303+
is SealedResult.Success -> onSuccess(result.value)
304304
}
305305
}
306306
}
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
package com.monstarlab.core.usecases.blog
22

3-
import com.monstarlab.arch.extensions.UseCaseResult
4-
import com.monstarlab.arch.extensions.safeFlow
3+
import com.monstarlab.arch.extensions.SealedResult
54
import com.monstarlab.core.data.repositories.PostRepository
65
import com.monstarlab.core.domain.model.Post
76
import kotlinx.coroutines.flow.Flow
7+
import kotlinx.coroutines.flow.flow
88
import javax.inject.Inject
99

1010
class GetPostsUseCase @Inject constructor(
1111
private val postRepository: PostRepository
12-
){
13-
fun getPosts(): Flow<UseCaseResult<List<Post>>> = safeFlow {
14-
postRepository.getPosts()
12+
) {
13+
fun getPosts(): Flow<SealedResult<List<Post>>> = flow {
14+
emit(SealedResult { postRepository.getPosts() })
1515
}
1616
}

app/src/main/java/com/monstarlab/core/usecases/resources/GetResourcesUseCase.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.monstarlab.core.usecases.resources
22

3-
import com.monstarlab.arch.extensions.Result
4-
import com.monstarlab.arch.extensions.safeFlow
3+
import com.monstarlab.arch.extensions.SealedResult
54
import com.monstarlab.core.data.repositories.ResourceRepository
65
import kotlinx.coroutines.delay
76
import kotlinx.coroutines.flow.flow
@@ -13,7 +12,7 @@ class GetResourcesUseCase @Inject constructor(
1312

1413
fun getResources() = flow {
1514
delay(2000)
16-
emit(Result { resourceRepository.getResources() })
15+
emit(SealedResult { resourceRepository.getResources() })
1716
}
1817

1918
}

0 commit comments

Comments
 (0)