summaryrefslogtreecommitdiff
path: root/src/main/java/sevenUnits/unit/UnitPrefix.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/sevenUnits/unit/UnitPrefix.java')
-rw-r--r--src/main/java/sevenUnits/unit/UnitPrefix.java136
1 files changed, 51 insertions, 85 deletions
diff --git a/src/main/java/sevenUnits/unit/UnitPrefix.java b/src/main/java/sevenUnits/unit/UnitPrefix.java
index 308f4b0..e1f7788 100644
--- a/src/main/java/sevenUnits/unit/UnitPrefix.java
+++ b/src/main/java/sevenUnits/unit/UnitPrefix.java
@@ -17,68 +17,59 @@
package sevenUnits.unit;
import java.util.Objects;
-import java.util.Optional;
-import java.util.Set;
import sevenUnits.utils.DecimalComparison;
+import sevenUnits.utils.NameSymbol;
+import sevenUnits.utils.Nameable;
/**
- * A prefix that can be applied to a {@code LinearUnit} to multiply it by some value
+ * A prefix that can be applied to a {@code LinearUnit} to multiply it by some
+ * value
*
* @author Adrien Hopkins
* @since 2019-10-16
*/
-public final class UnitPrefix {
+public final class UnitPrefix implements Nameable {
/**
* Gets a {@code UnitPrefix} from a multiplier
*
- * @param multiplier
- * multiplier of prefix
+ * @param multiplier multiplier of prefix
* @return prefix
* @since 2019-10-16
*/
public static UnitPrefix valueOf(final double 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
+ * @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
+ * @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."));
+ 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
+ * This prefix's name(s) and symbol.
+ *
+ * @since 2022-04-16
*/
- private final Set<String> otherNames;
-
+ private final NameSymbol nameSymbol;
+
/**
* The number that this prefix multiplies units by
*
* @since 2019-10-16
*/
private final double multiplier;
-
+
/**
* Creates the {@code DefaultUnitPrefix}.
*
@@ -88,28 +79,24 @@ public final class UnitPrefix {
*/
private UnitPrefix(final double multiplier, final NameSymbol ns) {
this.multiplier = multiplier;
- this.primaryName = ns.getPrimaryName();
- this.symbol = ns.getSymbol();
- this.otherNames = ns.getOtherNames();
+ this.nameSymbol = ns;
}
-
+
/**
* Divides this prefix by a scalar
*
- * @param divisor
- * number to divide by
+ * @param divisor number to divide by
* @return quotient of prefix and scalar
* @since 2019-10-16
*/
public UnitPrefix dividedBy(final double divisor) {
return valueOf(this.getMultiplier() / divisor);
}
-
+
/**
* Divides this prefix by {@code other}.
*
- * @param other
- * prefix to divide by
+ * @param other prefix to divide by
* @return quotient of prefixes
* @since 2019-04-13
* @since v0.2.0
@@ -117,7 +104,7 @@ public final class UnitPrefix {
public UnitPrefix dividedBy(final UnitPrefix other) {
return valueOf(this.getMultiplier() / other.getMultiplier());
}
-
+
/**
* {@inheritDoc}
*
@@ -132,9 +119,10 @@ public final class UnitPrefix {
if (!(obj instanceof UnitPrefix))
return false;
final UnitPrefix other = (UnitPrefix) obj;
- return DecimalComparison.equals(this.getMultiplier(), other.getMultiplier());
+ return DecimalComparison.equals(this.getMultiplier(),
+ other.getMultiplier());
}
-
+
/**
* @return prefix's multiplier
* @since 2019-11-26
@@ -142,31 +130,12 @@ public final class UnitPrefix {
public double getMultiplier() {
return this.multiplier;
}
-
- /**
- * @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;
+
+ @Override
+ public NameSymbol getNameSymbol() {
+ return this.nameSymbol;
}
-
+
/**
* {@inheritDoc}
*
@@ -176,24 +145,22 @@ public final class UnitPrefix {
public int hashCode() {
return DecimalComparison.hash(this.getMultiplier());
}
-
+
/**
* Multiplies this prefix by a scalar
*
- * @param multiplicand
- * number to multiply by
+ * @param multiplicand number to multiply by
* @return product of prefix and scalar
* @since 2019-10-16
*/
public UnitPrefix times(final double multiplicand) {
return valueOf(this.getMultiplier() * multiplicand);
}
-
+
/**
* Multiplies this prefix by {@code other}.
*
- * @param other
- * prefix to multiply by
+ * @param other prefix to multiply by
* @return product of prefixes
* @since 2019-04-13
* @since v0.2.0
@@ -201,12 +168,11 @@ public final class UnitPrefix {
public UnitPrefix times(final UnitPrefix other) {
return valueOf(this.getMultiplier() * other.getMultiplier());
}
-
+
/**
* Raises this prefix to an exponent.
*
- * @param exponent
- * exponent to raise to
+ * @param exponent exponent to raise to
* @return result of exponentiation.
* @since 2019-04-13
* @since v0.2.0
@@ -214,27 +180,27 @@ public final class UnitPrefix {
public UnitPrefix toExponent(final double exponent) {
return valueOf(Math.pow(this.getMultiplier(), exponent));
}
-
+
/**
* @return a string describing the prefix and its multiplier
*/
@Override
public String toString() {
- 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);
+ if (this.getPrimaryName().isPresent())
+ return String.format("%s (\u00D7 %s)", this.getPrimaryName().get(),
+ this.multiplier);
+ else if (this.getSymbol().isPresent())
+ return String.format("%s (\u00D7 %s)", this.getSymbol().get(),
+ this.multiplier);
else
return String.format("Unit Prefix (\u00D7 %s)", this.multiplier);
}
-
+
/**
- * @param ns
- * name(s) and symbol to use
+ * @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
+ * @throws NullPointerException if ns is null
*/
public UnitPrefix withName(final NameSymbol ns) {
return valueOf(this.multiplier, ns);