Advertisement
Kostiggig

Untitled

May 9th, 2022
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 3.06 KB | None | 0 0
  1. interface Mapper<T,E> {
  2.      fun map(name: String, age: Int) : T
  3.  
  4.     fun map(error: E) : T
  5. }
  6.  
  7. interface User<E> {
  8.     fun <T> map(mapper: Mapper<T, E>) : T
  9. }
  10.  
  11. sealed class UserData : User<Exception> {
  12.     class Success(
  13.         private val name: String,
  14.         private val age: Int
  15.     ) : UserDomain() {
  16.         override fun <T> map(mapper: Mapper<T, Exception>) : T {
  17.             return mapper.map(name,age)
  18.         }
  19.     }
  20.  
  21.     class Failure(
  22.         private val dataException: Exception
  23.     ) : UserDomain() {
  24.         override fun <T> map(mapper: Mapper<T, Exception>) : T {
  25.             return mapper.map(dataException)
  26.         }
  27.     }
  28. }
  29.  
  30. sealed class UserDomain : User<IOException> {
  31.     class Success(
  32.         private val name: String,
  33.         private val age: Int
  34.     ) : UserDomain() {
  35.         override fun <T> map(mapper: Mapper<T, IOException>) : T {
  36.             return mapper.map(name,age)
  37.         }
  38.     }
  39.  
  40.     class Failure(
  41.         private val domainException: IOException
  42.     ) : UserDomain() {
  43.         override fun <T> map(mapper: Mapper<T, IOException>) : T {
  44.             return mapper.map(errorMessage)
  45.         }
  46.     }
  47. }
  48.  
  49. class DataToDomainUserMapper(
  50.     private val mapper: ExceptionToDomainExceptionMapper
  51. ) : Mapper< UserDomain,Exception> {
  52.    
  53.     override fun map(name: String, age: Int) : UserDomain {
  54.         return UserDomain.Success(name, age)
  55.     }
  56.  
  57.     override fun map(error: Exception) : UserDomain {
  58.         val domainException = mapper.map(error)
  59.         return UserDomain.Failure(domainException)
  60.     }
  61. }
  62.  
  63. sealed class UserUI : User<String> {
  64.  
  65.     class Success(
  66.         private val name: String,
  67.         private val age: Int
  68.     ) : UserUI() {
  69.         override fun <T> map(mapper: Mapper<T,String>>) : T {
  70.             return mapper.map(name,age)
  71.         }
  72.     }
  73.  
  74.     class Failure(
  75.         private val errorMessage: String
  76.     ) : UserUI() {
  77.         override fun <T> map(mapper: Mapper<T,String>) : T {
  78.             return mapper.map(errorMessage)
  79.         }
  80.     }
  81. }
  82.  
  83. class DomainToUiUserMapper(
  84.     private val mapper: DomainExceptionToStringMessageMapper
  85. ) : Mapper< UserDomain,IOException> {
  86.    
  87.     override fun map(name: String, age: Int) : UserDomain {
  88.         return UserDomain.Success(name, age)
  89.     }
  90.  
  91.     override fun map(error: IOException) : UserDomain {
  92.         val errorMessage = mapper.map(error)
  93.         return UserUI.Failure(domainException)
  94.     }
  95. }
  96.  
  97. class UserUiToUserViewMapper : Mapper<UserView,String> {
  98.  
  99.     override fun map(name: String, age: Int) : UserView {
  100.         return UserView(name, age)
  101.     }
  102.  
  103.     override fun map(error: String) : UserView {
  104.         // handle failure
  105.     }
  106. }
  107.  
  108. class UserEmployedUiToUserEmployedViewMapper : Mapper<UserEmployedView,String> {
  109.  
  110.     override fun map(name: String, age: Int) : UserEmployedView {
  111.         return UserEmployedView(name, age)
  112.     }
  113.  
  114.     override fun map(error: String) : UserEmployedView {
  115.         // handle failure
  116.     }
  117. }
  118.  
  119. val dataUser = UserData.Success("Vasya",33)
  120. val domainMapper = DataToDomainUserMapper(...)
  121. val domainUser = dataUser.map(domainMapper)
  122.  
  123. val uiMapper = DomainToUiUserMapper(...)
  124. val uiUser = domainUser.(uiMapper)
  125.  
  126. val userViewMapper = UserUiToUserViewMapper()
  127. val userEmployedViewMapper = UserEmployedUiToUserEmployedViewMapper()
  128.  
  129. val userView = uiUser.map(userViewMapper)
  130. val employedView = uiUser.map(userEmployedViewMapper)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement