--- title: Cross Unit Converter test date: 2018-01-18 21:01:43 tags: - java - unit-converter --- ### Unit groups ![Unit groups](unitxc-groups.png "Unit groups") ```graphviz digraph G { kelvin[shape=box,style=filled,color=".9 0.5 0.9",label="kelvin\nK\nkelvin\n(thermodynamic_temperature)",labelloc=b]; kelvin -> kelvin[color=red,label="174"]; ampere[shape=box,style=filled,color=".9 0.5 0.9",label="ampere\nA\nampere\n(electric_current)",labelloc=b]; ampere -> ampere[color=red,label="29"]; candela[shape=box,style=filled,color=".9 0.5 0.9",label="candela\ncd\ncandela\n(luminous_intensity)",labelloc=b]; candela -> candela[color=red,label="29"]; mole[shape=box,style=filled,color=".9 0.5 0.9",label="mole\nmol\nmole\n(amount_of_substance)",labelloc=b]; mole -> mole[color=red,label="30"]; metre[shape=box,style=filled,color=".9 0.5 0.9",label="metre\nm\nmetre\n(length)",labelloc=b]; metre -> metre[color=red,label="348"]; kilogram[shape=box,style=filled,color=".9 0.5 0.9",label="kilogram\ng\ngram\n(mass)",labelloc=b]; kilogram -> kilogram[color=red,label="174"]; second[shape=box,style=filled,color=".9 0.5 0.9",label="second\ns\nsecond\n(time)",labelloc=b]; second -> second[color=red,label="174"]; square_metre[label="square_metre\nm²\nsquare metre\n(area)",labelloc=b]; square_metre -> square_metre[color=red,label="351"]; metre -> square_metre[dir=none]; cubic_metre[label="cubic_metre\nm³\ncubic metre\n(volume)",labelloc=b]; cubic_metre -> cubic_metre[color=red,label="348"]; metre -> cubic_metre[dir=none]; metre_per_second[label="metre_per_second\nm/s\nmetre per second\n(speed,velocity)",labelloc=b]; metre_per_second -> metre_per_second[color=red,label="60552"]; metre -> metre_per_second[dir=none]; second -> metre_per_second[dir=none]; cubic_metre_per_second[label="cubic_metre_per_second\nm³/s\ncubic metre per second\n(volumetric_flow)",labelloc=b]; cubic_metre_per_second -> cubic_metre_per_second[color=red,label="60552"]; cubic_metre -> cubic_metre_per_second[dir=none]; second -> cubic_metre_per_second[dir=none]; metre_per_second_squared[label="metre_per_second_squared\nm/s²\nMeter per second squared\n(acceleration)",labelloc=b]; metre_per_second_squared -> metre_per_second_squared[color=red,label="10536049"]; metre_per_second -> metre_per_second_squared[dir=none]; second -> metre_per_second_squared[dir=none]; metre_per_second_cubed[label="metre_per_second_cubed\nm/s²/s\nMeter per second squared per second\n(jerk,jolt,surge,lurch)",labelloc=b]; metre_per_second_cubed -> metre_per_second_cubed[color=red,label="1833272526"]; metre_per_second_squared -> metre_per_second_cubed[dir=none]; second -> metre_per_second_cubed[dir=none]; metre_per_quartic_second[label="metre_per_quartic_second\nm/s²/s/s\nMeter per second squared per second per second\n(snap,jounce)",labelloc=b]; metre_per_quartic_second -> metre_per_quartic_second[color=red,label="318989419524"]; metre_per_second_cubed -> metre_per_quartic_second[dir=none]; second -> metre_per_quartic_second[dir=none]; newton[label="newton\nN\nnewton\n(force,weight)",labelloc=b]; newton -> newton[color=red,label="1833272527"]; kilogram -> newton[dir=none]; metre_per_second_squared -> newton[dir=none]; kilogram_per_square_metre[label="kilogram_per_square_metre\ng/m²\ngram per square metre\n(area_density)",labelloc=b]; kilogram_per_square_metre -> kilogram_per_square_metre[color=red,label="61074"]; kilogram -> kilogram_per_square_metre[dir=none]; square_metre -> kilogram_per_square_metre[dir=none]; kilogram_per_cubic_metre[label="kilogram_per_cubic_metre\ng/m³\ngram per cubic metre\n(density,mass_density)",labelloc=b]; kilogram_per_cubic_metre -> kilogram_per_cubic_metre[color=red,label="60552"]; kilogram -> kilogram_per_cubic_metre[dir=none]; cubic_metre -> kilogram_per_cubic_metre[dir=none]; cubic_metre_per_kilogram[label="cubic_metre_per_kilogram\nm³/g\ncubic metre per gram\n(specific_volume)",labelloc=b]; cubic_metre_per_kilogram -> cubic_metre_per_kilogram[color=red,label="60552"]; cubic_metre -> cubic_metre_per_kilogram[dir=none]; kilogram -> cubic_metre_per_kilogram[dir=none]; mole_per_cubic_metre[label="mole_per_cubic_metre\nmol/m³\nmole per cubic metre\n(molarity)",labelloc=b]; mole_per_cubic_metre -> mole_per_cubic_metre[color=red,label="10440"]; mole -> mole_per_cubic_metre[dir=none]; cubic_metre -> mole_per_cubic_metre[dir=none]; cubic_metre_per_mole[label="cubic_metre_per_mole\nm³/mol\ncubic metre per mole\n(molar_volume)",labelloc=b]; cubic_metre_per_mole -> cubic_metre_per_mole[color=red,label="10440"]; cubic_metre -> cubic_metre_per_mole[dir=none]; mole -> cubic_metre_per_mole[dir=none]; newton_per_second[label="newton_per_second\nkg/m/s/s/s\nkilogram per metre per second per second per second\n(yank)",labelloc=b]; newton_per_second -> newton_per_second[color=red,label="318989419698"]; newton -> newton_per_second[dir=none]; second -> newton_per_second[dir=none]; Stats[shape=box,color=".9 0.2 0.8",label="unit groups: 21\ntotal units: 641656246143",labelloc=b]; } ``` ### Building groups ``` java /** * * * @author Willem Cazander * @version 1.0 Oct 17, 2015 */ public class UnitXCModuleKelvin implements UnitXCConfigModule { public static final String GROUP_ID = "kelvin"; public static final String GROUP_BASE_TYPE_ID = "K"; private static final String[] GROUP_QUANTITIES = {"thermodynamic_temperature"}; public static final String TYPE_KELVIN_ID = GROUP_BASE_TYPE_ID; public static final String TYPE_KELVIN_NAME = GROUP_ID; public static final String TYPE_KELVIN_FLAG = buildFlag(GROUP_ID,TYPE_KELVIN_NAME); private static final String TYPE_KELVIN_WIKI = "Kelvin"; public static final String TYPE_CELSIUS_ID = "°C"; public static final String TYPE_CELSIUS_NAME = "celsius"; public static final String TYPE_CELSIUS_FLAG = buildFlag(GROUP_ID,TYPE_CELSIUS_NAME); private static final double TYPE_CELSIUS_OFFSET = 273.15; private static final String TYPE_CELSIUS_WIKI = "Celsius"; public static final String TYPE_FAHRENHEIT_ID = "°F"; public static final String TYPE_FAHRENHEIT_NAME = "fahrenheit"; public static final String TYPE_FAHRENHEIT_FLAG = buildFlag(GROUP_ID,TYPE_FAHRENHEIT_NAME); private static final double TYPE_FAHRENHEIT_OFFSET = 459.67; private static final Fraction TYPE_FAHRENHEIT_FRACTION = Fraction.getFraction(5,9); private static final String TYPE_FAHRENHEIT_WIKI = "Fahrenheit"; public static final String TYPE_RANKINE_ID = "°R"; public static final String TYPE_RANKINE_NAME = "rankine"; public static final String TYPE_RANKINE_FLAG = buildFlag(GROUP_ID,TYPE_RANKINE_NAME); private static final String TYPE_RANKINE_WIKI = "Rankine_scale"; public static final String TYPE_ROMER_ID = "Rø"; public static final String TYPE_ROMER_NAME = "rømer"; public static final String TYPE_ROMER_FLAG = buildFlag(GROUP_ID,TYPE_ROMER_NAME); public static final String TYPE_ROMER_ALIAS_NAME = "romer"; public static final String TYPE_ROMER_ALIAS_FLAG = buildFlag(GROUP_ID,TYPE_ROMER_ALIAS_NAME); private static final Fraction TYPE_ROMER_FRACTION = Fraction.getFraction(40,21); private static final double TYPE_ROMER_FRACTION_OFFSET = 7.5; private static final String TYPE_ROMER_WIKI = "Rømer_scale"; @Override public void configModule(UnitXCConfigBuilder builder) { builder.createUnitGroupBase(GROUP_ID) .addQuantityIds(GROUP_QUANTITIES) .setBaseTypeId( TYPE_KELVIN_ID) .createSIUnitTypes() .setId( TYPE_KELVIN_ID) .setName( TYPE_KELVIN_NAME) .addTypeFlag( TYPE_KELVIN_FLAG) .setWebLinkWiki(TYPE_KELVIN_WIKI) .build() .createSIUnitTypes() .setId( TYPE_CELSIUS_ID) .setName( TYPE_CELSIUS_NAME) .addTypeFlag( TYPE_CELSIUS_FLAG) .setWebLinkWiki(TYPE_CELSIUS_WIKI) .createToBaseConverterSteps().offsetUp(TYPE_CELSIUS_OFFSET).build() .createFromBaseConverterSteps().offsetDown(TYPE_CELSIUS_OFFSET).build() .build() .createSIUnitTypes() .setId( TYPE_FAHRENHEIT_ID) .setName( TYPE_FAHRENHEIT_NAME) .addTypeFlag( TYPE_FAHRENHEIT_FLAG) .addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL) .setWebLinkWiki( TYPE_FAHRENHEIT_WIKI) .createToBaseConverterSteps() .offsetUp( TYPE_FAHRENHEIT_OFFSET) .multiply( TYPE_FAHRENHEIT_FRACTION) .build() .createFromBaseConverterSteps() .multiply( TYPE_FAHRENHEIT_FRACTION.invert()) .offsetDown( TYPE_FAHRENHEIT_OFFSET) .build() .build() .createSIUnitTypes() .setId( TYPE_RANKINE_ID) .setName( TYPE_RANKINE_NAME) .addTypeFlag ( TYPE_RANKINE_FLAG) .setWebLinkWiki( TYPE_RANKINE_WIKI) .createToBaseConverterSteps().multiply(TYPE_FAHRENHEIT_FRACTION).build() .createFromBaseConverterSteps().multiply(TYPE_FAHRENHEIT_FRACTION.invert()).build() .build() .createSIUnitTypes() .setId( TYPE_ROMER_ID) .setName( TYPE_ROMER_NAME) .addTypeFlag( TYPE_ROMER_FLAG) .setWebLinkWiki( TYPE_ROMER_WIKI) .createToBaseConverterSteps() .offsetUp( TYPE_CELSIUS_OFFSET) .multiply( TYPE_ROMER_FRACTION) .offsetUp( TYPE_ROMER_FRACTION_OFFSET) .build() .createFromBaseConverterSteps() .offsetDown( TYPE_ROMER_FRACTION_OFFSET) .multiply( TYPE_ROMER_FRACTION.invert()) .offsetDown( TYPE_CELSIUS_OFFSET) .build() .build() .createSIUnitTypes() .setAliasOfType( TYPE_ROMER_ID) .setId( TYPE_ROMER_ALIAS_NAME) .setName( TYPE_ROMER_ALIAS_NAME) .addTypeFlag( TYPE_ROMER_ALIAS_FLAG) .build() .build() ; } } ```