Skip to content

Commit

Permalink
WTA #71: Updated SecondaryStats implementations to be wrapped in seal…
Browse files Browse the repository at this point in the history
…ed class and use data class with private constructors.
  • Loading branch information
Jacob3075 committed Dec 28, 2022
1 parent 1cadfc6 commit 0860991
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@ data class Editor(override val name: String, override val time: Time) : Secondar
override fun copyStat(name: String, time: Time) = copy(name = name, time = time)
}

class Editors(values: List<Editor>) : SecondaryStats<Editor> {
override val values: List<Editor> = values.mergeDuplicates(::Editor)
sealed class Editors : SecondaryStats<Editor> {
private data class EditorsHolder(override val values: List<Editor>) : Editors()

override fun plus(other: SecondaryStats<Editor>) = Editors(values + other.values)
override fun plus(other: SecondaryStats<Editor>) = from(values + other.values)

override fun copyStats(values: List<Editor>) = Editors(values)
override fun copyStats(values: List<Editor>) = from(values)

companion object {
val NONE = Editors(emptyList())
fun from(values: List<Editor>): Editors =
EditorsHolder(values.mergeDuplicates(::Editor))

val NONE = from(emptyList())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,21 @@ data class Language(override val name: String, override val time: Time) : Second
override fun copyStat(name: String, time: Time) = copy(name = name, time = time)
}

class Languages(values: List<Language>) : SecondaryStats<Language> {
override val values: List<Language> = values.mergeDuplicates(::Language)
/**
* Need to wrap data class in a sealed class to be able to use a data class with private constructor
*
* [Source](https://youtrack.jetbrains.com/issue/KT-11914/Confusing-data-class-copy-with-private-constructor)
*/
sealed class Languages : SecondaryStats<Language> {
private data class LanguagesHolder(override val values: List<Language>) : Languages()

override fun plus(other: SecondaryStats<Language>) = Languages(values + other.values)

override fun copyStats(values: List<Language>) = Languages(values = values)
override fun plus(other: SecondaryStats<Language>) = from(values + other.values)
override fun copyStats(values: List<Language>) = from(values = values)

companion object {
val NONE = Languages(emptyList())
fun from(values: List<Language>): Languages =
LanguagesHolder(values.mergeDuplicates(::Language))

val NONE = from(emptyList())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,19 @@ data class OperatingSystem(override val name: String, override val time: Time) :
override fun copyStat(name: String, time: Time) = copy(name = name, time = time)
}

class OperatingSystems(values: List<OperatingSystem>) : SecondaryStats<OperatingSystem> {
override val values: List<OperatingSystem> = values.mergeDuplicates(::OperatingSystem)
sealed class OperatingSystems : SecondaryStats<OperatingSystem> {
private data class OperatingSystemsHolder(override val values: List<OperatingSystem>) :
OperatingSystems()

override fun plus(other: SecondaryStats<OperatingSystem>) =
OperatingSystems(values + other.values)
from(values + other.values)

override fun copyStats(values: List<OperatingSystem>) = OperatingSystems(values)
override fun copyStats(values: List<OperatingSystem>) = from(values)

companion object {
val NONE = OperatingSystems(emptyList())
fun from(values: List<OperatingSystem>): OperatingSystems =
OperatingSystemsHolder(values.mergeDuplicates(::OperatingSystem))

val NONE = from(emptyList())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ internal class SecondaryStatsRobot {
private lateinit var model: SecondaryStats<Language>

fun createModel(list: List<Language>) = apply {
model = Languages(list)
model = Languages.from(list)
}

fun resultValuesShouldBe(emptyList: List<Language>) = apply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ internal class SecondaryStatsTest {
@Test
internal fun `when adding 2 models without duplicate stats, then values should be added without merging`() {
modelRobot.createModel(LANGUAGES)
.addModel(Languages(LANGUAGES_ALT))
.addModel(Languages.from(LANGUAGES_ALT))
.resultValuesShouldBe(LANGUAGES + LANGUAGES_ALT)
}

@Test
internal fun `when adding 2 models with duplicate stats, then values should be merged and added`() {
modelRobot.createModel(LANGUAGES)
.addModel(Languages(LANGUAGES + LANGUAGES_ALT))
.addModel(Languages.from(LANGUAGES + LANGUAGES_ALT))
.resultValuesShouldBe(
LANGUAGES.map { it.copy(time = it.time * 2) } + LANGUAGES_ALT,
)
Expand Down

0 comments on commit 0860991

Please sign in to comment.