From bbce8cfdf971b28cd9072524499c634bb8edd6a2 Mon Sep 17 00:00:00 2001 From: iTitus Date: Mon, 18 Oct 2021 20:39:00 +0200 Subject: [PATCH] implement shifting of units --- .../github/ititus/si/unit/AlternateUnit.java | 5 + .../io/github/ititus/si/unit/BaseUnit.java | 5 + .../github/ititus/si/unit/CompoundUnit.java | 5 + .../github/ititus/si/unit/ConvertedUnit.java | 7 +- .../io/github/ititus/si/unit/PrefixUnit.java | 5 + .../java/io/github/ititus/si/unit/Unit.java | 30 +++++ .../java/io/github/ititus/si/unit/Units.java | 106 +++++++----------- .../si/unit/converter/CompoundConverter.java | 4 +- .../si/unit/converter/LinearConverter.java | 88 +++++++++++++++ .../converter/MultiplicationConverter.java | 68 ----------- .../si/unit/converter/UnitConverter.java | 12 +- .../ititus/si/quantity/type/ForceTest.java | 10 +- .../ititus/si/quantity/type/LengthTest.java | 6 +- .../ititus/si/quantity/type/MassTest.java | 4 +- .../si/quantity/type/TemperatureTest.java | 29 +++++ 15 files changed, 233 insertions(+), 151 deletions(-) create mode 100644 src/main/java/io/github/ititus/si/unit/converter/LinearConverter.java delete mode 100644 src/main/java/io/github/ititus/si/unit/converter/MultiplicationConverter.java create mode 100644 src/test/java/io/github/ititus/si/quantity/type/TemperatureTest.java diff --git a/src/main/java/io/github/ititus/si/unit/AlternateUnit.java b/src/main/java/io/github/ititus/si/unit/AlternateUnit.java index 86277d2..6a2db8d 100644 --- a/src/main/java/io/github/ititus/si/unit/AlternateUnit.java +++ b/src/main/java/io/github/ititus/si/unit/AlternateUnit.java @@ -44,6 +44,11 @@ public > Unit as(T type) { return new AlternateUnit<>(baseUnit.as(type), alternateSymbol); } + @Override + public Unit shift(QuantityValue v) { + return ConvertedUnit.of(this, UnitConverter.shift(v)); + } + @Override public Unit multiply(QuantityValue v) { return baseUnit.multiply(v); diff --git a/src/main/java/io/github/ititus/si/unit/BaseUnit.java b/src/main/java/io/github/ititus/si/unit/BaseUnit.java index 68ddd37..fc09db3 100644 --- a/src/main/java/io/github/ititus/si/unit/BaseUnit.java +++ b/src/main/java/io/github/ititus/si/unit/BaseUnit.java @@ -44,6 +44,11 @@ public > Unit as(T type) { return new BaseUnit<>(type, symbol); } + @Override + public Unit shift(QuantityValue v) { + return ConvertedUnit.of(this, UnitConverter.shift(v)); + } + @Override public Unit multiply(QuantityValue v) { return ConvertedUnit.of(this, UnitConverter.factor(v)); diff --git a/src/main/java/io/github/ititus/si/unit/CompoundUnit.java b/src/main/java/io/github/ititus/si/unit/CompoundUnit.java index 537bd49..a121231 100644 --- a/src/main/java/io/github/ititus/si/unit/CompoundUnit.java +++ b/src/main/java/io/github/ititus/si/unit/CompoundUnit.java @@ -162,6 +162,11 @@ public > Unit as(T type) { return new CompoundUnit<>(type, getDimension(), units); } + @Override + public Unit shift(QuantityValue v) { + return ConvertedUnit.of(this, UnitConverter.shift(v)); + } + @Override public Unit multiply(QuantityValue v) { return ConvertedUnit.of(this, UnitConverter.factor(v)); diff --git a/src/main/java/io/github/ititus/si/unit/ConvertedUnit.java b/src/main/java/io/github/ititus/si/unit/ConvertedUnit.java index ad0d282..01e723e 100644 --- a/src/main/java/io/github/ititus/si/unit/ConvertedUnit.java +++ b/src/main/java/io/github/ititus/si/unit/ConvertedUnit.java @@ -28,7 +28,7 @@ public static > Unit of(Unit baseUnit, UnitConve @Override public String getSymbol() { - throw new UnsupportedOperationException("converted units have not symbol"); + throw new UnsupportedOperationException("converted units have no symbol"); } @Override @@ -56,6 +56,11 @@ public > Unit as(T type) { return new ConvertedUnit<>(baseUnit.as(type), converter); } + @Override + public Unit shift(QuantityValue v) { + return of(baseUnit, converter.concat(UnitConverter.shift(v))); + } + @Override public Unit multiply(QuantityValue v) { return of(baseUnit, converter.concat(UnitConverter.factor(v))); diff --git a/src/main/java/io/github/ititus/si/unit/PrefixUnit.java b/src/main/java/io/github/ititus/si/unit/PrefixUnit.java index 0971b2b..a7358b9 100644 --- a/src/main/java/io/github/ititus/si/unit/PrefixUnit.java +++ b/src/main/java/io/github/ititus/si/unit/PrefixUnit.java @@ -58,6 +58,11 @@ public > Unit as(T type) { return new PrefixUnit<>(baseUnit.as(type), prefix); } + @Override + public Unit shift(QuantityValue v) { + return ConvertedUnit.of(this, UnitConverter.shift(v)); + } + @Override public Unit multiply(QuantityValue v) { return ConvertedUnit.of(this, UnitConverter.factor(v)); diff --git a/src/main/java/io/github/ititus/si/unit/Unit.java b/src/main/java/io/github/ititus/si/unit/Unit.java index 84c3866..21f74e0 100644 --- a/src/main/java/io/github/ititus/si/unit/Unit.java +++ b/src/main/java/io/github/ititus/si/unit/Unit.java @@ -59,6 +59,36 @@ default > boolean isCommensurableWith(T type) { > Unit as(T type); + default Unit shift(int n) { + return shift(BigRational.of(n)); + } + + default Unit shift(long n) { + return shift(QuantityValue.of(n)); + } + + default Unit shift(BigInteger n) { + return shift(QuantityValue.of(n)); + } + + default Unit shift(float f) { + return shift(QuantityValue.of(f)); + } + + default Unit shift(double d) { + return shift(QuantityValue.of(d)); + } + + default Unit shift(BigDecimal d) { + return shift(QuantityValue.of(d)); + } + + default Unit shift(BigRational r) { + return shift(QuantityValue.of(r)); + } + + Unit shift(QuantityValue v); + default Unit multiply(int n) { return multiply(BigRational.of(n)); } diff --git a/src/main/java/io/github/ititus/si/unit/Units.java b/src/main/java/io/github/ititus/si/unit/Units.java index eb0fd85..2c39b70 100644 --- a/src/main/java/io/github/ititus/si/unit/Units.java +++ b/src/main/java/io/github/ititus/si/unit/Units.java @@ -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; @@ -68,21 +69,21 @@ public final class Units { // Length public static final Unit KILOMETRE = METRE.prefix(KILO); public static final Unit DECIMETRE = METRE.prefix(DECI); + public static final Unit CUBIC_DECIMETRE = DECIMETRE.pow(3).as(VOLUME); + public static final Unit LITRE = CUBIC_DECIMETRE.alternate("l"); public static final Unit MILLIMETRE = METRE.prefix(MILLI); public static final Unit INCH = MILLIMETRE.multiply(ofExp(254, -1)).alternate("in"); public static final Unit FOOT = INCH.multiply(12).alternate("ft"); public static final Unit YARD = FOOT.multiply(3).alternate("yd"); public static final Unit MILE = YARD.multiply(1760).alternate("mi"); + public static final Unit FATHOM = YARD.multiply(2).alternate("ftm"); public static final Unit PICA = INCH.divide(6).alternate("pica"); public static final Unit POINT = PICA.divide(12).alternate("pt"); - public static final Unit FATHOM = YARD.multiply(2).alternate("ftm"); public static final Unit NAUTICAL_MILE = METRE.multiply(1_852).alternate("nmi"); public static final Unit CABLE = NAUTICAL_MILE.divide(10).alternate("cb"); - // Mass public static final Unit KILOGRAM = GRAM.prefix(KILO); public static final Unit TONNE = KILOGRAM.multiply(1_000).alternate("t"); - public static final Unit CARAT = GRAM.divide(5).alternate("ct"); public static final Unit POUND = KILOGRAM.multiply(ofExp(45_359_237, -8)).alternate("lb"); public static final Unit OUNCE = POUND.divide(16).alternate("oz"); public static final Unit DRAM = OUNCE.divide(16).alternate("dr"); @@ -90,128 +91,97 @@ public final class Units { public static final Unit PENNYWEIGHT = GRAIN.multiply(24).alternate("dwt"); public static final Unit TROY_OUNCE = PENNYWEIGHT.multiply(20).alternate("oz t"); public static final Unit TROY_POUND = TROY_OUNCE.multiply(12).alternate("lb t"); - + // Momentum + public static final Unit KILOGRAM_METRES_PER_SECOND = + KILOGRAM.multiply(METRE.divide(SECOND)).as(MOMENTUM); + public static final Unit CARAT = GRAM.divide(5).alternate("ct"); // Time public static final Unit