Skip to content

Commit

Permalink
#41 Added EnumBiMap
Browse files Browse the repository at this point in the history
  • Loading branch information
vityaman committed Apr 11, 2024
1 parent 8a77770 commit ab83b5b
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package ru.itmo.lms.botalka.api.http.message

import ru.itmo.lms.botalka.api.http.PromotionRequestMessage
import ru.itmo.lms.botalka.api.http.PromotionRequestStatusMessage
import ru.itmo.lms.botalka.commons.BiMap
import ru.itmo.lms.botalka.commons.BiMap.Companion.invoke
import ru.itmo.lms.botalka.domain.model.PromotionRequest

fun PromotionRequest.toMessage(): PromotionRequestMessage =
Expand All @@ -12,32 +14,14 @@ fun PromotionRequest.toMessage(): PromotionRequestMessage =
status = this.status.toMessage(),
)

fun PromotionRequest.Status.toMessage(): PromotionRequestStatusMessage =
when (this) {
PromotionRequest.Status.CREATED -> {
PromotionRequestStatusMessage.NEW
}

PromotionRequest.Status.REJECTED -> {
PromotionRequestStatusMessage.CANCELED
}
private val statusMap = BiMap.from(
PromotionRequest.Status.CREATED to PromotionRequestStatusMessage.NEW,
PromotionRequest.Status.REJECTED to PromotionRequestStatusMessage.CANCELED,
PromotionRequest.Status.APPROVED to PromotionRequestStatusMessage.APPROVED,
)

PromotionRequest.Status.APPROVED -> {
PromotionRequestStatusMessage.APPROVED
}
}
fun PromotionRequest.Status.toMessage(): PromotionRequestStatusMessage =
statusMap(this)

fun PromotionRequestStatusMessage.toModel(): PromotionRequest.Status =
when (this) {
PromotionRequestStatusMessage.NEW -> {
PromotionRequest.Status.CREATED
}

PromotionRequestStatusMessage.APPROVED -> {
PromotionRequest.Status.APPROVED
}

PromotionRequestStatusMessage.CANCELED -> {
PromotionRequest.Status.REJECTED
}
}
statusMap(this)
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,10 @@ package ru.itmo.lms.botalka.api.http.message
import ru.itmo.lms.botalka.api.http.UserDraftMessage
import ru.itmo.lms.botalka.api.http.UserMessage
import ru.itmo.lms.botalka.api.http.UserRoleMessage
import ru.itmo.lms.botalka.commons.BiMap
import ru.itmo.lms.botalka.commons.BiMap.Companion.invoke
import ru.itmo.lms.botalka.domain.model.User

fun User.Role.toMessage(): UserRoleMessage =
when (this) {
User.Role.TEACHER -> UserRoleMessage.TEACHER
User.Role.STUDENT -> UserRoleMessage.STUDENT
}

fun UserRoleMessage.toModel(): User.Role =
when (this) {
UserRoleMessage.STUDENT -> User.Role.STUDENT
UserRoleMessage.TEACHER -> User.Role.TEACHER
}

fun User.toMessage(): UserMessage =
UserMessage(
id = this.id.number,
Expand All @@ -40,3 +30,14 @@ fun UserMessage.toModel(): User =
alias = User.Alias(this.alias),
roles = this.roles.map { it.toModel() }.toSet(),
)

private val userRoleMap = BiMap.from(
User.Role.TEACHER to UserRoleMessage.TEACHER,
User.Role.STUDENT to UserRoleMessage.STUDENT,
)

fun User.Role.toMessage(): UserRoleMessage =
userRoleMap(this)

fun UserRoleMessage.toModel(): User.Role =
userRoleMap(this)
33 changes: 33 additions & 0 deletions botalka/src/main/kotlin/ru/itmo/lms/botalka/commons/BiMap.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package ru.itmo.lms.botalka.commons

import java.util.EnumMap

interface BiMap<T, U> {
fun toRight(value: T): U
fun toLeft(value: U): T

companion object {
inline fun <reified T : Enum<T>, reified U : Enum<U>> from(
vararg pairs: Pair<T, U>,
): BiMap<T, U> =
object : BiMap<T, U> {
private val rhs = pairs
.associateTo(EnumMap(T::class.java)) { (l, r) -> l to r }

private val lhs = pairs
.associateTo(EnumMap(U::class.java)) { (l, r) -> r to l }

override fun toRight(value: T): U = rhs[value]!!

override fun toLeft(value: U): T = lhs[value]!!
}

@JvmName("BiMapLeftToRight")
operator fun <T, U> BiMap<T, U>.invoke(value: T): U =
this.toRight(value)

@JvmName("BiMapRightToLeft")
operator fun <T, U> BiMap<T, U>.invoke(value: U): T =
this.toLeft(value)
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package ru.itmo.lms.botalka.storage.jooq.entity

import ru.itmo.lms.botalka.commons.BiMap
import ru.itmo.lms.botalka.commons.BiMap.Companion.invoke
import ru.itmo.lms.botalka.domain.model.PromotionRequest
import ru.itmo.lms.botalka.domain.model.User
import ru.itmo.lms.botalka.storage.jooq.enums.PromotionRequestStatus
Expand All @@ -13,16 +15,14 @@ fun PromotionRequestRecord.toModel(): PromotionRequest =
status = this.status!!.toModel(),
)

private val statusMap = BiMap.from(
PromotionRequest.Status.CREATED to PromotionRequestStatus.created,
PromotionRequest.Status.REJECTED to PromotionRequestStatus.rejected,
PromotionRequest.Status.APPROVED to PromotionRequestStatus.approved,
)

fun PromotionRequestStatus.toModel(): PromotionRequest.Status =
when (this) {
PromotionRequestStatus.created -> PromotionRequest.Status.CREATED
PromotionRequestStatus.rejected -> PromotionRequest.Status.REJECTED
PromotionRequestStatus.approved -> PromotionRequest.Status.APPROVED
}
statusMap(this)

fun PromotionRequest.Status.toRecord(): PromotionRequestStatus =
when (this) {
PromotionRequest.Status.CREATED -> PromotionRequestStatus.created
PromotionRequest.Status.REJECTED -> PromotionRequestStatus.rejected
PromotionRequest.Status.APPROVED -> PromotionRequestStatus.approved
}
statusMap(this)
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package ru.itmo.lms.botalka.storage.jooq.entity

import ru.itmo.lms.botalka.commons.BiMap
import ru.itmo.lms.botalka.commons.BiMap.Companion.invoke
import ru.itmo.lms.botalka.domain.model.User
import ru.itmo.lms.botalka.storage.jooq.enums.Role
import ru.itmo.lms.botalka.storage.jooq.tables.records.UserRecord
Expand All @@ -16,14 +18,13 @@ fun User.Draft.toRecord(): UserRecord =
alias = this.alias.text,
)

private val userRoleMap = BiMap.from(
User.Role.TEACHER to Role.teacher,
User.Role.STUDENT to Role.student,
)

fun User.Role.toRecord(): Role =
when (this) {
User.Role.TEACHER -> Role.teacher
User.Role.STUDENT -> Role.student
}
userRoleMap(this)

fun Role.toModel(): User.Role =
when (this) {
Role.teacher -> User.Role.TEACHER
Role.student -> User.Role.STUDENT
}
userRoleMap(this)

0 comments on commit ab83b5b

Please sign in to comment.