diff options
Diffstat (limited to 'src/org/unitConverter/unit/LinearUnit.java')
-rw-r--r-- | src/org/unitConverter/unit/LinearUnit.java | 56 |
1 files changed, 53 insertions, 3 deletions
diff --git a/src/org/unitConverter/unit/LinearUnit.java b/src/org/unitConverter/unit/LinearUnit.java index 2a55dea..7b7338b 100644 --- a/src/org/unitConverter/unit/LinearUnit.java +++ b/src/org/unitConverter/unit/LinearUnit.java @@ -38,9 +38,32 @@ public final class LinearUnit extends Unit { * value to convert * @return value expressed as a {@code LinearUnit} * @since 2019-10-16 + * @throws NullPointerException + * if unit is null */ public static LinearUnit fromUnitValue(final Unit unit, final double value) { - return new LinearUnit(unit.getBase(), NameSymbol.EMPTY, unit.convertToBase(value)); + return new LinearUnit(Objects.requireNonNull(unit, "unit must not be null.").getBase(), + unit.convertToBase(value), NameSymbol.EMPTY); + } + + /** + * Gets a {@code LinearUnit} from a unit and a value. For example, converts '59 °F' to a linear unit with the value + * of '288.15 K' + * + * @param unit + * unit to convert + * @param value + * value to convert + * @param ns + * name(s) and symbol of unit + * @return value expressed as a {@code LinearUnit} + * @since 2019-10-21 + * @throws NullPointerException + * if unit or ns is null + */ + public static LinearUnit fromUnitValue(final Unit unit, final double value, final NameSymbol ns) { + return new LinearUnit(Objects.requireNonNull(unit, "unit must not be null.").getBase(), + unit.convertToBase(value), ns); } /** @@ -53,9 +76,31 @@ public final class LinearUnit extends Unit { * number to multiply base by * @return product of base and conversion factor * @since 2019-10-16 + * @throws NullPointerException + * if unitBase is null */ public static LinearUnit valueOf(final ObjectProduct<BaseUnit> unitBase, final double conversionFactor) { - return new LinearUnit(unitBase, NameSymbol.EMPTY, conversionFactor); + return new LinearUnit(unitBase, conversionFactor, NameSymbol.EMPTY); + } + + /** + * Gets a {@code LinearUnit} from a unit base and a conversion factor. In other words, gets the product of + * {@code unitBase} and {@code conversionFactor}, expressed as a {@code LinearUnit}. + * + * @param unitBase + * unit base to multiply by + * @param conversionFactor + * number to multiply base by + * @param ns + * name(s) and symbol of unit + * @return product of base and conversion factor + * @since 2019-10-21 + * @throws NullPointerException + * if unitBase is null + */ + public static LinearUnit valueOf(final ObjectProduct<BaseUnit> unitBase, final double conversionFactor, + final NameSymbol ns) { + return new LinearUnit(unitBase, conversionFactor, ns); } /** @@ -78,7 +123,7 @@ public final class LinearUnit extends Unit { * conversion factor between base and unit * @since 2019-10-16 */ - private LinearUnit(final ObjectProduct<BaseUnit> unitBase, final NameSymbol ns, final double conversionFactor) { + private LinearUnit(final ObjectProduct<BaseUnit> unitBase, final double conversionFactor, final NameSymbol ns) { super(unitBase, ns); this.conversionFactor = conversionFactor; } @@ -275,6 +320,11 @@ public final class LinearUnit extends Unit { + Double.toString(this.conversionFactor) + " * " + this.getBase().toString(u -> u.getSymbol().get()); } + @Override + public LinearUnit withName(final NameSymbol ns) { + return valueOf(this.getBase(), this.getConversionFactor(), ns); + } + /** * Returns the result of applying {@code prefix} to this unit. * |