Skip to content

Commit

Permalink
WTA #71: Cleaned up SecondaryStats.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacob3075 committed Dec 28, 2022
1 parent 211d1d5 commit 1cadfc6
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.jacob.wakatimeapp.core.models.secondarystats

import com.jacob.wakatimeapp.core.models.Time
import com.jacob.wakatimeapp.core.models.secondarystats.SecondaryStats.Companion.mergeDuplicates

data class Editor(override val name: String, override val time: Time) : SecondaryStat<Editor> {

Expand All @@ -9,11 +10,12 @@ 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>(values.mergeDuplicates(::Editor)) {
class Editors(values: List<Editor>) : SecondaryStats<Editor> {
override val values: List<Editor> = values.mergeDuplicates(::Editor)

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

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

companion object {
val NONE = Editors(emptyList())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
package com.jacob.wakatimeapp.core.models.secondarystats

import com.jacob.wakatimeapp.core.models.Time
import com.jacob.wakatimeapp.core.models.secondarystats.SecondaryStats.Companion.mergeDuplicates

data class Language(override val name: String, override val time: Time) : SecondaryStat<Language> {
constructor(entry: Map.Entry<String, Time>) : this(entry.key, entry.value)

override fun copyStat(name: String, time: Time) = copy(name = name, time = time)
}

class Languages(values: List<Language>) :
SecondaryStats<Language>(values.mergeDuplicates(::Language)) {
class Languages(values: List<Language>) : SecondaryStats<Language> {
override val values: List<Language> = values.mergeDuplicates(::Language)

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

override fun copy(values: List<Language>) = Languages(values)
override fun copyStats(values: List<Language>) = Languages(values = values)

companion object {
val NONE = Languages(emptyList())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.jacob.wakatimeapp.core.models.secondarystats

import com.jacob.wakatimeapp.core.models.Time
import com.jacob.wakatimeapp.core.models.secondarystats.SecondaryStats.Companion.mergeDuplicates

data class OperatingSystem(override val name: String, override val time: Time) :
SecondaryStat<OperatingSystem> {
Expand All @@ -9,13 +10,13 @@ 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>(values.mergeDuplicates(::OperatingSystem)) {
class OperatingSystems(values: List<OperatingSystem>) : SecondaryStats<OperatingSystem> {
override val values: List<OperatingSystem> = values.mergeDuplicates(::OperatingSystem)

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

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

companion object {
val NONE = OperatingSystems(emptyList())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.jacob.wakatimeapp.core.models.secondarystats
import com.jacob.wakatimeapp.core.models.Time

/**
* Need to add generics to [SecondaryStat] to be able to use [uncheckedPlus] function without casting
* Need to add generics to [SecondaryStat] to be able to still have correct subtypes
*
* [Source](https://stackoverflow.com/a/47796513/13181948)
*/
Expand All @@ -14,12 +14,13 @@ interface SecondaryStat<T : SecondaryStat<T>> {
fun copyStat(name: String = this.name, time: Time = this.time): T
}

abstract class SecondaryStats<T : SecondaryStat<T>>(val values: List<T>) {
interface SecondaryStats<T : SecondaryStat<T>> {
val values: List<T>
val mostUsed get() = values.maxBy { it.time.totalSeconds }

abstract operator fun plus(other: SecondaryStats<T>): SecondaryStats<T>
operator fun plus(other: SecondaryStats<T>): SecondaryStats<T>

abstract fun copy(values: List<T> = this.values): SecondaryStats<T>
fun copyStats(values: List<T> = this.values): SecondaryStats<T>

fun topNAndCombineOthers(n: Int): SecondaryStats<T> {
if (values.size <= n) return this
Expand All @@ -29,13 +30,11 @@ abstract class SecondaryStats<T : SecondaryStat<T>>(val values: List<T>) {
.reduce(::reducer)
.copyStat(name = "Others")

return copy(values = topN + others)
return copyStats(values = topN + others)
}

private fun reducer(acc: T, other: T) = acc.copyStat(time = acc.time + other.time)

override fun toString() = "${javaClass.simpleName}(values=$values)"

companion object {
@JvmStatic
fun <T : SecondaryStat<T>> Iterable<T>.mergeDuplicates(
Expand Down

0 comments on commit 1cadfc6

Please sign in to comment.