Skip to content

Commit

Permalink
Cleanup repetitive code
Browse files Browse the repository at this point in the history
  • Loading branch information
kunalsheth committed Nov 4, 2018
1 parent 2b9a3f1 commit bd4451d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
22 changes: 12 additions & 10 deletions plugin/src/main/kotlin/info/kunalsheth/units/Source.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ fun writeBase(printWriter: PrintWriter) = ::UnitsOfMeasurePlugin::class.java

private const val underlying = "underlying"
private const val siValue = "siValue"
private fun quan(d: Dimension) = "Quan<$d>"

fun Dimension.src(relations: Set<Relation>, quantities: Set<Quantity>, units: Set<UnitOfMeasure>): String {
return """
typealias $this = $safeName
/*inline*/ class $safeName(internal val $underlying: Double) : Quan<$this> {
override val siValue get() = $underlying
override val abrev get() = "$abbreviation"
/*inline*/ class $safeName(internal val $underlying: Double) : ${quan(this)} {
override val $siValue /*get()*/ = $underlying
override val abrev /*get()*/ = "$abbreviation"
override fun new($siValue: Double) = $this($siValue)
Expand All @@ -34,8 +35,8 @@ typealias $this = $safeName
override operator fun plus(that: $this) = $this(this.$underlying + that.$underlying)
override operator fun minus(that: $this) = $this(this.$underlying - that.$underlying)
override operator fun times(that: Number) = $this(this.$underlying * that.toDouble())
override operator fun div(that: Number) = $this(this.$underlying / that.toDouble())
override operator fun times(that: Number) = $this(this.$underlying * that.d)
override operator fun div(that: Number) = $this(this.$underlying / that.d)
override operator fun rem(that: $this) = $this(this.$underlying % that.$underlying)
override infix fun min(that: $this) = if (this < that) this else that
Expand All @@ -49,6 +50,7 @@ typealias $this = $safeName
override fun compareTo(other: $this) = this.$underlying.compareTo(other.$underlying)
override fun toString() = "${'$'}$underlying ${'$'}abrev"
override fun equals(other: Any?) = other is $this && this.siValue == other.siValue
}
${units.joinToString(separator = "") {
it.src(quantities
Expand All @@ -69,22 +71,22 @@ private fun Relation.src(): String {
val logic = "$result(this.$siValue / that.$siValue)"
"""
${jvmName("generic")}
operator fun $a.div(that: Quan<$b>) = $logic
operator fun $a.div(that: ${quan(b)}) = $logic
// ${jvmName("concrete")}
operator fun $a.div(that: $b) = $logic
${jvmName("nonextension")}
fun div(thiz: Quan<$a>, that: Quan<$b>) = thiz.run { $logic }
fun div(thiz: ${quan(a)}, that: ${quan(b)}) = thiz.run { $logic }
""".trimIndent()
}
Multiply -> {
val logic = "$result(this.$siValue * that.$siValue)"
"""
${jvmName("generic")}
operator fun $a.times(that: Quan<$b>) = $logic
operator fun $a.times(that: ${quan(b)}) = $logic
// ${jvmName("concrete")}
operator fun $a.times(that: $b) = $logic
${jvmName("nonextension")}
fun times(thiz: Quan<$a>, that: Quan<$b>) = thiz.run { $logic }
fun times(thiz: ${quan(a)}, that: ${quan(b)}) = thiz.run { $logic }
""".trimIndent()
}
}
Expand All @@ -98,7 +100,7 @@ private fun UnitOfMeasure.src(quantity: Quantity?) = """
val Number.$this: ${quantity ?: dimension} get() = $dimension(d * $factorToSI)
val $dimension.$this get() = $siValue * ${1 / factorToSI}
object $this : UomConverter<$dimension>,
Quan<$dimension> by box($dimension($factorToSI)) {
${quan(dimension)} by box($dimension($factorToSI)) {
override val unitName = "$name"
override fun invoke(x: Number) = x.$this
override fun invoke(x: $dimension) = x.$this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,14 @@ operator fun <Q : Quan<Q>> Q.rangeTo(that: Q) = object : ClosedRange<Q> {
}

fun <Q : Quan<Q>> avg(a: Q, b: Q) = (a + b) / 2
fun <Q : Quan<Q>> avg(a: Q, b: Q, c: Q) = (a + b + c) / 3
fun <Q : Quan<Q>> avg(a: Q, b: Q, c: Q, d: Q) = (a + b + c + d) / 4
fun <Q : Quan<Q>> avg(first: Q, vararg x: Q) = first.new(
(first.siValue + x.sumByDouble { it.siValue }) / (x.size + 1)
(first.siValue + x.sumByDouble(Quan<Q>::siValue)) /
(1 + x.size)
)

private val Number.d get() = toDouble()

operator fun <Q : Quan<Q>> Number.times(that: Quan<Q>): Q = that * this
// operator fun <Q : Quan<Q>> Number.div(that: Quan<Q>): Q = need some sort of reciprocal op

fun <Q : Quan<Q>> Number.exa(f: UomConverter<Q>) = f(d * 1E18)
fun <Q : Quan<Q>> Q.exa(f: UomConverter<Q>) = f(this) * 1E-18
Expand Down

0 comments on commit bd4451d

Please sign in to comment.