Skip to content

Commit 05b25aa

Browse files
ge0ffreytriceo
authored andcommitted
feat(kotlin): Kotlinify domain classes to remove boilerplate code
1 parent 0f3cea6 commit 05b25aa

File tree

4 files changed

+28
-79
lines changed

4 files changed

+28
-79
lines changed

technology/kotlin-quarkus/src/main/kotlin/org/acme/kotlin/schooltimetabling/domain/Lesson.kt

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,12 @@ import com.fasterxml.jackson.annotation.JsonIdentityReference
77

88

99
@PlanningEntity
10-
class Lesson {
11-
10+
data class Lesson (
1211
@PlanningId
13-
var id: Long? = null
14-
15-
lateinit var subject: String
16-
lateinit var teacher: String
17-
lateinit var studentGroup: String
12+
val id: Long,
13+
val subject: String,
14+
val teacher: String,
15+
val studentGroup: String) {
1816

1917
@JsonIdentityReference
2018
@PlanningVariable
@@ -24,23 +22,15 @@ class Lesson {
2422
@PlanningVariable
2523
var room: Room? = null
2624

27-
// No-arg constructor required for Hibernate and Timefold
28-
constructor()
25+
// No-arg constructor required for Timefold
26+
constructor() : this(0, "", "", "")
2927

30-
constructor(id: Long?, subject: String, teacher: String, studentGroup: String) {
31-
this.id = id
32-
this.subject = subject.trim()
33-
this.teacher = teacher.trim()
34-
this.studentGroup = studentGroup.trim()
35-
}
36-
37-
constructor(id: Long?, subject: String, teacher: String, studentGroup: String, timeslot: Timeslot?, room: Room?)
28+
constructor(id: Long, subject: String, teacher: String, studentGroup: String, timeslot: Timeslot?, room: Room?)
3829
: this(id, subject, teacher, studentGroup) {
3930
this.timeslot = timeslot
4031
this.room = room
4132
}
4233

43-
4434
override fun toString(): String = "$subject($id)"
4535

4636
}

technology/kotlin-quarkus/src/main/kotlin/org/acme/kotlin/schooltimetabling/domain/Room.kt

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,10 @@ import com.fasterxml.jackson.annotation.ObjectIdGenerators
99
generator = ObjectIdGenerators.PropertyGenerator::class,
1010
property = "id"
1111
)
12-
class Room {
13-
12+
data class Room(
1413
@PlanningId
15-
var id: Long? = null
16-
17-
lateinit var name: String
18-
19-
// No-arg constructor required for Hibernate
20-
constructor()
21-
22-
constructor(id: Long?, name: String) {
23-
this.id = id
24-
this.name = name
25-
}
14+
val id: Long,
15+
val name: String) {
2616

2717
override fun toString(): String = name
2818

technology/kotlin-quarkus/src/main/kotlin/org/acme/kotlin/schooltimetabling/domain/Timeslot.kt

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,12 @@ import java.time.LocalTime
1111
generator = ObjectIdGenerators.PropertyGenerator::class,
1212
property = "id"
1313
)
14-
class Timeslot {
15-
14+
data class Timeslot(
1615
@PlanningId
17-
var id: Long? = null
18-
19-
lateinit var dayOfWeek: DayOfWeek
20-
lateinit var startTime: LocalTime
21-
lateinit var endTime: LocalTime
22-
23-
// No-arg constructor required for Hibernate
24-
constructor()
25-
26-
constructor(id: Long?, dayOfWeek: DayOfWeek, startTime: LocalTime, endTime: LocalTime) {
27-
this.id = id
28-
this.dayOfWeek = dayOfWeek
29-
this.startTime = startTime
30-
this.endTime = endTime
31-
}
32-
33-
constructor(id: Long?, dayOfWeek: DayOfWeek, startTime: LocalTime) :
34-
this(id, dayOfWeek, startTime, startTime.plusMinutes(50))
16+
val id: Long,
17+
val dayOfWeek: DayOfWeek,
18+
val startTime: LocalTime,
19+
val endTime: LocalTime = startTime.plusMinutes(50)) {
3520

3621
override fun toString(): String = "$dayOfWeek $startTime"
3722

technology/kotlin-quarkus/src/main/kotlin/org/acme/kotlin/schooltimetabling/domain/Timetable.kt

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,43 +9,27 @@ import ai.timefold.solver.core.api.score.buildin.hardsoft.HardSoftScore
99
import ai.timefold.solver.core.api.solver.SolverStatus
1010

1111
@PlanningSolution
12-
class Timetable {
13-
14-
lateinit var name: String
15-
12+
data class Timetable (
13+
val name: String,
1614
@ProblemFactCollectionProperty
1715
@ValueRangeProvider
18-
lateinit var timeslots: List<Timeslot>
19-
16+
val timeslots: List<Timeslot>,
2017
@ProblemFactCollectionProperty
2118
@ValueRangeProvider
22-
lateinit var rooms: List<Room>
23-
19+
val rooms: List<Room>,
2420
@PlanningEntityCollectionProperty
25-
lateinit var lessons: List<Lesson>
26-
21+
val lessons: List<Lesson>,
2722
@PlanningScore
28-
var score: HardSoftScore? = null
29-
23+
var score: HardSoftScore? = null,
3024
// Ignored by Timefold, used by the UI to display solve or stop solving button
31-
var solverStatus: SolverStatus? = null
25+
var solverStatus: SolverStatus? = null) {
3226

3327
// No-arg constructor required for Timefold
34-
constructor() {}
28+
constructor() : this("", emptyList(), emptyList(), emptyList())
29+
30+
constructor(name: String, score: HardSoftScore?, solverStatus: SolverStatus)
31+
: this(name, emptyList(), emptyList(), emptyList(), score, solverStatus)
3532

36-
constructor(name: String, score: HardSoftScore?, solverStatus: SolverStatus) {
37-
this.name = name
38-
this.score = score
39-
this.solverStatus = solverStatus
40-
this.timeslots = emptyList()
41-
this.rooms = emptyList()
42-
this.lessons = emptyList()
43-
}
33+
override fun toString(): String = name
4434

45-
constructor(name: String, timeslots: List<Timeslot>, rooms: List<Room>, lessons: List<Lesson>) {
46-
this.name = name
47-
this.timeslots = timeslots
48-
this.rooms = rooms
49-
this.lessons = lessons
50-
}
5135
}

0 commit comments

Comments
 (0)