diff options
Diffstat (limited to 'src/org/unitConverter/unit/UnitPrefix.java')
-rw-r--r-- | src/org/unitConverter/unit/UnitPrefix.java | 85 |
1 files changed, 82 insertions, 3 deletions
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,10 +38,41 @@ 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<String> primaryName; + + /** + * This prefix's symbol + */ + private final Optional<String> symbol; + + /** + * Other names and symbols used by this prefix + */ + private final Set<String> otherNames; + + /** * The number that this prefix multiplies units by * * @since 2019-10-16 @@ -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(); } /** @@ -106,6 +144,30 @@ public final class UnitPrefix { } /** + * @return other names + * @since 2019-11-26 + */ + public final Set<String> getOtherNames() { + return this.otherNames; + } + + /** + * @return primary name + * @since 2019-11-26 + */ + public final Optional<String> getPrimaryName() { + return this.primaryName; + } + + /** + * @return symbol + * @since 2019-11-26 + */ + public final Optional<String> getSymbol() { + return this.symbol; + } + + /** * {@inheritDoc} * * Uses the prefix's multiplier to determine a hash code. @@ -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); } } |