From bff8f363ce5e2ca84da1d57a470f0648c4b338e6 Mon Sep 17 00:00:00 2001 From: Adrien Hopkins Date: Tue, 26 Nov 2019 20:53:27 -0500 Subject: Prefixes can now have names. --- src/org/unitConverter/unit/LinearUnit.java | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) (limited to 'src/org/unitConverter/unit/LinearUnit.java') diff --git a/src/org/unitConverter/unit/LinearUnit.java b/src/org/unitConverter/unit/LinearUnit.java index 1532fc4..1e5ae53 100644 --- a/src/org/unitConverter/unit/LinearUnit.java +++ b/src/org/unitConverter/unit/LinearUnit.java @@ -350,14 +350,38 @@ public final class LinearUnit extends Unit { /** * Returns the result of applying {@code prefix} to this unit. + *

+ * If this unit and the provided prefix have a primary name, the returned unit will have a primary name (prefix's + * name + unit's name).
+ * If this unit and the provided prefix have a symbol, the returned unit will have a symbol.
+ * This method ignores alternate names of both this unit and the provided prefix. * * @param prefix * prefix to apply * @return unit with prefix * @since 2019-03-18 * @since v0.2.0 + * @throws NullPointerException + * if prefix is null */ public LinearUnit withPrefix(final UnitPrefix prefix) { - return this.times(prefix.getMultiplier()); + final LinearUnit unit = this.times(prefix.getMultiplier()); + + // create new name and symbol, if possible + final String name; + if (this.getPrimaryName().isPresent() && prefix.getPrimaryName().isPresent()) { + name = prefix.getPrimaryName().get() + this.getPrimaryName().get(); + } else { + name = null; + } + + final String symbol; + if (this.getSymbol().isPresent() && prefix.getSymbol().isPresent()) { + symbol = prefix.getSymbol().get() + this.getSymbol().get(); + } else { + symbol = null; + } + + return unit.withName(NameSymbol.ofNullable(name, symbol)); } } -- cgit v1.2.3