Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- interface Mapper<T,E> {
- fun map(name: String, age: Int) : T
- fun map(error: E) : T
- }
- interface User<E> {
- fun <T> map(mapper: Mapper<T, E>) : T
- }
- sealed class UserData : User<Exception> {
- class Success(
- private val name: String,
- private val age: Int
- ) : UserDomain() {
- override fun <T> map(mapper: Mapper<T, Exception>) : T {
- return mapper.map(name,age)
- }
- }
- class Failure(
- private val dataException: Exception
- ) : UserDomain() {
- override fun <T> map(mapper: Mapper<T, Exception>) : T {
- return mapper.map(dataException)
- }
- }
- }
- sealed class UserDomain : User<IOException> {
- class Success(
- private val name: String,
- private val age: Int
- ) : UserDomain() {
- override fun <T> map(mapper: Mapper<T, IOException>) : T {
- return mapper.map(name,age)
- }
- }
- class Failure(
- private val domainException: IOException
- ) : UserDomain() {
- override fun <T> map(mapper: Mapper<T, IOException>) : T {
- return mapper.map(errorMessage)
- }
- }
- }
- class DataToDomainUserMapper(
- private val mapper: ExceptionToDomainExceptionMapper
- ) : Mapper< UserDomain,Exception> {
- override fun map(name: String, age: Int) : UserDomain {
- return UserDomain.Success(name, age)
- }
- override fun map(error: Exception) : UserDomain {
- val domainException = mapper.map(error)
- return UserDomain.Failure(domainException)
- }
- }
- sealed class UserUI : User<String> {
- class Success(
- private val name: String,
- private val age: Int
- ) : UserUI() {
- override fun <T> map(mapper: Mapper<T,String>>) : T {
- return mapper.map(name,age)
- }
- }
- class Failure(
- private val errorMessage: String
- ) : UserUI() {
- override fun <T> map(mapper: Mapper<T,String>) : T {
- return mapper.map(errorMessage)
- }
- }
- }
- class DomainToUiUserMapper(
- private val mapper: DomainExceptionToStringMessageMapper
- ) : Mapper< UserDomain,IOException> {
- override fun map(name: String, age: Int) : UserDomain {
- return UserDomain.Success(name, age)
- }
- override fun map(error: IOException) : UserDomain {
- val errorMessage = mapper.map(error)
- return UserUI.Failure(domainException)
- }
- }
- class UserUiToUserViewMapper : Mapper<UserView,String> {
- override fun map(name: String, age: Int) : UserView {
- return UserView(name, age)
- }
- override fun map(error: String) : UserView {
- // handle failure
- }
- }
- class UserEmployedUiToUserEmployedViewMapper : Mapper<UserEmployedView,String> {
- override fun map(name: String, age: Int) : UserEmployedView {
- return UserEmployedView(name, age)
- }
- override fun map(error: String) : UserEmployedView {
- // handle failure
- }
- }
- val dataUser = UserData.Success("Vasya",33)
- val domainMapper = DataToDomainUserMapper(...)
- val domainUser = dataUser.map(domainMapper)
- val uiMapper = DomainToUiUserMapper(...)
- val uiUser = domainUser.(uiMapper)
- val userViewMapper = UserUiToUserViewMapper()
- val userEmployedViewMapper = UserEmployedUiToUserEmployedViewMapper()
- val userView = uiUser.map(userViewMapper)
- val employedView = uiUser.map(userEmployedViewMapper)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement