Skip to content

generics and subtypes #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 30 additions & 7 deletions src/main/kotlin/ru/kotlin/homework/network/NetworkLogger.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,35 @@ sealed class ApiException(message: String) : Throwable(message) {
data object UnknownException: ApiException("Unknown exception")
}

class ErrorLogger<E : Throwable> {
interface Logger<in E: Throwable> {
fun log(response: NetworkResponse<*, E>)
fun dumpLog()
}

interface ErrorDumper<out E: Throwable> {
fun dump(): List<Pair<LocalDateTime, E>>
}

val errors = mutableListOf<Pair<LocalDateTime, E>>()
class ErrorLogger<E : Throwable>: Logger<E>, ErrorDumper<E> {

fun log(response: NetworkResponse<*, E>) {
private val errors = mutableListOf<Pair<LocalDateTime, E>>()

override fun log(response: NetworkResponse<*, E>) {
if (response is Failure) {
errors.add(response.responseDateTime to response.error)
}
}

fun dumpLog() {
override fun dumpLog() {
errors.forEach { (date, error) ->
println("Error at $date: ${error.message}")
}
}

override fun dump(): List<Pair<LocalDateTime, E>> = errors.toList()
}

fun processThrowables(logger: ErrorLogger<Throwable>) {
fun processThrowables(logger: Logger<Throwable>) {
logger.log(Success("Success"))
Thread.sleep(100)
logger.log(Success(Circle))
Expand All @@ -42,16 +53,25 @@ fun processThrowables(logger: ErrorLogger<Throwable>) {
logger.dumpLog()
}

fun processApiErrors(apiExceptionLogger: ErrorLogger<ApiException>) {
fun processApiErrors(apiExceptionLogger: Logger<ApiException>) {
apiExceptionLogger.log(Success("Success"))
Thread.sleep(100)
apiExceptionLogger.log(Success(Circle))
Thread.sleep(100)
apiExceptionLogger.log(Failure(ApiException.NetworkException))

val f = Failure(ApiException.NetworkException)
apiExceptionLogger.log(f)

apiExceptionLogger.dumpLog()
}

fun dumpErrors(logger: ErrorDumper<Throwable>) {
logger.dump().forEach { (date, error) ->
println("Error at $date: ${error.message}")
}
}


fun main() {
val logger = ErrorLogger<Throwable>()

Expand All @@ -60,5 +80,8 @@ fun main() {

println("Processing Api:")
processApiErrors(logger)

println("Dump all errors:")
dumpErrors(logger)
}

6 changes: 3 additions & 3 deletions src/main/kotlin/ru/kotlin/homework/network/NetworkResponse.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@ import java.time.LocalDateTime
/**
* Network result
*/
sealed class NetworkResponse<T, R> {
sealed class NetworkResponse<out T, out R> {
val responseDateTime: LocalDateTime = LocalDateTime.now()
}

/**
* Network success
*/
data class Success<T, R>(val resp: T): NetworkResponse<T, R>()
data class Success<out T>(val resp: T): NetworkResponse<T, Nothing>()

/**
* Network error
*/
data class Failure<T, R>(val error: R): NetworkResponse<T, R>()
data class Failure<R: Throwable>(val error: R): NetworkResponse<Nothing, R>()

val s1 = Success("Message")
val r11: NetworkResponse<String, Error> = s1
Expand Down