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!: change unit enums to objects #62

Open
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Open
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 @@ -20,12 +20,31 @@ public interface AreaUnit {
/**
* 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)
Comment on lines +24 to +48
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it might make sense to make all of these getters? Eg.

private val Decimilliare: AreaUnit get() = object : AreaUnit { ... }

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. I generally don't like that pattern because the objects lose their name and and become anonymous objects.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, on second thought -- I think we should pause this until #42 is complete. Each of these being anonymous objects is pretty awkward and this will likely significantly change once the work in the above issue is completed.

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,66 @@ public interface EnergyUnit {
/**
* 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 @@ -19,13 +19,35 @@ public interface ForceUnit {
/**
* 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 @@ -19,30 +19,61 @@ public interface LengthUnit {
/**
* 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 @@ -19,13 +19,35 @@ public interface MassUnit {
/**
* 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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,35 @@ public interface PowerUnit {
/**
* 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<PowerUnit> = listOf(Microwatt, Milliwatt, Watt, Kilowatt, Megawatt, Gigawatt, Terawatt)
}
}
Loading