diff --git a/src/commonMain/kotlin/io/github/kevincianfarini/alchemist/unit/AreaUnit.kt b/src/commonMain/kotlin/io/github/kevincianfarini/alchemist/unit/AreaUnit.kt index fa53ee4..5d1c09f 100644 --- a/src/commonMain/kotlin/io/github/kevincianfarini/alchemist/unit/AreaUnit.kt +++ b/src/commonMain/kotlin/io/github/kevincianfarini/alchemist/unit/AreaUnit.kt @@ -4,7 +4,7 @@ package io.github.kevincianfarini.alchemist.unit /** * A unit of area precise to the millimeter². */ -public interface AreaUnit { +public interface AreaUnit : Comparable { /** * The amount of millimeters² in this unit. Implementations of [AreaUnit] should be perfectly divisible by a @@ -17,15 +17,36 @@ public interface AreaUnit { */ public val symbol: String + override fun compareTo(other: AreaUnit): Int = millimetersSquaredScale.compareTo(other.millimetersSquaredScale) + /** * A non-standard representation of area commonly used as part of the metric system. */ - public enum class Metric(override val symbol: String, override val millimetersSquaredScale: Long) : AreaUnit { - Decimilliare("dma", 100), - Centiare("ca", 1_000_000), - Deciare("da", 10_000_000), - Are("a", 100_000_000), - Decare("daa", 1_000_000_000), - Hectare("ha", 10_000_000_000), + public object Metric { + public val Decimilliare: AreaUnit = object : AreaUnit { + override val symbol: String = "dma" + override val millimetersSquaredScale: Long = 100 + } + public val Centiare: AreaUnit = object : AreaUnit { + override val symbol: String = "ca" + override val millimetersSquaredScale: Long = 1_000_000 + } + public val Deciare: AreaUnit = object : AreaUnit { + override val symbol: String = "da" + override val millimetersSquaredScale: Long = 10_000_000 + } + public val Are: AreaUnit = object : AreaUnit { + override val symbol: String = "a" + override val millimetersSquaredScale: Long = 100_000_000 + } + public val Decare: AreaUnit = object : AreaUnit { + override val symbol: String = "daa" + override val millimetersSquaredScale: Long = 1_000_000_000 + } + public val Hectare: AreaUnit = object : AreaUnit { + override val symbol: String = "ha" + override val millimetersSquaredScale: Long = 10_000_000_000 + } + public val entries: List = listOf(Decimilliare, Centiare, Deciare, Are, Decare, Hectare) } } diff --git a/src/commonMain/kotlin/io/github/kevincianfarini/alchemist/unit/EnergyUnit.kt b/src/commonMain/kotlin/io/github/kevincianfarini/alchemist/unit/EnergyUnit.kt index c700a69..c0a19db 100644 --- a/src/commonMain/kotlin/io/github/kevincianfarini/alchemist/unit/EnergyUnit.kt +++ b/src/commonMain/kotlin/io/github/kevincianfarini/alchemist/unit/EnergyUnit.kt @@ -3,7 +3,7 @@ package io.github.kevincianfarini.alchemist.unit /** * A unit of energy precise to the millijoule. */ -public interface EnergyUnit { +public interface EnergyUnit : Comparable { /** * The amount of millijoules in this unit. Implementations of [EnergyUnit] should be perfectly divisible by a @@ -16,34 +16,71 @@ public interface EnergyUnit { */ public val symbol: String + override fun compareTo(other: EnergyUnit): Int = millijouleScale.compareTo(other.millijouleScale) + /** * An International System of Units standard representation of energy. */ - public enum class International( - override val millijouleScale: Long, - override val symbol: String, - ) : EnergyUnit { - Millijoule(1, "mJ"), - Joule(1_000, "J"), - Kilojoule(1_000_000, "kJ"), - Megajoule(1_000_000_000, "MJ"), - Gigajoule(1_000_000_000_000, "GJ"), - Tetrajoule(1_000_000_000_000_000, "TJ"), - Petajoule(1_000_000_000_000_000_000, "PJ"), + public object International { + public val Millijoule: EnergyUnit = object : EnergyUnit { + override val millijouleScale: Long = 1 + override val symbol: String = "mJ" + } + public val Joule: EnergyUnit = object : EnergyUnit { + override val millijouleScale: Long = 1_000 + override val symbol: String = "J" + } + public val Kilojoule: EnergyUnit = object : EnergyUnit { + override val millijouleScale: Long = 1_000_000 + override val symbol: String = "kJ" + } + public val Megajoule: EnergyUnit = object : EnergyUnit { + override val millijouleScale: Long = 1_000_000_000 + override val symbol: String = "MJ" + } + public val Gigajoule: EnergyUnit = object : EnergyUnit { + override val millijouleScale: Long = 1_000_000_000_000 + override val symbol: String = "GJ" + } + public val Tetrajoule: EnergyUnit = object : EnergyUnit { + override val millijouleScale: Long = 1_000_000_000_000_000 + override val symbol: String = "TJ" + } + public val Petajoule: EnergyUnit = object : EnergyUnit { + override val millijouleScale: Long = 1_000_000_000_000_000_000 + override val symbol: String = "PJ" + } + public val entries: List = listOf(Millijoule, Joule, Kilojoule, Megajoule, Gigajoule, Tetrajoule, Petajoule) } /** * A non-standard representation of energy commonly used to measure electrical energy. */ - public enum class Electricity( - override val millijouleScale: Long, - override val symbol: String, - ) : EnergyUnit { - MilliwattHour(3_600, "mWh"), - WattHour(3_600_000, "Wh"), - KilowattHour(3_600_000_000, "kWh"), - MegawattHour(3_600_000_000_000, "MWh"), - GigawattHour(3_600_000_000_000_000, "GWh"), - TerawattHour(3_600_000_000_000_000_000, "TWh"), + public object Electricity { + public val MilliwattHour: EnergyUnit = object : EnergyUnit { + override val millijouleScale: Long = 3_600 + override val symbol: String = "mWh" + } + public val WattHour: EnergyUnit = object : EnergyUnit { + override val millijouleScale: Long = 3_600_000 + override val symbol: String = "Wh" + } + public val KilowattHour: EnergyUnit = object : EnergyUnit { + override val millijouleScale: Long = 3_600_000_000 + override val symbol: String = "kWh" + } + public val MegawattHour: EnergyUnit = object : EnergyUnit { + override val millijouleScale: Long = 3_600_000_000_000 + override val symbol: String = "MWh" + } + public val GigawattHour: EnergyUnit = object : EnergyUnit { + override val millijouleScale: Long = 3_600_000_000_000_000 + override val symbol: String = "GWh" + } + public val TerawattHour: EnergyUnit = object : EnergyUnit { + override val millijouleScale: Long = 3_600_000_000_000_000_000 + override val symbol: String = "TWh" + } + public val entries: List = listOf(MilliwattHour, WattHour, KilowattHour, MegawattHour, GigawattHour, TerawattHour) } -} \ No newline at end of file +} diff --git a/src/commonMain/kotlin/io/github/kevincianfarini/alchemist/unit/ForceUnit.kt b/src/commonMain/kotlin/io/github/kevincianfarini/alchemist/unit/ForceUnit.kt index 5175003..9669bf9 100644 --- a/src/commonMain/kotlin/io/github/kevincianfarini/alchemist/unit/ForceUnit.kt +++ b/src/commonMain/kotlin/io/github/kevincianfarini/alchemist/unit/ForceUnit.kt @@ -3,7 +3,7 @@ package io.github.kevincianfarini.alchemist.unit /** * A unit of force precise to the nanonewton. */ -public interface ForceUnit { +public interface ForceUnit : Comparable { /** * The symbol of this unit. @@ -16,16 +16,40 @@ public interface ForceUnit { */ public val nanonewtonScale: Long + override fun compareTo(other: ForceUnit): Int = nanonewtonScale.compareTo(other.nanonewtonScale) + /** * An International System of Units standard representation of force. */ - public enum class International(override val symbol: String, override val nanonewtonScale: Long) : ForceUnit { - Nanonewton("nN", 1), - Micronewton("μN", 1_000), - Millinewton("mN", 1_000_000), - Newton("N", 1_000_000_000), - Kilonewton("kN", 1_000_000_000_000), - Meganewton("MN", 1_000_000_000_000_000), - Giganewton("GN", 1_000_000_000_000_000_000) + public object International { + public val Nanonewton: ForceUnit = object : ForceUnit { + override val symbol: String = "nN" + override val nanonewtonScale: Long = 1 + } + public val Micronewton: ForceUnit = object : ForceUnit { + override val symbol: String = "μN" + override val nanonewtonScale: Long = 1_000 + } + public val Millinewton: ForceUnit = object : ForceUnit { + override val symbol: String = "mN" + override val nanonewtonScale: Long = 1_000_000 + } + public val Newton: ForceUnit = object : ForceUnit { + override val symbol: String = "N" + override val nanonewtonScale: Long = 1_000_000_000 + } + public val Kilonewton: ForceUnit = object : ForceUnit { + override val symbol: String = "kN" + override val nanonewtonScale: Long = 1_000_000_000_000 + } + public val Meganewton: ForceUnit = object : ForceUnit { + override val symbol: String = "MN" + override val nanonewtonScale: Long = 1_000_000_000_000_000 + } + public val Giganewton: ForceUnit = object : ForceUnit { + override val symbol: String = "GN" + override val nanonewtonScale: Long = 1_000_000_000_000_000_000 + } + public val entries: List = listOf(Nanonewton, Micronewton, Millinewton, Newton, Kilonewton, Meganewton, Giganewton) } } diff --git a/src/commonMain/kotlin/io/github/kevincianfarini/alchemist/unit/LengthUnit.kt b/src/commonMain/kotlin/io/github/kevincianfarini/alchemist/unit/LengthUnit.kt index fd6c37e..8cdaa9c 100644 --- a/src/commonMain/kotlin/io/github/kevincianfarini/alchemist/unit/LengthUnit.kt +++ b/src/commonMain/kotlin/io/github/kevincianfarini/alchemist/unit/LengthUnit.kt @@ -3,7 +3,7 @@ package io.github.kevincianfarini.alchemist.unit /** * A unit of length precise to the nanometer. */ -public interface LengthUnit { +public interface LengthUnit : Comparable { /** * The amount of nanometers in this unit. Implementations of [LengthUnit] should be perfectly divisible by a @@ -16,33 +16,66 @@ public interface LengthUnit { */ public val symbol: String + override fun compareTo(other: LengthUnit): Int = nanometerScale.compareTo(other.nanometerScale) + /** * An International System of Units standard representation of length. */ - public enum class International( - override val nanometerScale: Long, - override val symbol: String, - ) : LengthUnit { - Nanometer(1, "nm"), - Micrometer(1_000, "μm"), - Millimeter(1_000_000, "mm"), - Centimeter(10_000_000, "cm"), - Meter(1_000_000_000, "m"), - Kilometer(1_000_000_000_000, "km"), - Megameter(1_000_000_000_000_000, "Mm"), - Gigameter(1_000_000_000_000_000_000, "Gm"), + public object International { + public val Nanometer: LengthUnit = object : LengthUnit { + override val nanometerScale: Long = 1 + override val symbol: String = "nm" + } + public val Micrometer: LengthUnit = object : LengthUnit { + override val nanometerScale: Long = 1_000 + override val symbol: String = "μm" + } + public val Millimeter: LengthUnit = object : LengthUnit { + override val nanometerScale: Long = 1_000_000 + override val symbol: String = "mm" + } + public val Centimeter: LengthUnit = object : LengthUnit { + override val nanometerScale: Long = 10_000_000 + override val symbol: String = "cm" + } + public val Meter: LengthUnit = object : LengthUnit { + override val nanometerScale: Long = 1_000_000_000 + override val symbol: String = "m" + } + public val Kilometer: LengthUnit = object : LengthUnit { + override val nanometerScale: Long = 1_000_000_000_000 + override val symbol: String = "km" + } + public val Megameter: LengthUnit = object : LengthUnit { + override val nanometerScale: Long = 1_000_000_000_000_000 + override val symbol: String = "Mm" + } + public val Gigameter: LengthUnit = object : LengthUnit { + override val nanometerScale: Long = 1_000_000_000_000_000_000 + override val symbol: String = "Gm" + } + public val entries: List = listOf(Nanometer, Micrometer, Millimeter, Centimeter, Meter, Kilometer, Megameter, Gigameter) } /** * A non-standard representation of length commonly used in the United States. */ - public enum class UnitedStatesCustomary( - override val nanometerScale: Long, - override val symbol: String, - ) : LengthUnit { - Inch(25_400_000, "in"), - Foot(304_800_000, "ft"), - Yard(914_400_000, "yd"), - Mile(1_609_344_000_000, "mi"), + public object UnitedStatesCustomary { + public val Inch: LengthUnit = object : LengthUnit { + override val nanometerScale: Long = 25_400_000 + override val symbol: String = "in" + } + public val Foot: LengthUnit = object : LengthUnit { + override val nanometerScale: Long = 304_800_000 + override val symbol: String = "ft" + } + public val Yard: LengthUnit = object : LengthUnit { + override val nanometerScale: Long = 914_400_000 + override val symbol: String = "yd" + } + public val Mile: LengthUnit = object : LengthUnit { + override val nanometerScale: Long = 1_609_344_000_000 + override val symbol: String = "mi" + } } } diff --git a/src/commonMain/kotlin/io/github/kevincianfarini/alchemist/unit/MassUnit.kt b/src/commonMain/kotlin/io/github/kevincianfarini/alchemist/unit/MassUnit.kt index 39f1a91..a79ac80 100644 --- a/src/commonMain/kotlin/io/github/kevincianfarini/alchemist/unit/MassUnit.kt +++ b/src/commonMain/kotlin/io/github/kevincianfarini/alchemist/unit/MassUnit.kt @@ -3,7 +3,7 @@ package io.github.kevincianfarini.alchemist.unit /** * A unit of mass precise to the microgram. */ -public interface MassUnit { +public interface MassUnit : Comparable { /** * The amount of micrograms in this unit. Implementations of [MassUnit] should be perfectly divisible by a @@ -16,16 +16,40 @@ public interface MassUnit { */ public val symbol: String + override fun compareTo(other: MassUnit): Int = microgramScale.compareTo(other.microgramScale) + /** * An International System of Units standard representation of mass. */ - public enum class International(override val microgramScale: Long, override val symbol: String): MassUnit { - Microgram(1, "μg"), - Milligram(1_000, "mg"), - Gram(1_000_000, "g"), - Kilogram(1_000_000_000, "kg"), - Megagram(1_000_000_000_000, "Mg"), - Gigagram(1_000_000_000_000_000, "Gg"), - Teragram(1_000_000_000_000_000_000, "Tg"), + public object International { + public val Microgram: MassUnit = object : MassUnit { + override val microgramScale: Long = 1 + override val symbol: String = "μg" + } + public val Milligram: MassUnit = object : MassUnit { + override val microgramScale: Long = 1_000 + override val symbol: String = "mg" + } + public val Gram: MassUnit = object : MassUnit { + override val microgramScale: Long = 1_000_000 + override val symbol: String = "g" + } + public val Kilogram: MassUnit = object : MassUnit { + override val microgramScale: Long = 1_000_000_000 + override val symbol: String = "kg" + } + public val Megagram: MassUnit = object : MassUnit { + override val microgramScale: Long = 1_000_000_000_000 + override val symbol: String = "Mg" + } + public val Gigagram: MassUnit = object : MassUnit { + override val microgramScale: Long = 1_000_000_000_000_000 + override val symbol: String = "Gg" + } + public val Teragram: MassUnit = object : MassUnit { + override val microgramScale: Long = 1_000_000_000_000_000_000 + override val symbol: String = "Tg" + } + public val entries: List = listOf(Microgram, Milligram, Gram, Kilogram, Megagram, Gigagram, Teragram) } } diff --git a/src/commonMain/kotlin/io/github/kevincianfarini/alchemist/unit/PowerUnit.kt b/src/commonMain/kotlin/io/github/kevincianfarini/alchemist/unit/PowerUnit.kt index d49826d..3989576 100644 --- a/src/commonMain/kotlin/io/github/kevincianfarini/alchemist/unit/PowerUnit.kt +++ b/src/commonMain/kotlin/io/github/kevincianfarini/alchemist/unit/PowerUnit.kt @@ -3,7 +3,7 @@ package io.github.kevincianfarini.alchemist.unit /** * A unit of power precise to the microwatt. */ -public interface PowerUnit { +public interface PowerUnit : Comparable { /** * The amount of microwatts in this unit. Implementations of [PowerUnit] should be perfectly divisible by a @@ -16,19 +16,40 @@ public interface PowerUnit { */ public val symbol: String + override fun compareTo(other: PowerUnit): Int = microwattScale.compareTo(other.microwattScale) + /** * An International System of Units standard representation of power. */ - public enum class International( - override val microwattScale: Long, - override val symbol: String, - ) : PowerUnit { - Microwatt(1, "μW"), - Milliwatt(1_000, "mW"), - Watt(1_000_000, "W"), - Kilowatt(1_000_000_000, "kW"), - Megawatt(1_000_000_000_000, "MW"), - Gigawatt(1_000_000_000_000_000, "GW"), - Terawatt(1_000_000_000_000_000_000, "TW"), + public object International { + public val Microwatt: PowerUnit = object : PowerUnit { + override val microwattScale: Long = 1 + override val symbol: String = "μW" + } + public val Milliwatt: PowerUnit = object : PowerUnit { + override val microwattScale: Long = 1_000 + override val symbol: String = "mW" + } + public val Watt: PowerUnit = object : PowerUnit { + override val microwattScale: Long = 1_000_000 + override val symbol: String = "W" + } + public val Kilowatt: PowerUnit = object : PowerUnit { + override val microwattScale: Long = 1_000_000_000 + override val symbol: String = "kW" + } + public val Megawatt: PowerUnit = object : PowerUnit { + override val microwattScale: Long = 1_000_000_000_000 + override val symbol: String = "MW" + } + public val Gigawatt: PowerUnit = object : PowerUnit { + override val microwattScale: Long = 1_000_000_000_000_000 + override val symbol: String = "GW" + } + public val Terawatt: PowerUnit = object : PowerUnit { + override val microwattScale: Long = 1_000_000_000_000_000_000 + override val symbol: String = "TW" + } + public val entries: List = listOf(Microwatt, Milliwatt, Watt, Kilowatt, Megawatt, Gigawatt, Terawatt) } } diff --git a/src/commonMain/kotlin/io/github/kevincianfarini/alchemist/unit/TemperatureUnit.kt b/src/commonMain/kotlin/io/github/kevincianfarini/alchemist/unit/TemperatureUnit.kt index f392c02..41019e1 100644 --- a/src/commonMain/kotlin/io/github/kevincianfarini/alchemist/unit/TemperatureUnit.kt +++ b/src/commonMain/kotlin/io/github/kevincianfarini/alchemist/unit/TemperatureUnit.kt @@ -10,9 +10,9 @@ import kotlin.text.Typography.nbsp */ @RequiresOptIn( message = """ - Implementing TemperatureUnit requires detecting integer overflow detection, which normal Long values don't - expose. Implementors should exercise caution when converting between their custom temperature units and - nanokelvins. + Implementing TemperatureUnit requires detecting integer overflow detection, which normal Long values don't + expose. Implementors should exercise caution when converting between their custom temperature units and + nanokelvins. """, level = RequiresOptIn.Level.ERROR, ) @@ -48,16 +48,19 @@ public interface TemperatureUnit { /** * An International System of Units standard representation of temperature. */ - public enum class International(override val symbol: String) : TemperatureUnit { - Nanokelvin("${nbsp}nK") { + public object International { + public val Nanokelvin: TemperatureUnit = object : TemperatureUnit { + override val symbol: String = "${nbsp}nK" override fun convertToNanokelvin(degrees: Long): Long = degrees override fun convertToNanokelvin(degrees: Double): Long { require(!degrees.isNaN()) { "Temperature value cannot be NaN." } return degrees.roundToLong() } override fun convertNanokelvinsToThis(nanokelvins: Long): Double = nanokelvins.toDouble() - }, - Microkelvin("${nbsp}μK") { + } + + public val Microkelvin: TemperatureUnit = object : TemperatureUnit { + override val symbol: String = "${nbsp}μK" override fun convertToNanokelvin(degrees: Long): Long = (degrees.saturated * 1_000).rawValue override fun convertToNanokelvin(degrees: Double): Long { val ret = degrees * 1_000 @@ -65,8 +68,10 @@ public interface TemperatureUnit { return ret.roundToLong() } override fun convertNanokelvinsToThis(nanokelvins: Long): Double = nanokelvins.saturated.toDouble() / 1_000 - }, - Millikelvin("${nbsp}mK") { + } + + public val Millikelvin: TemperatureUnit = object : TemperatureUnit { + override val symbol: String = "${nbsp}mK" override fun convertToNanokelvin(degrees: Long): Long = (degrees.saturated * 1_000_000).rawValue override fun convertToNanokelvin(degrees: Double): Long { val ret = degrees * 1_000_000 @@ -74,8 +79,10 @@ public interface TemperatureUnit { return ret.roundToLong() } override fun convertNanokelvinsToThis(nanokelvins: Long): Double = nanokelvins.saturated.toDouble() / 1_000_000 - }, - Kelvin("${nbsp}K") { + } + + public val Kelvin: TemperatureUnit = object : TemperatureUnit { + override val symbol: String = "${nbsp}K" override fun convertToNanokelvin(degrees: Long): Long = (degrees.saturated * 1_000_000_000).rawValue override fun convertToNanokelvin(degrees: Double): Long { val ret = degrees * 1_000_000_000 @@ -83,8 +90,10 @@ public interface TemperatureUnit { return ret.roundToLong() } override fun convertNanokelvinsToThis(nanokelvins: Long): Double = nanokelvins.saturated.toDouble() / 1_000_000_000 - }, - Kilokelvin("${nbsp}kK") { + } + + public val Kilokelvin: TemperatureUnit = object : TemperatureUnit { + override val symbol: String = "${nbsp}kK" override fun convertToNanokelvin(degrees: Long): Long = (degrees.saturated * 1_000_000_000_000).rawValue override fun convertToNanokelvin(degrees: Double): Long { val ret = degrees * 1_000_000_000_000 @@ -92,8 +101,10 @@ public interface TemperatureUnit { return ret.roundToLong() } override fun convertNanokelvinsToThis(nanokelvins: Long): Double = nanokelvins.saturated.toDouble() / 1_000_000_000_000 - }, - Megakelvin("${nbsp}MK") { + } + + public val Megakelvin: TemperatureUnit = object : TemperatureUnit { + override val symbol: String = "${nbsp}MK" override fun convertToNanokelvin(degrees: Long): Long = (degrees.saturated * 1_000_000_000_000_000).rawValue override fun convertToNanokelvin(degrees: Double): Long { val ret = degrees * 1_000_000_000_000_000 @@ -101,8 +112,10 @@ public interface TemperatureUnit { return ret.roundToLong() } override fun convertNanokelvinsToThis(nanokelvins: Long): Double = nanokelvins.saturated.toDouble() / 1_000_000_000_000_000 - }, - Gigakelvin("${nbsp}GK") { + } + + public val Gigakelvin: TemperatureUnit = object : TemperatureUnit { + override val symbol: String = "${nbsp}GK" override fun convertToNanokelvin(degrees: Long): Long = (degrees.saturated * 1_000_000_000_000_000_000).rawValue override fun convertToNanokelvin(degrees: Double): Long { val ret = degrees * 1_000_000_000_000_000_000 @@ -110,8 +123,10 @@ public interface TemperatureUnit { return ret.roundToLong() } override fun convertNanokelvinsToThis(nanokelvins: Long): Double = nanokelvins.saturated.toDouble() / 1_000_000_000_000_000_000 - }, - Celsius("°C") { + } + + public val Celsius: TemperatureUnit = object : TemperatureUnit { + override val symbol: String = "°C" override fun convertToNanokelvin(degrees: Long): Long { return ((degrees.saturated * 1_000_000_000) + 273_150_000_000).rawValue } @@ -123,7 +138,17 @@ public interface TemperatureUnit { override fun convertNanokelvinsToThis(nanokelvins: Long): Double { return (nanokelvins.saturated.toDouble() - 273_150_000_000) / 1_000_000_000 } - }, + } + + public val entries: List = listOf( + Nanokelvin, + Microkelvin, + Millikelvin, + Kelvin, + Kilokelvin, + Megakelvin, + Gigakelvin, + ) } /** diff --git a/src/commonMain/kotlin/io/github/kevincianfarini/alchemist/unit/VolumeUnit.kt b/src/commonMain/kotlin/io/github/kevincianfarini/alchemist/unit/VolumeUnit.kt index 3d51155..a4c6eba 100644 --- a/src/commonMain/kotlin/io/github/kevincianfarini/alchemist/unit/VolumeUnit.kt +++ b/src/commonMain/kotlin/io/github/kevincianfarini/alchemist/unit/VolumeUnit.kt @@ -5,7 +5,7 @@ import kotlin.text.Typography.nbsp /** * A unit of volume precise to the centimeter³. */ -public interface VolumeUnit { +public interface VolumeUnit : Comparable { /** * The symbol of this unit. @@ -18,16 +18,40 @@ public interface VolumeUnit { */ public val cubicCentimetersScale: Long + override fun compareTo(other: VolumeUnit): Int = cubicCentimetersScale.compareTo(other.cubicCentimetersScale) + /** * A non-standard representation of volume commonly used as part of the metric system. */ - public enum class Metric(override val symbol: String, override val cubicCentimetersScale: Long) : VolumeUnit { - Milliliter("${nbsp}mL", 1), - Liter("${nbsp}L", 1_000), - Kiloliter("${nbsp}kL", 1_000_000), - Megaliter("${nbsp}ML", 1_000_000_000), - Gigaliter("${nbsp}GL", 1_000_000_000_000), - Teraliter("${nbsp}TL", 1_000_000_000_000_000), - Petaliter("${nbsp}PL", 1_000_000_000_000_000_000), + public object Metric { + public val Milliliter: VolumeUnit = object : VolumeUnit { + override val symbol: String = "${nbsp}mL" + override val cubicCentimetersScale: Long = 1 + } + public val Liter: VolumeUnit = object : VolumeUnit { + override val symbol: String = "${nbsp}L" + override val cubicCentimetersScale: Long = 1_000 + } + public val Kiloliter: VolumeUnit = object : VolumeUnit { + override val symbol: String = "${nbsp}kL" + override val cubicCentimetersScale: Long = 1_000_000 + } + public val Megaliter: VolumeUnit = object : VolumeUnit { + override val symbol: String = "${nbsp}ML" + override val cubicCentimetersScale: Long = 1_000_000_000 + } + public val Gigaliter: VolumeUnit = object : VolumeUnit { + override val symbol: String = "${nbsp}GL" + override val cubicCentimetersScale: Long = 1_000_000_000_000 + } + public val Teraliter: VolumeUnit = object : VolumeUnit { + override val symbol: String = "${nbsp}TL" + override val cubicCentimetersScale: Long = 1_000_000_000_000_000 + } + public val Petaliter: VolumeUnit = object : VolumeUnit { + override val symbol: String = "${nbsp}PL" + override val cubicCentimetersScale: Long = 1_000_000_000_000_000_000 + } + public val entries: List = listOf(Milliliter, Liter, Kiloliter, Megaliter, Gigaliter, Teraliter, Petaliter) } }