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/UnitPrefix.java | 85 ++++++++++++++++++++++++++++-- 1 file changed, 82 insertions(+), 3 deletions(-) (limited to 'src/org/unitConverter/unit/UnitPrefix.java') diff --git a/src/org/unitConverter/unit/UnitPrefix.java b/src/org/unitConverter/unit/UnitPrefix.java index 360b0c1..31cc0b3 100644 --- a/src/org/unitConverter/unit/UnitPrefix.java +++ b/src/org/unitConverter/unit/UnitPrefix.java @@ -16,6 +16,10 @@ */ package org.unitConverter.unit; +import java.util.Objects; +import java.util.Optional; +import java.util.Set; + import org.unitConverter.math.DecimalComparison; /** @@ -34,9 +38,40 @@ public final class UnitPrefix { * @since 2019-10-16 */ public static UnitPrefix valueOf(final double multiplier) { - return new UnitPrefix(multiplier); + return new UnitPrefix(multiplier, NameSymbol.EMPTY); + } + + /** + * Gets a {@code UnitPrefix} from a multiplier and a name + * + * @param multiplier + * multiplier of prefix + * @param ns + * name(s) and symbol of prefix + * @return prefix + * @since 2019-10-16 + * @throws NullPointerException + * if ns is null + */ + public static UnitPrefix valueOf(final double multiplier, final NameSymbol ns) { + return new UnitPrefix(multiplier, Objects.requireNonNull(ns, "ns must not be null.")); } + /** + * This prefix's primary name + */ + private final Optional primaryName; + + /** + * This prefix's symbol + */ + private final Optional symbol; + + /** + * Other names and symbols used by this prefix + */ + private final Set otherNames; + /** * The number that this prefix multiplies units by * @@ -51,8 +86,11 @@ public final class UnitPrefix { * @since 2019-01-14 * @since v0.2.0 */ - private UnitPrefix(final double multiplier) { + private UnitPrefix(final double multiplier, final NameSymbol ns) { this.multiplier = multiplier; + this.primaryName = ns.getPrimaryName(); + this.symbol = ns.getSymbol(); + this.otherNames = ns.getOtherNames(); } /** @@ -105,6 +143,30 @@ public final class UnitPrefix { return this.multiplier; } + /** + * @return other names + * @since 2019-11-26 + */ + public final Set getOtherNames() { + return this.otherNames; + } + + /** + * @return primary name + * @since 2019-11-26 + */ + public final Optional getPrimaryName() { + return this.primaryName; + } + + /** + * @return symbol + * @since 2019-11-26 + */ + public final Optional getSymbol() { + return this.symbol; + } + /** * {@inheritDoc} * @@ -158,6 +220,23 @@ public final class UnitPrefix { */ @Override public String toString() { - return String.format("Unit prefix with multiplier %s", this.multiplier); + if (this.primaryName.isPresent()) + return String.format("%s (\u00D7 %s)", this.primaryName.get(), this.multiplier); + else if (this.symbol.isPresent()) + return String.format("%s (\u00D7 %s)", this.symbol.get(), this.multiplier); + else + return String.format("Unit Prefix (\u00D7 %s)", this.multiplier); + } + + /** + * @param ns + * name(s) and symbol to use + * @return copy of this prefix with provided name(s) and symbol + * @since 2019-11-26 + * @throws NullPointerException + * if ns is null + */ + public UnitPrefix withName(final NameSymbol ns) { + return valueOf(this.multiplier, ns); } } -- cgit v1.2.3