From 3c23fd15b88396868101457256173c0c2c29df5c Mon Sep 17 00:00:00 2001 From: Adrien Hopkins Date: Mon, 21 Oct 2019 21:27:59 -0400 Subject: Added unit names and the NameSymbol. --- src/org/unitConverter/unit/BaseUnit.java | 54 +++++++++++++++++--------------- 1 file changed, 29 insertions(+), 25 deletions(-) (limited to 'src/org/unitConverter/unit/BaseUnit.java') diff --git a/src/org/unitConverter/unit/BaseUnit.java b/src/org/unitConverter/unit/BaseUnit.java index 8f44861..8fd0664 100644 --- a/src/org/unitConverter/unit/BaseUnit.java +++ b/src/org/unitConverter/unit/BaseUnit.java @@ -16,10 +16,15 @@ */ package org.unitConverter.unit; +import java.util.HashSet; import java.util.Objects; +import java.util.Set; /** * A unit that other units are defined by. + *

+ * Note that BaseUnits must have names and symbols. This is because they are used for toString code. Therefore, + * the Optionals provided by {@link #getPrimaryName} and {@link #getSymbol} will always contain a value. * * @author Adrien Hopkins * @since 2019-10-16 @@ -38,19 +43,34 @@ public final class BaseUnit extends Unit { * @since 2019-10-16 */ public static BaseUnit valueOf(final BaseDimension dimension, final String name, final String symbol) { - return new BaseUnit(dimension, name, symbol); + return new BaseUnit(dimension, name, symbol, new HashSet<>()); + } + + /** + * Gets a base unit from the dimension it measures, its name and its symbol. + * + * @param dimension + * dimension measured by this unit + * @param name + * name of unit + * @param symbol + * symbol of unit + * @return base unit + * @since 2019-10-21 + */ + public static BaseUnit valueOf(final BaseDimension dimension, final String name, final String symbol, + final Set otherNames) { + return new BaseUnit(dimension, name, symbol, otherNames); } private final BaseDimension dimension; - private final String name; - private final String symbol; /** * Creates the {@code BaseUnit}. * * @param dimension * dimension of unit - * @param name + * @param primaryName * name of unit * @param symbol * symbol of unit @@ -58,11 +78,10 @@ public final class BaseUnit extends Unit { * if any argument is null * @since 2019-10-16 */ - private BaseUnit(final BaseDimension dimension, final String name, final String symbol) { - super(); + private BaseUnit(final BaseDimension dimension, final String primaryName, final String symbol, + final Set otherNames) { + super(primaryName, symbol, otherNames); this.dimension = Objects.requireNonNull(dimension, "dimension must not be null."); - this.name = Objects.requireNonNull(name, "name must not be null."); - this.symbol = Objects.requireNonNull(symbol, "symbol must not be null."); } /** @@ -94,24 +113,9 @@ public final class BaseUnit extends Unit { return this.dimension; } - /** - * @return name - * @since 2019-10-16 - */ - public final String getName() { - return this.name; - } - - /** - * @return symbol - * @since 2019-10-16 - */ - public final String getSymbol() { - return this.symbol; - } - @Override public String toString() { - return String.format("%s (%s)", this.getName(), this.getSymbol()); + return this.getPrimaryName().orElse("Unnamed unit") + + (this.getSymbol().isPresent() ? String.format(" (%s)", this.getSymbol().get()) : ""); } } -- cgit v1.2.3 From 8ec94bea790cc010c29cd8de86e47117ff331979 Mon Sep 17 00:00:00 2001 From: Adrien Hopkins Date: Mon, 21 Oct 2019 21:41:26 -0400 Subject: Added new ways to create named units. --- src/org/unitConverter/unit/BaseUnit.java | 11 ++++++ src/org/unitConverter/unit/LinearUnit.java | 56 ++++++++++++++++++++++++++++-- src/org/unitConverter/unit/Unit.java | 13 +++++++ 3 files changed, 77 insertions(+), 3 deletions(-) (limited to 'src/org/unitConverter/unit/BaseUnit.java') diff --git a/src/org/unitConverter/unit/BaseUnit.java b/src/org/unitConverter/unit/BaseUnit.java index 8fd0664..e9ef3fa 100644 --- a/src/org/unitConverter/unit/BaseUnit.java +++ b/src/org/unitConverter/unit/BaseUnit.java @@ -118,4 +118,15 @@ public final class BaseUnit extends Unit { return this.getPrimaryName().orElse("Unnamed unit") + (this.getSymbol().isPresent() ? String.format(" (%s)", this.getSymbol().get()) : ""); } + + @Override + public BaseUnit withName(final NameSymbol ns) { + Objects.requireNonNull(ns, "ns must not be null."); + if (!ns.getPrimaryName().isPresent()) + throw new IllegalArgumentException("BaseUnits must have primary names."); + if (!ns.getSymbol().isPresent()) + throw new IllegalArgumentException("BaseUnits must have symbols."); + return BaseUnit.valueOf(this.getBaseDimension(), ns.getPrimaryName().get(), ns.getSymbol().get(), + ns.getOtherNames()); + } } 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 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 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 unitBase, final NameSymbol ns, final double conversionFactor) { + private LinearUnit(final ObjectProduct 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. * diff --git a/src/org/unitConverter/unit/Unit.java b/src/org/unitConverter/unit/Unit.java index d65e14f..d848ea1 100644 --- a/src/org/unitConverter/unit/Unit.java +++ b/src/org/unitConverter/unit/Unit.java @@ -513,4 +513,17 @@ public abstract class Unit { + (this.getSymbol().isPresent() ? String.format(" (%s)", this.getSymbol().get()) : "") + ", derived from " + this.getBase().toString(); } + + /** + * @param ns + * name(s) and symbol to use + * @return a copy of this unit with provided name(s) and symbol + * @since 2019-10-21 + * @throws NullPointerException + * if ns is null + */ + public Unit withName(final NameSymbol ns) { + return fromConversionFunctions(this.getBase(), this::convertFromBase, this::convertToBase, + Objects.requireNonNull(ns, "ns must not be null.")); + } } -- cgit v1.2.3