Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement Comparable on all units except TemperatureUnit #63

Draft
wants to merge 3 commits into
base: trunk
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<AreaUnit> {

/**
* The amount of millimeters² in this unit. Implementations of [AreaUnit] should be perfectly divisible by a
Expand All @@ -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<AreaUnit> = listOf(Decimilliare, Centiare, Deciare, Are, Decare, Hectare)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<EnergyUnit> {

/**
* The amount of millijoules in this unit. Implementations of [EnergyUnit] should be perfectly divisible by a
Expand All @@ -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<EnergyUnit> = 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<EnergyUnit> = listOf(MilliwattHour, WattHour, KilowattHour, MegawattHour, GigawattHour, TerawattHour)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<ForceUnit> {

/**
* The symbol of this unit.
Expand All @@ -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<ForceUnit> = listOf(Nanonewton, Micronewton, Millinewton, Newton, Kilonewton, Meganewton, Giganewton)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<LengthUnit> {

/**
* The amount of nanometers in this unit. Implementations of [LengthUnit] should be perfectly divisible by a
Expand All @@ -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<LengthUnit> = 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"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<MassUnit> {

/**
* The amount of micrograms in this unit. Implementations of [MassUnit] should be perfectly divisible by a
Expand All @@ -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<MassUnit> = listOf(Microgram, Milligram, Gram, Kilogram, Megagram, Gigagram, Teragram)
}
}
Loading