Skip to content

Commit

Permalink
implement shifting of units
Browse files Browse the repository at this point in the history
  • Loading branch information
iTitus committed Oct 18, 2021
1 parent 4a400b5 commit bbce8cf
Show file tree
Hide file tree
Showing 15 changed files with 233 additions and 151 deletions.
5 changes: 5 additions & 0 deletions src/main/java/io/github/ititus/si/unit/AlternateUnit.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ public <T extends QuantityType<T>> Unit<T> as(T type) {
return new AlternateUnit<>(baseUnit.as(type), alternateSymbol);
}

@Override
public Unit<Q> shift(QuantityValue v) {
return ConvertedUnit.of(this, UnitConverter.shift(v));
}

@Override
public Unit<Q> multiply(QuantityValue v) {
return baseUnit.multiply(v);
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/io/github/ititus/si/unit/BaseUnit.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ public <T extends QuantityType<T>> Unit<T> as(T type) {
return new BaseUnit<>(type, symbol);
}

@Override
public Unit<Q> shift(QuantityValue v) {
return ConvertedUnit.of(this, UnitConverter.shift(v));
}

@Override
public Unit<Q> multiply(QuantityValue v) {
return ConvertedUnit.of(this, UnitConverter.factor(v));
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/io/github/ititus/si/unit/CompoundUnit.java
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,11 @@ public <T extends QuantityType<T>> Unit<T> as(T type) {
return new CompoundUnit<>(type, getDimension(), units);
}

@Override
public Unit<Q> shift(QuantityValue v) {
return ConvertedUnit.of(this, UnitConverter.shift(v));
}

@Override
public Unit<Q> multiply(QuantityValue v) {
return ConvertedUnit.of(this, UnitConverter.factor(v));
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/io/github/ititus/si/unit/ConvertedUnit.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static <Q extends QuantityType<Q>> Unit<Q> of(Unit<Q> baseUnit, UnitConve

@Override
public String getSymbol() {
throw new UnsupportedOperationException("converted units have not symbol");
throw new UnsupportedOperationException("converted units have no symbol");
}

@Override
Expand Down Expand Up @@ -56,6 +56,11 @@ public <T extends QuantityType<T>> Unit<T> as(T type) {
return new ConvertedUnit<>(baseUnit.as(type), converter);
}

@Override
public Unit<Q> shift(QuantityValue v) {
return of(baseUnit, converter.concat(UnitConverter.shift(v)));
}

@Override
public Unit<Q> multiply(QuantityValue v) {
return of(baseUnit, converter.concat(UnitConverter.factor(v)));
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/io/github/ititus/si/unit/PrefixUnit.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ public <T extends QuantityType<T>> Unit<T> as(T type) {
return new PrefixUnit<>(baseUnit.as(type), prefix);
}

@Override
public Unit<Q> shift(QuantityValue v) {
return ConvertedUnit.of(this, UnitConverter.shift(v));
}

@Override
public Unit<Q> multiply(QuantityValue v) {
return ConvertedUnit.of(this, UnitConverter.factor(v));
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/io/github/ititus/si/unit/Unit.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,36 @@ default <T extends QuantityType<T>> boolean isCommensurableWith(T type) {

<T extends QuantityType<T>> Unit<T> as(T type);

default Unit<Q> shift(int n) {
return shift(BigRational.of(n));
}

default Unit<Q> shift(long n) {
return shift(QuantityValue.of(n));
}

default Unit<Q> shift(BigInteger n) {
return shift(QuantityValue.of(n));
}

default Unit<Q> shift(float f) {
return shift(QuantityValue.of(f));
}

default Unit<Q> shift(double d) {
return shift(QuantityValue.of(d));
}

default Unit<Q> shift(BigDecimal d) {
return shift(QuantityValue.of(d));
}

default Unit<Q> shift(BigRational r) {
return shift(QuantityValue.of(r));
}

Unit<Q> shift(QuantityValue v);

default Unit<Q> multiply(int n) {
return multiply(BigRational.of(n));
}
Expand Down
106 changes: 38 additions & 68 deletions src/main/java/io/github/ititus/si/unit/Units.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.github.ititus.si.quantity.type.*;

import static io.github.ititus.math.number.BigRational.of;
import static io.github.ititus.math.number.BigRational.ofExp;
import static io.github.ititus.si.prefix.MetricPrefix.*;
import static io.github.ititus.si.quantity.type.Acceleration.ACCELERATION;
Expand Down Expand Up @@ -68,150 +69,119 @@ public final class Units {
// Length
public static final Unit<Length> KILOMETRE = METRE.prefix(KILO);
public static final Unit<Length> DECIMETRE = METRE.prefix(DECI);
public static final Unit<Volume> CUBIC_DECIMETRE = DECIMETRE.pow(3).as(VOLUME);
public static final Unit<Volume> LITRE = CUBIC_DECIMETRE.alternate("l");
public static final Unit<Length> MILLIMETRE = METRE.prefix(MILLI);
public static final Unit<Length> INCH = MILLIMETRE.multiply(ofExp(254, -1)).alternate("in");
public static final Unit<Length> FOOT = INCH.multiply(12).alternate("ft");
public static final Unit<Length> YARD = FOOT.multiply(3).alternate("yd");
public static final Unit<Length> MILE = YARD.multiply(1760).alternate("mi");
public static final Unit<Length> FATHOM = YARD.multiply(2).alternate("ftm");
public static final Unit<Length> PICA = INCH.divide(6).alternate("pica");
public static final Unit<Length> POINT = PICA.divide(12).alternate("pt");
public static final Unit<Length> FATHOM = YARD.multiply(2).alternate("ftm");
public static final Unit<Length> NAUTICAL_MILE = METRE.multiply(1_852).alternate("nmi");
public static final Unit<Length> CABLE = NAUTICAL_MILE.divide(10).alternate("cb");

// Mass
public static final Unit<Mass> KILOGRAM = GRAM.prefix(KILO);
public static final Unit<Mass> TONNE = KILOGRAM.multiply(1_000).alternate("t");
public static final Unit<Mass> CARAT = GRAM.divide(5).alternate("ct");
public static final Unit<Mass> POUND = KILOGRAM.multiply(ofExp(45_359_237, -8)).alternate("lb");
public static final Unit<Mass> OUNCE = POUND.divide(16).alternate("oz");
public static final Unit<Mass> DRAM = OUNCE.divide(16).alternate("dr");
public static final Unit<Mass> GRAIN = POUND.divide(7_000).alternate("gr");
public static final Unit<Mass> PENNYWEIGHT = GRAIN.multiply(24).alternate("dwt");
public static final Unit<Mass> TROY_OUNCE = PENNYWEIGHT.multiply(20).alternate("oz t");
public static final Unit<Mass> TROY_POUND = TROY_OUNCE.multiply(12).alternate("lb t");

// Momentum
public static final Unit<Momentum> KILOGRAM_METRES_PER_SECOND =
KILOGRAM.multiply(METRE.divide(SECOND)).as(MOMENTUM);
public static final Unit<Mass> CARAT = GRAM.divide(5).alternate("ct");
// Time
public static final Unit<Time> MINUTE = SECOND.multiply(60).alternate("min");
public static final Unit<Time> HOUR = MINUTE.multiply(60).alternate("h");
public static final Unit<Time> DAY = HOUR.multiply(24).alternate("d");
public static final Unit<Time> NANOSECOND = SECOND.prefix(NANO);
public static final Unit<Time> MICROSECOND = SECOND.prefix(MICRO);
public static final Unit<Time> MILLISECOND = SECOND.prefix(MILLI);

// Electric Current

// Thermodynamic Temperature
public static final Unit<Speed> KILOMETRES_PER_HOUR = KILOMETRE.divide(HOUR).as(SPEED);
public static final Unit<Speed> MILES_PER_HOUR = MILE.divide(HOUR).alternate("mph").as(SPEED);
public static final Unit<Speed> KNOT = NAUTICAL_MILE.divide(HOUR).alternate("kn").as(SPEED);

// Amount of Substance

// Luminous Intensity

public static final Unit<Pace> MINUTES_PER_KILOMETRE = MINUTE.divide(KILOMETRE).as(PACE);
public static final Unit<Time> NANOSECOND = SECOND.prefix(NANO);
public static final Unit<Time> MICROSECOND = SECOND.prefix(MICRO);
public static final Unit<Time> MILLISECOND = SECOND.prefix(MILLI);
// Thermodynamic Temperature
public static final Unit<ThermodynamicTemperature> CELSIUS = KELVIN.shift(ofExp(27315, -2)).alternate("°C");
public static final Unit<ThermodynamicTemperature> RANKINE = KELVIN.multiply(of(9, 5)).alternate("°R");
public static final Unit<ThermodynamicTemperature> FAHRENHEIT = RANKINE.shift(ofExp(45967, -2)).alternate("°F");
// (Plane) Angle
public static final Unit<Angle> RADIAN = ONE.alternate("rad").as(ANGLE);

// Solid Angle
public static final Unit<SolidAngle> STERADIAN = ONE.alternate("sr").as(SOLID_ANGLE);

// Luminous Flux
public static final Unit<LuminousFlux> LUMEN = CANDELA.multiply(STERADIAN).alternate("lm").as(LUMINOUS_FLUX);
// Frequency
public static final Unit<Frequency> HERTZ = SECOND.inverse().alternate("Hz").as(FREQUENCY);

// Speed
public static final Unit<Speed> METRES_PER_SECOND = METRE.divide(SECOND).as(SPEED);
public static final Unit<Speed> KILOMETRES_PER_HOUR = KILOMETRE.divide(HOUR).as(SPEED);
public static final Unit<Speed> MILES_PER_HOUR = MILE.divide(HOUR).alternate("mph").as(SPEED);
public static final Unit<Speed> KNOT = NAUTICAL_MILE.divide(HOUR).alternate("kn").as(SPEED);

// Pace
public static final Unit<Pace> SECONDS_PER_METRE = SECOND.divide(METRE).as(PACE);
public static final Unit<Pace> MINUTES_PER_KILOMETRE = MINUTE.divide(KILOMETRE).as(PACE);

// Acceleration
public static final Unit<Acceleration> METRES_PER_SECOND_SQUARED = METRE.divide(SECOND.pow(2)).as(ACCELERATION);

// Momentum
public static final Unit<Momentum> KILOGRAM_METRES_PER_SECOND =
KILOGRAM.multiply(METRE.divide(SECOND)).as(MOMENTUM);

// Force
public static final Unit<Force> NEWTON = KILOGRAM.multiply(METRES_PER_SECOND_SQUARED).alternate("N").as(FORCE);

// Energy & Power
public static final Unit<Energy> JOULE = NEWTON.multiply(METRE).alternate("J").as(ENERGY);
public static final Unit<Energy> ELECTRON_VOLT = JOULE.multiply(ofExp(1.602_176_634, -19));
public static final Unit<Power> WATT = JOULE.divide(SECOND).alternate("W").as(POWER);
public static final Unit<Power> KILOWATT = WATT.prefix(KILO);
public static final Unit<Energy> KILOWATT_HOUR = KILOWATT.multiply(HOUR).as(ENERGY);

// Luminous Efficacy
public static final Unit<LuminousEfficacy> LUMEN_PER_WATT = LUMEN.divide(WATT).as(LUMINOUS_EFFICACY);
// Radiation Dose Absorbed
public static final Unit<RadiationDoseAbsorbed> GRAY =
JOULE.divide(KILOGRAM).alternate("Gy").as(RADIATION_DOSE_ABSORBED);
// Equivalent Radiation Dose
public static final Unit<EquivalentRadiationDose> SIEVERT =
JOULE.divide(KILOGRAM).alternate("Sv").as(EQUIVALENT_RADIATION_DOSE);
// Area
public static final Unit<Area> SQUARE_METRE = METRE.pow(2).as(AREA);

// Pressure
public static final Unit<Pressure> PASCAL = NEWTON.divide(SQUARE_METRE).alternate("Pa").as(PRESSURE);
// Electric Current Density
public static final Unit<ElectricCurrentDensity> AMPERE_PER_SQUARE_METRE =
AMPERE.divide(SQUARE_METRE).as(ELECTRIC_CURRENT_DENSITY);
// Illuminance
public static final Unit<Illuminance> LUX = LUMEN.divide(SQUARE_METRE).alternate("lx").as(ILLUMINANCE);
// Volume
public static final Unit<Volume> CUBIC_METRE = METRE.pow(3).as(VOLUME);
public static final Unit<Volume> CUBIC_DECIMETRE = DECIMETRE.pow(3).as(VOLUME);
public static final Unit<Volume> LITRE = CUBIC_DECIMETRE.alternate("l");

// Density
public static final Unit<Density> KILOGRAM_PER_CUBIC_METRE = KILOGRAM.divide(CUBIC_METRE).as(DENSITY);

// Pressure
public static final Unit<Pressure> PASCAL = NEWTON.divide(SQUARE_METRE).alternate("Pa").as(PRESSURE);

// Electric Charge
public static final Unit<ElectricCharge> COULOMB = AMPERE.multiply(SECOND).alternate("C").as(ELECTRIC_CHARGE);

// Electric Potential
public static final Unit<ElectricPotential> VOLT = JOULE.divide(COULOMB).alternate("V").as(ELECTRIC_POTENTIAL);

// Electric Capacitance
public static final Unit<ElectricCapacitance> FARAD = COULOMB.divide(VOLT).alternate("F").as(ELECTRIC_CAPACITANCE);

// Electric Permittivity
public static final Unit<ElectricPermittivity> FARAD_PER_METRE = FARAD.divide(METRE).as(ELECTRIC_PERMITTIVITY);
// Electric Resistance
public static final Unit<ElectricResistance> OHM = VOLT.divide(AMPERE).alternate("\u03A9").as(ELECTRIC_RESISTANCE);

// Electric Conductance
public static final Unit<ElectricConductance> SIEMENS = OHM.inverse().alternate("S").as(ELECTRIC_CONDUCTANCE);

// Magnetic Flux
public static final Unit<MagneticFlux> WEBER = VOLT.multiply(SECOND).alternate("Wb").as(MAGNETIC_FLUX);

// Magnetic Flux Density
public static final Unit<MagneticFluxDensity> TESLA =
WEBER.divide(SQUARE_METRE).alternate("T").as(MAGNETIC_FLUX_DENSITY);

// Electric Inductance
public static final Unit<ElectricInductance> HENRY = WEBER.divide(AMPERE).alternate("H").as(ELECTRIC_INDUCTANCE);

// Electric Current Density
public static final Unit<ElectricCurrentDensity> AMPERE_PER_SQUARE_METRE =
AMPERE.divide(SQUARE_METRE).as(ELECTRIC_CURRENT_DENSITY);

// Electric Permittivity
public static final Unit<ElectricPermittivity> FARAD_PER_METRE = FARAD.divide(METRE).as(ELECTRIC_PERMITTIVITY);

// Magnetic Permeability
public static final Unit<MagneticPermeability> HENRY_PER_METRE = HENRY.divide(METRE).as(MAGNETIC_PERMEABILITY);

// Radioactivity
public static final Unit<Radioactivity> BECQUEREL = SECOND.inverse().alternate("Bq").as(RADIOACTIVITY);

// Radiation Dose Absorbed
public static final Unit<RadiationDoseAbsorbed> GRAY =
JOULE.divide(KILOGRAM).alternate("Gy").as(RADIATION_DOSE_ABSORBED);

// Equivalent Radiation Dose
public static final Unit<EquivalentRadiationDose> SIEVERT =
JOULE.divide(KILOGRAM).alternate("Sv").as(EQUIVALENT_RADIATION_DOSE);

// Luminous Flux
public static final Unit<LuminousFlux> LUMEN = CANDELA.multiply(STERADIAN).alternate("lm").as(LUMINOUS_FLUX);

// Luminous Efficacy
public static final Unit<LuminousEfficacy> LUMEN_PER_WATT = LUMEN.divide(WATT).as(LUMINOUS_EFFICACY);

// Illuminance
public static final Unit<Illuminance> LUX = LUMEN.divide(SQUARE_METRE).alternate("lx").as(ILLUMINANCE);

// Catalytic Activity
public static final Unit<CatalyticActivity> KATAL = MOLE.divide(SECOND).alternate("kat").as(CATALYTIC_ACTIVITY);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ private static List<UnitConverter> simplify(List<UnitConverter> converters) {
outer:
for (int i = 0; i < converters.size(); i++) {
UnitConverter c = converters.get(i);
if (!(c instanceof MultiplicationConverter)) {
if (!(c instanceof LinearConverter)) {
simplified.add(c);
}

for (int j = i + 1; j < converters.size(); j++) {
UnitConverter c_ = converters.get(j);
if (!(c_ instanceof MultiplicationConverter)) {
if (!(c_ instanceof LinearConverter)) {
simplified.add(c);
i = j;
continue outer;
Expand Down
Loading

0 comments on commit bbce8cf

Please sign in to comment.