All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.inkapplications.spondee.measures.Power.kt Maven / Gradle / Ivy

There is a newer version: 0.7.0
Show newest version
package inkapplications.spondee.measures

/**
 * Unit of energy per unit time
 */
interface Power {
    /**
     * Express the power in watts.
     */
    val inWatts: Double

    operator fun compareTo(other: Power): Int
    operator fun plus(other: Power): Power
    operator fun minus(other: Power): Power
    operator fun div(other: Number): Power
    operator fun times(other: Number): Power
    operator fun unaryPlus(): Power = this
    operator fun unaryMinus(): Power
}

/**
 * SI Unit of Power.
 */
internal data class Watts(override val inWatts: Double): Power {
    override fun compareTo(other: Power) = inWatts.compareTo(other.inWatts)
    override fun plus(other: Power): Power = Watts(inWatts + other.inWatts)
    override fun minus(other: Power): Power = Watts(inWatts - other.inWatts)
    override fun div(other: Number): Power = Watts(inWatts / other.toDouble())
    override fun times(other: Number): Power = Watts(inWatts * other.toDouble())
    override fun unaryMinus(): Power = Watts(-inWatts)

    override fun toString(): String = "${inWatts}W"
}

/**
 * Express a number as unit of power in Watts.
 */
val Number.watts: Power get() = Watts(toDouble())




© 2015 - 2025 Weber Informatics LLC | Privacy Policy