calculator: data-ify unit registry into compact rows
- Commit
53cfd57262883351a700e6a677ec7e7c8d4a9ad8- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-01 19:17 UTC (4 days ago)
- Parents
5c88121
Replace the per-category builder functions and the 132 named factor constants in Units.kt / UnitsBasicCategories.kt with one compact row table. A small Row/cat helper in UnitRegistry now builds each UnitCategory from single-line r() rows carrying the token, symbol, display name, factor to the coherent base unit, affine offsetK for temperatures, and parser aliases. Factors keep their original expressions (e.g. 9.4607304725808e15, 1.0 / 3.6, PI / 180.0). The Length/Mass/Time builders previously split into UnitsBasicCategories.kt fold into the same table in their original concatenation order, so that file is deleted. Behaviour is identical: the same units, factors, categories and ordering are preserved. This was proved before committing by extracting the catalogue (category order, inEquations flag, and per-unit token/symbol/name/dimension/factor/offset/aliases, in order) from HEAD and from the new source with a standalone parser and comparing the two dumps exactly: 20 categories and 152 units, equal. Currency handling is untouched. Measured with tools/structure_check.py lines: the two registry files go from 825 lines (581 + 244) to 354, a saving of 471 lines; the repo total drops from 1,648,914 to 1,648,443. calculator/AGENTS.md needed no change: it lists Units.kt, which still exists with the same role, and never listed the deleted file. Verified: ./gradlew :calculator:testDevUnitTest :calculator:lint green; :calculator:detekt green; :calculator:ktlintCheck shows no findings in Units.kt; tools/structure_check.py lines passes (pre-existing exceptions drift in communicate/ is unchanged).
Diff
calculator/src/main/java/com/vayunmathur/calculator/domain/BasicUnitCategories.kt
diff --git a/calculator/src/main/java/com/vayunmathur/calculator/domain/BasicUnitCategories.kt b/calculator/src/main/java/com/vayunmathur/calculator/domain/BasicUnitCategories.ktdeleted file mode 100644index a9a1b0a10f..0000000000--- a/calculator/src/main/java/com/vayunmathur/calculator/domain/BasicUnitCategories.kt+++ /dev/null@@ -1,253 +0,0 @@-package com.vayunmathur.calculator.domain--/**- * Length, mass and time categories for [UnitRegistry], split out so the registry- * stays a single readable source without one oversized category builder.- */-internal object BasicUnitCategories {- // Length (metres).- private const val METRES_PER_KM = 1000.0- private const val METRES_PER_CM = 0.01- private const val METRES_PER_MM = 0.001- private const val METRES_PER_MICROMETRE = 1e-6- private const val METRES_PER_NM = 1e-9- private const val METRES_PER_MILE = 1609.344- private const val METRES_PER_YARD = 0.9144- private const val METRES_PER_FOOT = 0.3048- private const val METRES_PER_INCH = 0.0254- private const val METRES_PER_NAUTICAL_MILE = 1852.0- private const val METRES_PER_LIGHT_NANOSECOND = 0.299792458- private const val METRES_PER_LIGHT_MICROSECOND = 299.792458- private const val METRES_PER_LIGHT_MILLISECOND = 299792.458- private const val METRES_PER_LIGHT_SECOND = 299792458.0- private const val METRES_PER_LIGHT_MINUTE = 17987547480.0- private const val METRES_PER_LIGHT_HOUR = 1079252848800.0- private const val METRES_PER_LIGHT_DAY = 25902068371200.0- private const val METRES_PER_LIGHT_WEEK = 181314478598400.0- private const val METRES_PER_LIGHT_YEAR = 9.4607304725808e15- private const val METRES_PER_AU = 1.495978707e11- private const val METRES_PER_PARSEC = 3.08567758149137e16- private const val METRES_PER_KILOPARSEC = 3.08567758149137e19- private const val METRES_PER_MEGAPARSEC = 3.08567758149137e22- private const val METRES_PER_GIGAPARSEC = 3.08567758149137e25-- // Mass (kilograms).- private const val KG_PER_TONNE = 1000.0- private const val KG_PER_GRAM = 0.001- private const val KG_PER_MILLIGRAM = 1e-6- private const val KG_PER_MICROGRAM = 1e-9- private const val KG_PER_POUND = 0.45359237- private const val KG_PER_OUNCE = 0.028349523125- private const val KG_PER_STONE = 6.35029318-- // Time (seconds).- private const val SECONDS_PER_NANOSECOND = 1e-9- private const val SECONDS_PER_MICROSECOND = 1e-6- private const val SECONDS_PER_MILLISECOND = 0.001- private const val SECONDS_PER_MINUTE = 60.0- private const val SECONDS_PER_HOUR = 3600.0- private const val SECONDS_PER_DAY = 86400.0- private const val SECONDS_PER_WEEK = 604800.0- private const val SECONDS_PER_YEAR = 31557600.0-- fun lengthCategory(): UnitCategory =- UnitCategory(- "Length",- metricLengthUnits() + imperialLengthUnits() + parsecLengthUnits() +- largeLightLengthUnits() + smallLightLengthUnits() + astronomicalLengthUnits(),- )-- private fun metricLengthUnits(): List<UnitDef> =- listOf(- UnitDef("km", "km", "Kilometre", Dimension.LENGTH, METRES_PER_KM),- UnitDef("m", "m", "Metre", Dimension.LENGTH, 1.0),- UnitDef("cm", "cm", "Centimetre", Dimension.LENGTH, METRES_PER_CM),- UnitDef("mm", "mm", "Millimetre", Dimension.LENGTH, METRES_PER_MM),- UnitDef("um", "µm", "Micrometre", Dimension.LENGTH, METRES_PER_MICROMETRE),- UnitDef("nm", "nm", "Nanometre", Dimension.LENGTH, METRES_PER_NM),- )-- private fun imperialLengthUnits(): List<UnitDef> =- listOf(- UnitDef("mi", "mi", "Mile", Dimension.LENGTH, METRES_PER_MILE),- UnitDef("yd", "yd", "Yard", Dimension.LENGTH, METRES_PER_YARD),- UnitDef("ft", "ft", "Foot", Dimension.LENGTH, METRES_PER_FOOT),- UnitDef("in", "in", "Inch", Dimension.LENGTH, METRES_PER_INCH),- UnitDef("nmi", "nmi", "Nautical mile", Dimension.LENGTH, METRES_PER_NAUTICAL_MILE),- )-- private fun parsecLengthUnits(): List<UnitDef> =- listOf(- UnitDef(- "Gpc",- "Gpc",- "Gigaparsec",- Dimension.LENGTH,- METRES_PER_GIGAPARSEC,- aliases = listOf("gpc", "gigaparsec", "gigaparsecs"),- ),- UnitDef(- "Mpc",- "Mpc",- "Megaparsec",- Dimension.LENGTH,- METRES_PER_MEGAPARSEC,- aliases = listOf("mpc", "megaparsec", "megaparsecs"),- ),- UnitDef(- "kpc",- "kpc",- "Kiloparsec",- Dimension.LENGTH,- METRES_PER_KILOPARSEC,- aliases = listOf("kiloparsec", "kiloparsecs"),- ),- UnitDef(- "pc",- "pc",- "Parsec",- Dimension.LENGTH,- METRES_PER_PARSEC,- aliases = listOf("parsec", "parsecs"),- ),- )-- private fun largeLightLengthUnits(): List<UnitDef> =- listOf(- UnitDef(- "ly",- "ly",- "Light-year",- Dimension.LENGTH,- METRES_PER_LIGHT_YEAR,- aliases = listOf("lightyear", "lightyears"),- ),- UnitDef(- "lw",- "lw",- "Light-week",- Dimension.LENGTH,- METRES_PER_LIGHT_WEEK,- aliases = listOf("lwk", "lightweek", "lightweeks"),- ),- UnitDef(- "ld",- "ld",- "Light-day",- Dimension.LENGTH,- METRES_PER_LIGHT_DAY,- aliases = listOf("lday", "lightday", "lightdays"),- ),- UnitDef(- "lh",- "lh",- "Light-hour",- Dimension.LENGTH,- METRES_PER_LIGHT_HOUR,- aliases = listOf("lighthour", "lighthours"),- ),- UnitDef(- "lmin",- "lmin",- "Light-minute",- Dimension.LENGTH,- METRES_PER_LIGHT_MINUTE,- aliases = listOf("lightminute", "lightminutes"),- ),- )-- private fun smallLightLengthUnits(): List<UnitDef> =- listOf(- UnitDef(- "ls",- "ls",- "Light-second",- Dimension.LENGTH,- METRES_PER_LIGHT_SECOND,- aliases = listOf("lightsecond", "lightseconds"),- ),- UnitDef(- "lms",- "lms",- "Light-millisecond",- Dimension.LENGTH,- METRES_PER_LIGHT_MILLISECOND,- aliases =- listOf(- "lightmillisecond",- "lightmilliseconds",- ),- ),- UnitDef(- "lus",- "lµs",- "Light-microsecond",- Dimension.LENGTH,- METRES_PER_LIGHT_MICROSECOND,- aliases =- listOf(- "lightmicrosecond",- "lightmicroseconds",- ),- ),- UnitDef(- "lns",- "lns",- "Light-nanosecond",- Dimension.LENGTH,- METRES_PER_LIGHT_NANOSECOND,- aliases =- listOf(- "lightnanosecond",- "lightnanoseconds",- ),- ),- )-- private fun astronomicalLengthUnits(): List<UnitDef> =- listOf(- UnitDef(- "AU",- "AU",- "Astronomical unit",- Dimension.LENGTH,- METRES_PER_AU,- aliases = listOf("au", "astronomicalunit", "astronomicalunits"),- ),- )-- fun massCategory(): UnitCategory {- val d = Dimension- return UnitCategory(- "Mass",- listOf(- UnitDef("tonne", "t", "Tonne", d.MASS, KG_PER_TONNE),- UnitDef("kg", "kg", "Kilogram", d.MASS, 1.0),- UnitDef("g", "g", "Gram", d.MASS, KG_PER_GRAM),- UnitDef("mg", "mg", "Milligram", d.MASS, KG_PER_MILLIGRAM),- UnitDef("ug", "µg", "Microgram", d.MASS, KG_PER_MICROGRAM),- UnitDef("lb", "lb", "Pound", d.MASS, KG_PER_POUND),- UnitDef("oz", "oz", "Ounce", d.MASS, KG_PER_OUNCE),- UnitDef("st", "st", "Stone", d.MASS, KG_PER_STONE),- ),- )- }-- fun timeCategory(): UnitCategory {- val d = Dimension- return UnitCategory(- "Time",- listOf(- UnitDef("ns", "ns", "Nanosecond", d.TIME, SECONDS_PER_NANOSECOND),- UnitDef("us", "µs", "Microsecond", d.TIME, SECONDS_PER_MICROSECOND),- UnitDef("ms", "ms", "Millisecond", d.TIME, SECONDS_PER_MILLISECOND),- UnitDef("s", "s", "Second", d.TIME, 1.0),- UnitDef("min", "min", "Minute", d.TIME, SECONDS_PER_MINUTE),- UnitDef("h", "h", "Hour", d.TIME, SECONDS_PER_HOUR),- UnitDef("day", "day", "Day", d.TIME, SECONDS_PER_DAY),- UnitDef("wk", "wk", "Week", d.TIME, SECONDS_PER_WEEK),- UnitDef("yr", "yr", "Year", d.TIME, SECONDS_PER_YEAR),- ),- )- }-}
calculator/src/main/java/com/vayunmathur/calculator/domain/UnitRegistry.kt
diff --git a/calculator/src/main/java/com/vayunmathur/calculator/domain/UnitRegistry.kt b/calculator/src/main/java/com/vayunmathur/calculator/domain/UnitRegistry.ktindex 7a69a92a11..58398c0aae 100644--- a/calculator/src/main/java/com/vayunmathur/calculator/domain/UnitRegistry.kt+++ b/calculator/src/main/java/com/vayunmathur/calculator/domain/UnitRegistry.kt@@ -10,158 +10,247 @@ import kotlin.math.PI * units (metre, kilogram, second, kelvin, …), so addition just checks the [Dimension]s match * and multiplication just multiplies. Temperatures are the one affine exception and are * documented on [Quantity].- *+ */++/** * The single source of unit truth: the converter's [categories], the parser's [parseTokens],- * and the output-selector's [unitsFor].+ * and the output-selector's [unitsFor]. Each row below is one unit: token, symbol, display+ * name, and factor to the coherent base unit of its category (plus the affine [UnitDef.offsetK]+ * for temperatures and parser aliases where a unit has them). Category and unit order are the+ * converter's presentation order. */ object UnitRegistry {+ // Names the engine reserves for variables/constants/ans; a unit token must never shadow one. private val RESERVED = setOf("e", "t", "x", "theta", "pi", "tau", "phi", "ans") - private const val FAHRENHEIT_OFFSET_K = 273.15 - 32.0 * 5.0 / 9.0-- // ---- Conversion factors to coherent base units (one per unit, named for detekt). ----- // Temperature.- private const val KELVIN_OFFSET_OF_CELSIUS = 273.15- private const val KELVIN_PER_FAHRENHEIT_DEGREE = 5.0 / 9.0-- // Area (square metres).- private const val SQM_PER_SQUARE_KM = 1e6- private const val SQM_PER_SQUARE_CM = 1e-4- private const val SQM_PER_SQUARE_MM = 1e-6- private const val SQM_PER_HECTARE = 10000.0- private const val SQM_PER_ACRE = 4046.8564224- private const val SQM_PER_SQUARE_FOOT = 0.09290304- private const val SQM_PER_SQUARE_INCH = 0.00064516- private const val SQM_PER_SQUARE_MILE = 2589988.110336-- // Volume (cubic metres).- private const val CUBM_PER_CUBIC_CM = 1e-6- private const val CUBM_PER_LITRE = 0.001- private const val CUBM_PER_MILLILITRE = 1e-6- private const val CUBM_PER_GALLON = 0.003785411784- private const val CUBM_PER_QUART = 9.46352946e-4- private const val CUBM_PER_PINT = 4.73176473e-4- private const val CUBM_PER_CUP = 2.365882365e-4- private const val CUBM_PER_FLUID_OUNCE = 2.95735295625e-5- private const val CUBM_PER_CUBIC_FOOT = 0.028316846592- private const val CUBM_PER_CUBIC_INCH = 1.6387064e-5-- // Speed (metres per second).- private const val MPS_PER_KMH = 1.0 / 3.6- private const val MPS_PER_MPH = 0.44704- private const val MPS_PER_KNOT = 0.514444- private const val METRES_PER_FOOT = 0.3048-- // Data (bits).- private const val BITS_PER_BYTE = 8.0- private const val BITS_PER_KILOBYTE = 8e3- private const val BITS_PER_MEGABYTE = 8e6- private const val BITS_PER_GIGABYTE = 8e9- private const val BITS_PER_TERABYTE = 8e12- private const val BITS_PER_KIBIBYTE = 8.0 * 1024.0- private const val BITS_PER_MEBIBYTE = 8.0 * 1024.0 * 1024.0- private const val BITS_PER_GIBIBYTE = 8.0 * 1024.0 * 1024.0 * 1024.0- private const val BITS_PER_TEBIBYTE = 8.0 * 1024.0 * 1024.0 * 1024.0 * 1024.0- private const val BITS_PER_KILOBIT = 1e3- private const val BITS_PER_MEGABIT = 1e6- private const val BITS_PER_GIGABIT = 1e9-- // Energy (joules).- private const val JOULES_PER_KILOJOULE = 1000.0- private const val JOULES_PER_MEGAJOULE = 1e6- private const val JOULES_PER_CALORIE = 4.184- private const val JOULES_PER_KILOCALORIE = 4184.0- private const val JOULES_PER_WATT_HOUR = 3600.0- private const val JOULES_PER_KILOWATT_HOUR = 3.6e6- private const val JOULES_PER_ELECTRONVOLT = 1.602176634e-19- private const val JOULES_PER_BTU = 1055.05585262- private const val JOULES_PER_ERG = 1e-7-- // Power (watts).- private const val WATTS_PER_MILLIWATT = 0.001- private const val WATTS_PER_KILOWATT = 1000.0- private const val WATTS_PER_MEGAWATT = 1e6- private const val WATTS_PER_GIGAWATT = 1e9- private const val WATTS_PER_HORSEPOWER = 745.6998715823-- // Pressure (pascals).- private const val PA_PER_HECTOPASCAL = 100.0- private const val PA_PER_KILOPASCAL = 1000.0- private const val PA_PER_MEGAPASCAL = 1e6- private const val PA_PER_BAR = 1e5- private const val PA_PER_MILLIBAR = 100.0- private const val PA_PER_ATMOSPHERE = 101325.0- private const val PA_PER_PSI = 6894.757293168- private const val PA_PER_MMHG = 133.322387415- private const val PA_PER_TORR = 133.32236842105263-- // Force (newtons).- private const val NEWTONS_PER_KILONEWTON = 1000.0- private const val NEWTONS_PER_MILLINEWTON = 0.001- private const val NEWTONS_PER_POUND_FORCE = 4.4482216152605- private const val NEWTONS_PER_KG_FORCE = 9.80665- private const val NEWTONS_PER_DYNE = 1e-5-- // Frequency (hertz).- private const val HZ_PER_KILOHERTZ = 1000.0- private const val HZ_PER_MEGAHERTZ = 1e6- private const val HZ_PER_GIGAHERTZ = 1e9-- // Current (amperes).- private const val AMPS_PER_MICROAMP = 1e-6- private const val AMPS_PER_MILLIAMP = 0.001- private const val AMPS_PER_KILOAMP = 1000.0-- // Voltage (volts).- private const val VOLTS_PER_MICROVOLT = 1e-6- private const val VOLTS_PER_MILLIVOLT = 0.001- private const val VOLTS_PER_KILOVOLT = 1000.0-- // Resistance (ohms).- private const val OHMS_PER_MILLIOHM = 0.001- private const val OHMS_PER_KILOOHM = 1000.0- private const val OHMS_PER_MEGAOHM = 1e6-- // Charge (coulombs).- private const val COULOMBS_PER_MICROCOULOMB = 1e-6- private const val COULOMBS_PER_MILLICOULOMB = 0.001- private const val COULOMBS_PER_MILLIAMP_HOUR = 3.6- private const val COULOMBS_PER_AMP_HOUR = 3600.0-- // Capacitance (farads).- private const val FARADS_PER_PICOFARAD = 1e-12- private const val FARADS_PER_NANOFARAD = 1e-9- private const val FARADS_PER_MICROFARAD = 1e-6- private const val FARADS_PER_MILLIFARAD = 0.001-- // Amount (moles).- private const val MOLES_PER_MICROMOLE = 1e-6- private const val MOLES_PER_MILLIMOLE = 0.001- private const val MOLES_PER_KILOMOLE = 1000.0-- // Angle (radians) — derived from PI, so plain vals rather than consts.- private val RADIANS_PER_DEGREE = PI / 180.0- private val RADIANS_PER_GRADIAN = PI / 200.0- private val RADIANS_PER_ARCMINUTE = PI / 10800.0- private val RADIANS_PER_ARCSECOND = PI / 648000.0- private val RADIANS_PER_REVOLUTION = 2 * PI-- val categories: List<UnitCategory> = buildCategories()+ /** One converter row before its [Dimension] is attached by [cat]. */+ private data class Row(+ val token: String, val symbol: String, val name: String, val factorToBase: Double,+ val offsetK: Double? = null, val aliases: List<String> = emptyList(),+ )++ private fun r(+ token: String, symbol: String, name: String, factorToBase: Double,+ offsetK: Double? = null, aliases: List<String> = emptyList(),+ ) = Row(token, symbol, name, factorToBase, offsetK, aliases)++ private fun cat(+ name: String, dimension: Dimension, units: List<Row>, inEquations: Boolean = true,+ ) = UnitCategory(+ name,+ units.map { UnitDef(it.token, it.symbol, it.name, dimension, it.factorToBase, it.offsetK, it.aliases) },+ inEquations,+ )++ val categories: List<UnitCategory> = listOf(+ cat("Length", Dimension.LENGTH, listOf(+ r("km", "km", "Kilometre", 1000.0),+ r("m", "m", "Metre", 1.0),+ r("cm", "cm", "Centimetre", 0.01),+ r("mm", "mm", "Millimetre", 0.001),+ r("um", "µm", "Micrometre", 1e-6),+ r("nm", "nm", "Nanometre", 1e-9),+ r("mi", "mi", "Mile", 1609.344),+ r("yd", "yd", "Yard", 0.9144),+ r("ft", "ft", "Foot", 0.3048),+ r("in", "in", "Inch", 0.0254),+ r("nmi", "nmi", "Nautical mile", 1852.0),+ r("Gpc", "Gpc", "Gigaparsec", 3.08567758149137e25, aliases = listOf("gpc", "gigaparsec", "gigaparsecs")),+ r("Mpc", "Mpc", "Megaparsec", 3.08567758149137e22, aliases = listOf("mpc", "megaparsec", "megaparsecs")),+ r("kpc", "kpc", "Kiloparsec", 3.08567758149137e19, aliases = listOf("kiloparsec", "kiloparsecs")),+ r("pc", "pc", "Parsec", 3.08567758149137e16, aliases = listOf("parsec", "parsecs")),+ r("ly", "ly", "Light-year", 9.4607304725808e15, aliases = listOf("lightyear", "lightyears")),+ r("lw", "lw", "Light-week", 181314478598400.0, aliases = listOf("lwk", "lightweek", "lightweeks")),+ r("ld", "ld", "Light-day", 25902068371200.0, aliases = listOf("lday", "lightday", "lightdays")),+ r("lh", "lh", "Light-hour", 1079252848800.0, aliases = listOf("lighthour", "lighthours")),+ r("lmin", "lmin", "Light-minute", 17987547480.0, aliases = listOf("lightminute", "lightminutes")),+ r("ls", "ls", "Light-second", 299792458.0, aliases = listOf("lightsecond", "lightseconds")),+ r("lms", "lms", "Light-millisecond", 299792.458, aliases = listOf("lightmillisecond", "lightmilliseconds")),+ r("lus", "lµs", "Light-microsecond", 299.792458, aliases = listOf("lightmicrosecond", "lightmicroseconds")),+ r("lns", "lns", "Light-nanosecond", 0.299792458, aliases = listOf("lightnanosecond", "lightnanoseconds")),+ r("AU", "AU", "Astronomical unit", 1.495978707e11,+ aliases = listOf("au", "astronomicalunit", "astronomicalunits")),+ )),+ cat("Mass", Dimension.MASS, listOf(+ r("tonne", "t", "Tonne", 1000.0),+ r("kg", "kg", "Kilogram", 1.0),+ r("g", "g", "Gram", 0.001),+ r("mg", "mg", "Milligram", 1e-6),+ r("ug", "µg", "Microgram", 1e-9),+ r("lb", "lb", "Pound", 0.45359237),+ r("oz", "oz", "Ounce", 0.028349523125),+ r("st", "st", "Stone", 6.35029318),+ )),+ cat("Time", Dimension.TIME, listOf(+ r("ns", "ns", "Nanosecond", 1e-9),+ r("us", "µs", "Microsecond", 1e-6),+ r("ms", "ms", "Millisecond", 0.001),+ r("s", "s", "Second", 1.0),+ r("min", "min", "Minute", 60.0),+ r("h", "h", "Hour", 3600.0),+ r("day", "day", "Day", 86400.0),+ r("wk", "wk", "Week", 604800.0),+ r("yr", "yr", "Year", 31557600.0),+ )),+ cat("Temperature", Dimension.TEMPERATURE, listOf(+ r("K", "K", "Kelvin", 1.0, offsetK = 0.0, aliases = listOf("k")),+ r("degC", "°C", "Celsius", 1.0, offsetK = 273.15, aliases = listOf("c")),+ r("degF", "°F", "Fahrenheit", 5.0 / 9.0, offsetK = 273.15 - 32.0 * 5.0 / 9.0),+ r("degR", "°R", "Rankine", 5.0 / 9.0, offsetK = 0.0),+ )),+ cat("Area", Dimension.AREA, listOf(+ r("km2", "km²", "Square kilometre", 1e6),+ r("m2", "m²", "Square metre", 1.0),+ r("cm2", "cm²", "Square centimetre", 1e-4),+ r("mm2", "mm²", "Square millimetre", 1e-6),+ r("ha", "ha", "Hectare", 10000.0),+ r("acre", "acre", "Acre", 4046.8564224),+ r("ft2", "ft²", "Square foot", 0.09290304),+ r("in2", "in²", "Square inch", 0.00064516),+ r("mi2", "mi²", "Square mile", 2589988.110336),+ )),+ cat("Volume", Dimension.VOLUME, listOf(+ r("m3", "m³", "Cubic metre", 1.0),+ r("cm3", "cm³", "Cubic centimetre", 1e-6),+ r("L", "L", "Litre", 0.001, aliases = listOf("l")),+ r("mL", "mL", "Millilitre", 1e-6, aliases = listOf("ml")),+ r("gal", "gal", "Gallon (US)", 0.003785411784),+ r("qt", "qt", "Quart (US)", 9.46352946e-4),+ r("pt", "pt", "Pint (US)", 4.73176473e-4),+ r("cup", "cup", "Cup (US)", 2.365882365e-4),+ r("floz", "fl oz", "Fluid ounce (US)", 2.95735295625e-5),+ r("ft3", "ft³", "Cubic foot", 0.028316846592),+ r("in3", "in³", "Cubic inch", 1.6387064e-5),+ )),+ cat("Speed", Dimension.SPEED, listOf(+ r("mps", "m/s", "Metres per second", 1.0),+ r("kmh", "km/h", "Kilometres per hour", 1.0 / 3.6),+ r("mph", "mph", "Miles per hour", 0.44704),+ r("fps", "ft/s", "Feet per second", 0.3048),+ r("kn", "kn", "Knot", 0.514444),+ )),+ cat("Data", Dimension.INFORMATION, listOf(+ r("bit", "bit", "Bit", 1.0),+ r("B", "B", "Byte", 8.0),+ r("kB", "kB", "Kilobyte", 8e3),+ r("MB", "MB", "Megabyte", 8e6),+ r("GB", "GB", "Gigabyte", 8e9),+ r("TB", "TB", "Terabyte", 8e12),+ r("KiB", "KiB", "Kibibyte", 8.0 * 1024.0),+ r("MiB", "MiB", "Mebibyte", 8.0 * 1024.0 * 1024.0),+ r("GiB", "GiB", "Gibibyte", 8.0 * 1024.0 * 1024.0 * 1024.0),+ r("TiB", "TiB", "Tebibyte", 8.0 * 1024.0 * 1024.0 * 1024.0 * 1024.0),+ r("kbit", "kbit", "Kilobit", 1e3),+ r("Mbit", "Mbit", "Megabit", 1e6),+ r("Gbit", "Gbit", "Gigabit", 1e9),+ )),+ cat("Energy", Dimension.ENERGY, listOf(+ r("J", "J", "Joule", 1.0),+ r("kJ", "kJ", "Kilojoule", 1000.0),+ r("MJ", "MJ", "Megajoule", 1e6),+ r("cal", "cal", "Calorie", 4.184),+ r("kcal", "kcal", "Kilocalorie", 4184.0),+ r("Wh", "Wh", "Watt-hour", 3600.0),+ r("kWh", "kWh", "Kilowatt-hour", 3.6e6),+ r("eV", "eV", "Electronvolt", 1.602176634e-19),+ r("BTU", "BTU", "British thermal unit", 1055.05585262),+ r("erg", "erg", "Erg", 1e-7),+ )),+ cat("Power", Dimension.POWER, listOf(+ r("mW", "mW", "Milliwatt", 0.001),+ r("W", "W", "Watt", 1.0),+ r("kW", "kW", "Kilowatt", 1000.0),+ r("MW", "MW", "Megawatt", 1e6),+ r("GW", "GW", "Gigawatt", 1e9),+ r("hp", "hp", "Horsepower", 745.6998715823),+ )),+ cat("Pressure", Dimension.PRESSURE, listOf(+ r("Pa", "Pa", "Pascal", 1.0),+ r("hPa", "hPa", "Hectopascal", 100.0),+ r("kPa", "kPa", "Kilopascal", 1000.0),+ r("MPa", "MPa", "Megapascal", 1e6),+ r("bar", "bar", "Bar", 1e5),+ r("mbar", "mbar", "Millibar", 100.0),+ r("atm", "atm", "Atmosphere", 101325.0),+ r("psi", "psi", "Pound per square inch", 6894.757293168),+ r("mmHg", "mmHg", "Millimetre of mercury", 133.322387415),+ r("torr", "torr", "Torr", 133.32236842105263),+ )),+ cat("Force", Dimension.FORCE, listOf(+ r("N", "N", "Newton", 1.0),+ r("kN", "kN", "Kilonewton", 1000.0),+ r("mN", "mN", "Millinewton", 0.001),+ r("lbf", "lbf", "Pound-force", 4.4482216152605),+ r("kgf", "kgf", "Kilogram-force", 9.80665),+ r("dyn", "dyn", "Dyne", 1e-5),+ )),+ cat("Frequency", Dimension.FREQUENCY, listOf(+ r("Hz", "Hz", "Hertz", 1.0),+ r("kHz", "kHz", "Kilohertz", 1000.0),+ r("MHz", "MHz", "Megahertz", 1e6),+ r("GHz", "GHz", "Gigahertz", 1e9),+ )),+ cat("Current", Dimension.CURRENT, listOf(+ r("uA", "µA", "Microampere", 1e-6),+ r("mA", "mA", "Milliampere", 0.001),+ r("A", "A", "Ampere", 1.0),+ r("kA", "kA", "Kiloampere", 1000.0),+ )),+ cat("Voltage", Dimension.VOLTAGE, listOf(+ r("uV", "µV", "Microvolt", 1e-6),+ r("mV", "mV", "Millivolt", 0.001),+ r("V", "V", "Volt", 1.0),+ r("kV", "kV", "Kilovolt", 1000.0),+ )),+ cat("Resistance", Dimension.RESISTANCE, listOf(+ r("mohm", "mΩ", "Milliohm", 0.001),+ r("ohm", "Ω", "Ohm", 1.0),+ r("kohm", "kΩ", "Kiloohm", 1000.0),+ r("Mohm", "MΩ", "Megaohm", 1e6),+ )),+ cat("Charge", Dimension.CHARGE, listOf(+ r("uC", "µC", "Microcoulomb", 1e-6),+ r("mC", "mC", "Millicoulomb", 0.001),+ r("C", "C", "Coulomb", 1.0),+ r("mAh", "mAh", "Milliamp-hour", 3.6),+ r("Ah", "Ah", "Amp-hour", 3600.0),+ )),+ cat("Capacitance", Dimension.CAPACITANCE, listOf(+ r("pF", "pF", "Picofarad", 1e-12),+ r("nF", "nF", "Nanofarad", 1e-9),+ r("uF", "µF", "Microfarad", 1e-6),+ r("mF", "mF", "Millifarad", 0.001),+ r("F", "F", "Farad", 1.0),+ )),+ cat("Amount", Dimension.AMOUNT, listOf(+ r("umol", "µmol", "Micromole", 1e-6),+ r("mmol", "mmol", "Millimole", 0.001),+ r("mol", "mol", "Mole", 1.0),+ r("kmol", "kmol", "Kilomole", 1000.0),+ )),+ cat("Angle", Dimension.NONE, listOf(+ r("rad", "rad", "Radian", 1.0),+ r("deg", "°", "Degree", PI / 180.0),+ r("grad", "grad", "Gradian", PI / 200.0),+ r("arcmin", "′", "Arcminute", PI / 10800.0),+ r("arcsec", "″", "Arcsecond", PI / 648000.0),+ r("rev", "rev", "Revolution", 2 * PI),+ ), inEquations = false),+ ) /** Case-sensitive lookup for the parser, keyed by every token and alias. */- val parseTokens: Map<String, UnitDef> =- buildMap {- for (category in categories) {- if (!category.inEquations) continue- for (unit in category.units) {- for (key in listOf(unit.token) + unit.aliases) {- if (key.lowercase() in RESERVED) continue- putIfAbsent(key, unit)- }+ val parseTokens: Map<String, UnitDef> = buildMap {+ for (category in categories) {+ if (!category.inEquations) continue+ for (unit in category.units) {+ for (key in listOf(unit.token) + unit.aliases) {+ if (key.lowercase() in RESERVED) continue+ putIfAbsent(key, unit) } } }+ } /** Units sharing [dimension], to populate the output-unit selector. */ fun unitsFor(dimension: Dimension): List<UnitDef> =@@ -180,171 +269,63 @@ object UnitRegistry { /** Display names for every ISO 4217 code the rate source returns; unknown codes fall back * to the code itself. */- private val CURRENCY_NAMES: Map<String, String> =- mapOf(- "AED" to "UAE Dirham",- "AFN" to "Afghan Afghani",- "ALL" to "Albanian Lek",- "AMD" to "Armenian Dram",- "ANG" to "Netherlands Antillean Guilder",- "AOA" to "Angolan Kwanza",- "ARS" to "Argentine Peso",- "AUD" to "Australian Dollar",- "AWG" to "Aruban Florin",- "AZN" to "Azerbaijani Manat",- "BAM" to "Bosnia-Herzegovina Convertible Mark",- "BBD" to "Barbadian Dollar",- "BDT" to "Bangladeshi Taka",- "BGN" to "Bulgarian Lev",- "BHD" to "Bahraini Dinar",- "BIF" to "Burundian Franc",- "BMD" to "Bermudan Dollar",- "BND" to "Brunei Dollar",- "BOB" to "Bolivian Boliviano",- "BRL" to "Brazilian Real",- "BSD" to "Bahamian Dollar",- "BTN" to "Bhutanese Ngultrum",- "BWP" to "Botswanan Pula",- "BYN" to "Belarusian Ruble",- "BZD" to "Belize Dollar",- "CAD" to "Canadian Dollar",- "CDF" to "Congolese Franc",- "CHF" to "Swiss Franc",- "CLP" to "Chilean Peso",- "CNY" to "Chinese Yuan",- "COP" to "Colombian Peso",- "CRC" to "Costa Rican Colon",- "CUP" to "Cuban Peso",- "CVE" to "Cape Verdean Escudo",- "CZK" to "Czech Koruna",- "DJF" to "Djiboutian Franc",- "DKK" to "Danish Krone",- "DOP" to "Dominican Peso",- "DZD" to "Algerian Dinar",- "EGP" to "Egyptian Pound",- "ERN" to "Eritrean Nakfa",- "ETB" to "Ethiopian Birr",- "EUR" to "Euro",- "FJD" to "Fijian Dollar",- "FKP" to "Falkland Islands Pound",- "FOK" to "Faroese Krona",- "GBP" to "British Pound",- "GEL" to "Georgian Lari",- "GGP" to "Guernsey Pound",- "GHS" to "Ghanaian Cedi",- "GIP" to "Gibraltar Pound",- "GMD" to "Gambian Dalasi",- "GNF" to "Guinean Franc",- "GTQ" to "Guatemalan Quetzal",- "GYD" to "Guyanaese Dollar",- "HKD" to "Hong Kong Dollar",- "HNL" to "Honduran Lempira",- "HRK" to "Croatian Kuna",- "HTG" to "Haitian Gourde",- "HUF" to "Hungarian Forint",- "IDR" to "Indonesian Rupiah",- "ILS" to "Israeli Shekel",- "IMP" to "Manx Pound",- "INR" to "Indian Rupee",- "IQD" to "Iraqi Dinar",- "IRR" to "Iranian Rial",- "ISK" to "Icelandic Krona",- "JEP" to "Jersey Pound",- "JMD" to "Jamaican Dollar",- "JOD" to "Jordanian Dinar",- "JPY" to "Japanese Yen",- "KES" to "Kenyan Shilling",- "KGS" to "Kyrgystani Som",- "KHR" to "Cambodian Riel",- "KID" to "Kiribati Dollar",- "KMF" to "Comorian Franc",- "KRW" to "South Korean Won",- "KWD" to "Kuwaiti Dinar",- "KYD" to "Cayman Islands Dollar",- "KZT" to "Kazakhstani Tenge",- "LAK" to "Laotian Kip",- "LBP" to "Lebanese Pound",- "LKR" to "Sri Lankan Rupee",- "LRD" to "Liberian Dollar",- "LSL" to "Lesotho Loti",- "LYD" to "Libyan Dinar",- "MAD" to "Moroccan Dirham",- "MDL" to "Moldovan Leu",- "MGA" to "Malagasy Ariary",- "MKD" to "Macedonian Denar",- "MMK" to "Myanmar Kyat",- "MNT" to "Mongolian Tugrik",- "MOP" to "Macanese Pataca",- "MRU" to "Mauritanian Ouguiya",- "MUR" to "Mauritian Rupee",- "MVR" to "Maldivian Rufiyaa",- "MWK" to "Malawian Kwacha",- "MXN" to "Mexican Peso",- "MYR" to "Malaysian Ringgit",- "MZN" to "Mozambican Metical",- "NAD" to "Namibian Dollar",- "NGN" to "Nigerian Naira",- "NIO" to "Nicaraguan Cordoba",- "NOK" to "Norwegian Krone",- "NPR" to "Nepalese Rupee",- "NZD" to "New Zealand Dollar",- "OMR" to "Omani Rial",- "PAB" to "Panamanian Balboa",- "PEN" to "Peruvian Sol",- "PGK" to "Papua New Guinean Kina",- "PHP" to "Philippine Peso",- "PKR" to "Pakistani Rupee",- "PLN" to "Polish Zloty",- "PYG" to "Paraguayan Guarani",- "QAR" to "Qatari Riyal",- "RON" to "Romanian Leu",- "RSD" to "Serbian Dinar",- "RUB" to "Russian Ruble",- "RWF" to "Rwandan Franc",- "SAR" to "Saudi Riyal",- "SBD" to "Solomon Islands Dollar",- "SCR" to "Seychellois Rupee",- "SDG" to "Sudanese Pound",- "SEK" to "Swedish Krona",- "SGD" to "Singapore Dollar",- "SHP" to "St. Helena Pound",- "SLE" to "Sierra Leonean Leone",- "SOS" to "Somali Shilling",- "SRD" to "Surinamese Dollar",- "SSP" to "South Sudanese Pound",- "STN" to "Sao Tome & Principe Dobra",- "SYP" to "Syrian Pound",- "SZL" to "Swazi Lilangeni",- "THB" to "Thai Baht",- "TJS" to "Tajikistani Somoni",- "TMT" to "Turkmenistani Manat",- "TND" to "Tunisian Dinar",- "TOP" to "Tongan Pa'anga",- "TRY" to "Turkish Lira",- "TTD" to "Trinidad & Tobago Dollar",- "TVD" to "Tuvaluan Dollar",- "TWD" to "Taiwan Dollar",- "TZS" to "Tanzanian Shilling",- "UAH" to "Ukrainian Hryvnia",- "UGX" to "Ugandan Shilling",- "USD" to "US Dollar",- "UYU" to "Uruguayan Peso",- "UZS" to "Uzbekistani Som",- "VES" to "Venezuelan Bolivar",- "VND" to "Vietnamese Dong",- "VUV" to "Vanuatu Vatu",- "WST" to "Samoan Tala",- "XAF" to "Central African CFA Franc",- "XCD" to "East Caribbean Dollar",- "XCG" to "Caribbean Guilder",- "XDR" to "Special Drawing Rights",- "XOF" to "West African CFA Franc",- "XPF" to "CFP Franc",- "YER" to "Yemeni Rial",- "ZAR" to "South African Rand",- "ZMW" to "Zambian Kwacha",- "ZWL" to "Zimbabwean Dollar",- )+ private val CURRENCY_NAMES: Map<String, String> = mapOf(+ "AED" to "UAE Dirham", "AFN" to "Afghan Afghani", "ALL" to "Albanian Lek",+ "AMD" to "Armenian Dram", "ANG" to "Netherlands Antillean Guilder", "AOA" to "Angolan Kwanza",+ "ARS" to "Argentine Peso", "AUD" to "Australian Dollar", "AWG" to "Aruban Florin",+ "AZN" to "Azerbaijani Manat", "BAM" to "Bosnia-Herzegovina Convertible Mark",+ "BBD" to "Barbadian Dollar", "BDT" to "Bangladeshi Taka", "BGN" to "Bulgarian Lev",+ "BHD" to "Bahraini Dinar", "BIF" to "Burundian Franc", "BMD" to "Bermudan Dollar",+ "BND" to "Brunei Dollar", "BOB" to "Bolivian Boliviano", "BRL" to "Brazilian Real",+ "BSD" to "Bahamian Dollar", "BTN" to "Bhutanese Ngultrum", "BWP" to "Botswanan Pula",+ "BYN" to "Belarusian Ruble", "BZD" to "Belize Dollar", "CAD" to "Canadian Dollar",+ "CDF" to "Congolese Franc", "CHF" to "Swiss Franc", "CLP" to "Chilean Peso",+ "CNY" to "Chinese Yuan", "COP" to "Colombian Peso", "CRC" to "Costa Rican Colon",+ "CUP" to "Cuban Peso", "CVE" to "Cape Verdean Escudo", "CZK" to "Czech Koruna",+ "DJF" to "Djiboutian Franc", "DKK" to "Danish Krone", "DOP" to "Dominican Peso",+ "DZD" to "Algerian Dinar", "EGP" to "Egyptian Pound", "ERN" to "Eritrean Nakfa",+ "ETB" to "Ethiopian Birr", "EUR" to "Euro", "FJD" to "Fijian Dollar",+ "FKP" to "Falkland Islands Pound", "FOK" to "Faroese Krona", "GBP" to "British Pound",+ "GEL" to "Georgian Lari", "GGP" to "Guernsey Pound", "GHS" to "Ghanaian Cedi",+ "GIP" to "Gibraltar Pound", "GMD" to "Gambian Dalasi", "GNF" to "Guinean Franc",+ "GTQ" to "Guatemalan Quetzal", "GYD" to "Guyanaese Dollar", "HKD" to "Hong Kong Dollar",+ "HNL" to "Honduran Lempira", "HRK" to "Croatian Kuna", "HTG" to "Haitian Gourde",+ "HUF" to "Hungarian Forint", "IDR" to "Indonesian Rupiah", "ILS" to "Israeli Shekel",+ "IMP" to "Manx Pound", "INR" to "Indian Rupee", "IQD" to "Iraqi Dinar",+ "IRR" to "Iranian Rial", "ISK" to "Icelandic Krona", "JEP" to "Jersey Pound",+ "JMD" to "Jamaican Dollar", "JOD" to "Jordanian Dinar", "JPY" to "Japanese Yen",+ "KES" to "Kenyan Shilling", "KGS" to "Kyrgystani Som", "KHR" to "Cambodian Riel",+ "KID" to "Kiribati Dollar", "KMF" to "Comorian Franc", "KRW" to "South Korean Won",+ "KWD" to "Kuwaiti Dinar", "KYD" to "Cayman Islands Dollar", "KZT" to "Kazakhstani Tenge",+ "LAK" to "Laotian Kip", "LBP" to "Lebanese Pound", "LKR" to "Sri Lankan Rupee",+ "LRD" to "Liberian Dollar", "LSL" to "Lesotho Loti", "LYD" to "Libyan Dinar",+ "MAD" to "Moroccan Dirham", "MDL" to "Moldovan Leu", "MGA" to "Malagasy Ariary",+ "MKD" to "Macedonian Denar", "MMK" to "Myanmar Kyat", "MNT" to "Mongolian Tugrik",+ "MOP" to "Macanese Pataca", "MRU" to "Mauritanian Ouguiya", "MUR" to "Mauritian Rupee",+ "MVR" to "Maldivian Rufiyaa", "MWK" to "Malawian Kwacha", "MXN" to "Mexican Peso",+ "MYR" to "Malaysian Ringgit", "MZN" to "Mozambican Metical", "NAD" to "Namibian Dollar",+ "NGN" to "Nigerian Naira", "NIO" to "Nicaraguan Cordoba", "NOK" to "Norwegian Krone",+ "NPR" to "Nepalese Rupee", "NZD" to "New Zealand Dollar", "OMR" to "Omani Rial",+ "PAB" to "Panamanian Balboa", "PEN" to "Peruvian Sol", "PGK" to "Papua New Guinean Kina",+ "PHP" to "Philippine Peso", "PKR" to "Pakistani Rupee", "PLN" to "Polish Zloty",+ "PYG" to "Paraguayan Guarani", "QAR" to "Qatari Riyal", "RON" to "Romanian Leu",+ "RSD" to "Serbian Dinar", "RUB" to "Russian Ruble", "RWF" to "Rwandan Franc",+ "SAR" to "Saudi Riyal", "SBD" to "Solomon Islands Dollar", "SCR" to "Seychellois Rupee",+ "SDG" to "Sudanese Pound", "SEK" to "Swedish Krona", "SGD" to "Singapore Dollar",+ "SHP" to "St. Helena Pound", "SLE" to "Sierra Leonean Leone", "SOS" to "Somali Shilling",+ "SRD" to "Surinamese Dollar", "SSP" to "South Sudanese Pound",+ "STN" to "Sao Tome & Principe Dobra", "SYP" to "Syrian Pound", "SZL" to "Swazi Lilangeni",+ "THB" to "Thai Baht", "TJS" to "Tajikistani Somoni", "TMT" to "Turkmenistani Manat",+ "TND" to "Tunisian Dinar", "TOP" to "Tongan Pa'anga", "TRY" to "Turkish Lira",+ "TTD" to "Trinidad & Tobago Dollar", "TVD" to "Tuvaluan Dollar", "TWD" to "Taiwan Dollar",+ "TZS" to "Tanzanian Shilling", "UAH" to "Ukrainian Hryvnia", "UGX" to "Ugandan Shilling",+ "USD" to "US Dollar", "UYU" to "Uruguayan Peso", "UZS" to "Uzbekistani Som",+ "VES" to "Venezuelan Bolivar", "VND" to "Vietnamese Dong", "VUV" to "Vanuatu Vatu",+ "WST" to "Samoan Tala", "XAF" to "Central African CFA Franc", "XCD" to "East Caribbean Dollar",+ "XCG" to "Caribbean Guilder", "XDR" to "Special Drawing Rights",+ "XOF" to "West African CFA Franc", "XPF" to "CFP Franc", "YER" to "Yemeni Rial",+ "ZAR" to "South African Rand", "ZMW" to "Zambian Kwacha", "ZWL" to "Zimbabwean Dollar",+ ) /** * Build the converter's live "Currency" category from [rates] (units of the currency per@@ -359,324 +340,15 @@ object UnitRegistry { if (usable.isEmpty()) return null val priority = CURRENCY_PRIORITY.filter { it in usable } val rest = usable.keys.filter { it !in priority }.sorted()- val units =- (priority + rest).map { code ->- UnitDef(- token = code,- symbol = code,- name = CURRENCY_NAMES[code] ?: code,- dimension = Dimension.CURRENCY,- factorToBase = 1.0 / usable.getValue(code),- )- }+ val units = (priority + rest).map { code ->+ UnitDef(+ token = code,+ symbol = code,+ name = CURRENCY_NAMES[code] ?: code,+ dimension = Dimension.CURRENCY,+ factorToBase = 1.0 / usable.getValue(code),+ )+ } return UnitCategory("Currency", units, inEquations = false) }-- private fun buildCategories(): List<UnitCategory> =- listOf(- BasicUnitCategories.lengthCategory(),- BasicUnitCategories.massCategory(),- BasicUnitCategories.timeCategory(),- temperatureCategory(),- areaCategory(),- volumeCategory(),- speedCategory(),- dataCategory(),- energyCategory(),- powerCategory(),- pressureCategory(),- forceCategory(),- frequencyCategory(),- currentCategory(),- voltageCategory(),- resistanceCategory(),- chargeCategory(),- capacitanceCategory(),- amountCategory(),- angleCategory(),- )-- private fun temperatureCategory(): UnitCategory {- val d = Dimension- return UnitCategory(- "Temperature",- listOf(- UnitDef("K", "K", "Kelvin", d.TEMPERATURE, 1.0, offsetK = 0.0, aliases = listOf("k")),- UnitDef(- "degC",- "°C",- "Celsius",- d.TEMPERATURE,- 1.0,- offsetK = KELVIN_OFFSET_OF_CELSIUS,- aliases = listOf("c"),- ),- UnitDef(- "degF",- "°F",- "Fahrenheit",- d.TEMPERATURE,- KELVIN_PER_FAHRENHEIT_DEGREE,- offsetK = FAHRENHEIT_OFFSET_K,- ),- UnitDef(- "degR",- "°R",- "Rankine",- d.TEMPERATURE,- KELVIN_PER_FAHRENHEIT_DEGREE,- offsetK = 0.0,- ),- ),- )- }-- private fun areaCategory(): UnitCategory {- val d = Dimension- return UnitCategory(- "Area",- listOf(- UnitDef("km2", "km²", "Square kilometre", d.AREA, SQM_PER_SQUARE_KM),- UnitDef("m2", "m²", "Square metre", d.AREA, 1.0),- UnitDef("cm2", "cm²", "Square centimetre", d.AREA, SQM_PER_SQUARE_CM),- UnitDef("mm2", "mm²", "Square millimetre", d.AREA, SQM_PER_SQUARE_MM),- UnitDef("ha", "ha", "Hectare", d.AREA, SQM_PER_HECTARE),- UnitDef("acre", "acre", "Acre", d.AREA, SQM_PER_ACRE),- UnitDef("ft2", "ft²", "Square foot", d.AREA, SQM_PER_SQUARE_FOOT),- UnitDef("in2", "in²", "Square inch", d.AREA, SQM_PER_SQUARE_INCH),- UnitDef("mi2", "mi²", "Square mile", d.AREA, SQM_PER_SQUARE_MILE),- ),- )- }-- private fun volumeCategory(): UnitCategory {- val d = Dimension- return UnitCategory(- "Volume",- listOf(- UnitDef("m3", "m³", "Cubic metre", d.VOLUME, 1.0),- UnitDef("cm3", "cm³", "Cubic centimetre", d.VOLUME, CUBM_PER_CUBIC_CM),- UnitDef("L", "L", "Litre", d.VOLUME, CUBM_PER_LITRE, aliases = listOf("l")),- UnitDef("mL", "mL", "Millilitre", d.VOLUME, CUBM_PER_MILLILITRE, aliases = listOf("ml")),- UnitDef("gal", "gal", "Gallon (US)", d.VOLUME, CUBM_PER_GALLON),- UnitDef("qt", "qt", "Quart (US)", d.VOLUME, CUBM_PER_QUART),- UnitDef("pt", "pt", "Pint (US)", d.VOLUME, CUBM_PER_PINT),- UnitDef("cup", "cup", "Cup (US)", d.VOLUME, CUBM_PER_CUP),- UnitDef("floz", "fl oz", "Fluid ounce (US)", d.VOLUME, CUBM_PER_FLUID_OUNCE),- UnitDef("ft3", "ft³", "Cubic foot", d.VOLUME, CUBM_PER_CUBIC_FOOT),- UnitDef("in3", "in³", "Cubic inch", d.VOLUME, CUBM_PER_CUBIC_INCH),- ),- )- }-- private fun speedCategory(): UnitCategory {- val d = Dimension- return UnitCategory(- "Speed",- listOf(- UnitDef("mps", "m/s", "Metres per second", d.SPEED, 1.0),- UnitDef("kmh", "km/h", "Kilometres per hour", d.SPEED, MPS_PER_KMH),- UnitDef("mph", "mph", "Miles per hour", d.SPEED, MPS_PER_MPH),- UnitDef("fps", "ft/s", "Feet per second", d.SPEED, METRES_PER_FOOT),- UnitDef("kn", "kn", "Knot", d.SPEED, MPS_PER_KNOT),- ),- )- }-- private fun dataCategory(): UnitCategory {- val d = Dimension- return UnitCategory(- "Data",- listOf(- UnitDef("bit", "bit", "Bit", d.INFORMATION, 1.0),- UnitDef("B", "B", "Byte", d.INFORMATION, BITS_PER_BYTE),- UnitDef("kB", "kB", "Kilobyte", d.INFORMATION, BITS_PER_KILOBYTE),- UnitDef("MB", "MB", "Megabyte", d.INFORMATION, BITS_PER_MEGABYTE),- UnitDef("GB", "GB", "Gigabyte", d.INFORMATION, BITS_PER_GIGABYTE),- UnitDef("TB", "TB", "Terabyte", d.INFORMATION, BITS_PER_TERABYTE),- UnitDef("KiB", "KiB", "Kibibyte", d.INFORMATION, BITS_PER_KIBIBYTE),- UnitDef("MiB", "MiB", "Mebibyte", d.INFORMATION, BITS_PER_MEBIBYTE),- UnitDef("GiB", "GiB", "Gibibyte", d.INFORMATION, BITS_PER_GIBIBYTE),- UnitDef("TiB", "TiB", "Tebibyte", d.INFORMATION, BITS_PER_TEBIBYTE),- UnitDef("kbit", "kbit", "Kilobit", d.INFORMATION, BITS_PER_KILOBIT),- UnitDef("Mbit", "Mbit", "Megabit", d.INFORMATION, BITS_PER_MEGABIT),- UnitDef("Gbit", "Gbit", "Gigabit", d.INFORMATION, BITS_PER_GIGABIT),- ),- )- }-- private fun energyCategory(): UnitCategory {- val d = Dimension- return UnitCategory(- "Energy",- listOf(- UnitDef("J", "J", "Joule", d.ENERGY, 1.0),- UnitDef("kJ", "kJ", "Kilojoule", d.ENERGY, JOULES_PER_KILOJOULE),- UnitDef("MJ", "MJ", "Megajoule", d.ENERGY, JOULES_PER_MEGAJOULE),- UnitDef("cal", "cal", "Calorie", d.ENERGY, JOULES_PER_CALORIE),- UnitDef("kcal", "kcal", "Kilocalorie", d.ENERGY, JOULES_PER_KILOCALORIE),- UnitDef("Wh", "Wh", "Watt-hour", d.ENERGY, JOULES_PER_WATT_HOUR),- UnitDef("kWh", "kWh", "Kilowatt-hour", d.ENERGY, JOULES_PER_KILOWATT_HOUR),- UnitDef("eV", "eV", "Electronvolt", d.ENERGY, JOULES_PER_ELECTRONVOLT),- UnitDef("BTU", "BTU", "British thermal unit", d.ENERGY, JOULES_PER_BTU),- UnitDef("erg", "erg", "Erg", d.ENERGY, JOULES_PER_ERG),- ),- )- }-- private fun powerCategory(): UnitCategory {- val d = Dimension- return UnitCategory(- "Power",- listOf(- UnitDef("mW", "mW", "Milliwatt", d.POWER, WATTS_PER_MILLIWATT),- UnitDef("W", "W", "Watt", d.POWER, 1.0),- UnitDef("kW", "kW", "Kilowatt", d.POWER, WATTS_PER_KILOWATT),- UnitDef("MW", "MW", "Megawatt", d.POWER, WATTS_PER_MEGAWATT),- UnitDef("GW", "GW", "Gigawatt", d.POWER, WATTS_PER_GIGAWATT),- UnitDef("hp", "hp", "Horsepower", d.POWER, WATTS_PER_HORSEPOWER),- ),- )- }-- private fun pressureCategory(): UnitCategory {- val d = Dimension- return UnitCategory(- "Pressure",- listOf(- UnitDef("Pa", "Pa", "Pascal", d.PRESSURE, 1.0),- UnitDef("hPa", "hPa", "Hectopascal", d.PRESSURE, PA_PER_HECTOPASCAL),- UnitDef("kPa", "kPa", "Kilopascal", d.PRESSURE, PA_PER_KILOPASCAL),- UnitDef("MPa", "MPa", "Megapascal", d.PRESSURE, PA_PER_MEGAPASCAL),- UnitDef("bar", "bar", "Bar", d.PRESSURE, PA_PER_BAR),- UnitDef("mbar", "mbar", "Millibar", d.PRESSURE, PA_PER_MILLIBAR),- UnitDef("atm", "atm", "Atmosphere", d.PRESSURE, PA_PER_ATMOSPHERE),- UnitDef("psi", "psi", "Pound per square inch", d.PRESSURE, PA_PER_PSI),- UnitDef("mmHg", "mmHg", "Millimetre of mercury", d.PRESSURE, PA_PER_MMHG),- UnitDef("torr", "torr", "Torr", d.PRESSURE, PA_PER_TORR),- ),- )- }-- private fun forceCategory(): UnitCategory {- val d = Dimension- return UnitCategory(- "Force",- listOf(- UnitDef("N", "N", "Newton", d.FORCE, 1.0),- UnitDef("kN", "kN", "Kilonewton", d.FORCE, NEWTONS_PER_KILONEWTON),- UnitDef("mN", "mN", "Millinewton", d.FORCE, NEWTONS_PER_MILLINEWTON),- UnitDef("lbf", "lbf", "Pound-force", d.FORCE, NEWTONS_PER_POUND_FORCE),- UnitDef("kgf", "kgf", "Kilogram-force", d.FORCE, NEWTONS_PER_KG_FORCE),- UnitDef("dyn", "dyn", "Dyne", d.FORCE, NEWTONS_PER_DYNE),- ),- )- }-- private fun frequencyCategory(): UnitCategory {- val d = Dimension- return UnitCategory(- "Frequency",- listOf(- UnitDef("Hz", "Hz", "Hertz", d.FREQUENCY, 1.0),- UnitDef("kHz", "kHz", "Kilohertz", d.FREQUENCY, HZ_PER_KILOHERTZ),- UnitDef("MHz", "MHz", "Megahertz", d.FREQUENCY, HZ_PER_MEGAHERTZ),- UnitDef("GHz", "GHz", "Gigahertz", d.FREQUENCY, HZ_PER_GIGAHERTZ),- ),- )- }-- private fun currentCategory(): UnitCategory {- val d = Dimension- return UnitCategory(- "Current",- listOf(- UnitDef("uA", "µA", "Microampere", d.CURRENT, AMPS_PER_MICROAMP),- UnitDef("mA", "mA", "Milliampere", d.CURRENT, AMPS_PER_MILLIAMP),- UnitDef("A", "A", "Ampere", d.CURRENT, 1.0),- UnitDef("kA", "kA", "Kiloampere", d.CURRENT, AMPS_PER_KILOAMP),- ),- )- }-- private fun voltageCategory(): UnitCategory {- val d = Dimension- return UnitCategory(- "Voltage",- listOf(- UnitDef("uV", "µV", "Microvolt", d.VOLTAGE, VOLTS_PER_MICROVOLT),- UnitDef("mV", "mV", "Millivolt", d.VOLTAGE, VOLTS_PER_MILLIVOLT),- UnitDef("V", "V", "Volt", d.VOLTAGE, 1.0),- UnitDef("kV", "kV", "Kilovolt", d.VOLTAGE, VOLTS_PER_KILOVOLT),- ),- )- }-- private fun resistanceCategory(): UnitCategory {- val d = Dimension- return UnitCategory(- "Resistance",- listOf(- UnitDef("mohm", "mΩ", "Milliohm", d.RESISTANCE, OHMS_PER_MILLIOHM),- UnitDef("ohm", "Ω", "Ohm", d.RESISTANCE, 1.0),- UnitDef("kohm", "kΩ", "Kiloohm", d.RESISTANCE, OHMS_PER_KILOOHM),- UnitDef("Mohm", "MΩ", "Megaohm", d.RESISTANCE, OHMS_PER_MEGAOHM),- ),- )- }-- private fun chargeCategory(): UnitCategory {- val d = Dimension- return UnitCategory(- "Charge",- listOf(- UnitDef("uC", "µC", "Microcoulomb", d.CHARGE, COULOMBS_PER_MICROCOULOMB),- UnitDef("mC", "mC", "Millicoulomb", d.CHARGE, COULOMBS_PER_MILLICOULOMB),- UnitDef("C", "C", "Coulomb", d.CHARGE, 1.0),- UnitDef("mAh", "mAh", "Milliamp-hour", d.CHARGE, COULOMBS_PER_MILLIAMP_HOUR),- UnitDef("Ah", "Ah", "Amp-hour", d.CHARGE, COULOMBS_PER_AMP_HOUR),- ),- )- }-- private fun capacitanceCategory(): UnitCategory {- val d = Dimension- return UnitCategory(- "Capacitance",- listOf(- UnitDef("pF", "pF", "Picofarad", d.CAPACITANCE, FARADS_PER_PICOFARAD),- UnitDef("nF", "nF", "Nanofarad", d.CAPACITANCE, FARADS_PER_NANOFARAD),- UnitDef("uF", "µF", "Microfarad", d.CAPACITANCE, FARADS_PER_MICROFARAD),- UnitDef("mF", "mF", "Millifarad", d.CAPACITANCE, FARADS_PER_MILLIFARAD),- UnitDef("F", "F", "Farad", d.CAPACITANCE, 1.0),- ),- )- }-- private fun amountCategory(): UnitCategory {- val d = Dimension- return UnitCategory(- "Amount",- listOf(- UnitDef("umol", "µmol", "Micromole", d.AMOUNT, MOLES_PER_MICROMOLE),- UnitDef("mmol", "mmol", "Millimole", d.AMOUNT, MOLES_PER_MILLIMOLE),- UnitDef("mol", "mol", "Mole", d.AMOUNT, 1.0),- UnitDef("kmol", "kmol", "Kilomole", d.AMOUNT, MOLES_PER_KILOMOLE),- ),- )- }-- private fun angleCategory(): UnitCategory =- UnitCategory(- "Angle",- listOf(- UnitDef("rad", "rad", "Radian", Dimension.NONE, 1.0),- UnitDef("deg", "°", "Degree", Dimension.NONE, RADIANS_PER_DEGREE),- UnitDef("grad", "grad", "Gradian", Dimension.NONE, RADIANS_PER_GRADIAN),- UnitDef("arcmin", "′", "Arcminute", Dimension.NONE, RADIANS_PER_ARCMINUTE),- UnitDef("arcsec", "″", "Arcsecond", Dimension.NONE, RADIANS_PER_ARCSECOND),- UnitDef("rev", "rev", "Revolution", Dimension.NONE, RADIANS_PER_REVOLUTION),- ),- inEquations = false,- ) }