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.java100
1 files changed, 64 insertions, 36 deletions
diff --git a/src/main/java/sevenUnits/unit/UnitPrefix.java b/src/main/java/sevenUnits/unit/UnitPrefix.java
index 9035969..af106b9 100644
--- a/src/main/java/sevenUnits/unit/UnitPrefix.java
+++ b/src/main/java/sevenUnits/unit/UnitPrefix.java
@@ -1,5 +1,5 @@
/**
- * Copyright (C) 2019 Adrien Hopkins
+ * Copyright (C) 2019, 2021, 2022, 2024, 2025 Adrien Hopkins
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
@@ -25,17 +25,19 @@ import sevenUnits.utils.Nameable;
/**
* A prefix that can be applied to a {@code LinearUnit} to multiply it by some
* value
- *
+ *
* @author Adrien Hopkins
* @since 2019-10-16
+ * @since v0.3.0
*/
public final class UnitPrefix implements Nameable {
/**
* Gets a {@code UnitPrefix} from a multiplier
- *
+ *
* @param multiplier multiplier of prefix
* @return prefix
* @since 2019-10-16
+ * @since v0.3.0
*/
public static UnitPrefix valueOf(final double multiplier) {
return new UnitPrefix(multiplier, NameSymbol.EMPTY);
@@ -43,11 +45,12 @@ public final class UnitPrefix implements Nameable {
/**
* 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
+ * @since v0.3.0
* @throws NullPointerException if ns is null
*/
public static UnitPrefix valueOf(final double multiplier,
@@ -58,21 +61,23 @@ public final class UnitPrefix implements Nameable {
/**
* This prefix's name(s) and symbol.
- *
+ *
* @since 2022-04-16
+ * @since v0.4.0
*/
private final NameSymbol nameSymbol;
/**
* The number that this prefix multiplies units by
- *
+ *
* @since 2019-10-16
+ * @since v0.3.0
*/
private final double multiplier;
/**
* Creates the {@code DefaultUnitPrefix}.
- *
+ *
* @param multiplier
* @since 2019-01-14
* @since v0.2.0
@@ -84,10 +89,11 @@ public final class UnitPrefix implements Nameable {
/**
* Divides this prefix by a scalar
- *
+ *
* @param divisor number to divide by
* @return quotient of prefix and scalar
* @since 2019-10-16
+ * @since v0.3.0
*/
public UnitPrefix dividedBy(final double divisor) {
return valueOf(this.getMultiplier() / divisor);
@@ -95,7 +101,7 @@ public final class UnitPrefix implements Nameable {
/**
* Divides this prefix by {@code other}.
- *
+ *
* @param other prefix to divide by
* @return quotient of prefixes
* @since 2019-04-13
@@ -107,18 +113,32 @@ public final class UnitPrefix implements Nameable {
/**
* {@inheritDoc}
- *
+ *
* Uses the prefix's multiplier to determine equality.
*/
@Override
public boolean equals(final Object obj) {
if (this == obj)
return true;
- if (obj == null)
+ if ((obj == null) || !(obj instanceof UnitPrefix))
return false;
- if (!(obj instanceof UnitPrefix))
+ final var other = (UnitPrefix) obj;
+ return Double.compare(this.getMultiplier(), other.getMultiplier()) == 0;
+ }
+
+ /**
+ * @param other prefix to compare to
+ * @return true iff this prefix and other are equal, ignoring small
+ * differences caused by floating-point error.
+ *
+ * @apiNote This method is not transitive, so it cannot be used as an equals
+ * method.
+ */
+ public boolean equalsApproximately(final UnitPrefix other) {
+ if (this == other)
+ return true;
+ if (other == null)
return false;
- final UnitPrefix other = (UnitPrefix) obj;
return DecimalComparison.equals(this.getMultiplier(),
other.getMultiplier());
}
@@ -126,6 +146,7 @@ public final class UnitPrefix implements Nameable {
/**
* @return prefix's multiplier
* @since 2019-11-26
+ * @since v0.3.0
*/
public double getMultiplier() {
return this.multiplier;
@@ -138,46 +159,55 @@ public final class UnitPrefix implements Nameable {
/**
* {@inheritDoc}
- *
+ *
* Uses the prefix's multiplier to determine a hash code.
*/
@Override
public int hashCode() {
- return DecimalComparison.hash(this.getMultiplier());
+ return Double.hashCode(this.getMultiplier());
}
/**
- * Multiplies this prefix by a scalar
- *
- * @param multiplicand number to multiply by
- * @return product of prefix and scalar
- * @since 2019-10-16
+ * Subtracts {@code other} from this prefix and returns the result.
+ *
+ * @param other prefix to subtract
+ * @return difference of prefixes
+ *
+ * @since 2024-03-03
+ * @since v0.5.0
*/
- public UnitPrefix times(final double multiplicand) {
- return valueOf(this.getMultiplier() * multiplicand);
+ public UnitPrefix minus(final UnitPrefix other) {
+ return valueOf(this.getMultiplier() - other.getMultiplier());
}
/**
* Adds {@code other} to this prefix and returns the result.
- *
+ *
+ * @param other prefix to add
+ * @return sum of prefixes
+ *
* @since 2024-03-03
+ * @since v0.5.0
*/
public UnitPrefix plus(final UnitPrefix other) {
return valueOf(this.getMultiplier() + other.getMultiplier());
}
/**
- * Subtracts {@code other} from this prefix and returns the result.
- *
- * @since 2024-03-03
+ * Multiplies this prefix by a scalar
+ *
+ * @param multiplicand number to multiply by
+ * @return product of prefix and scalar
+ * @since 2019-10-16
+ * @since v0.3.0
*/
- public UnitPrefix minus(final UnitPrefix other) {
- return valueOf(this.getMultiplier() - other.getMultiplier());
+ public UnitPrefix times(final double multiplicand) {
+ return valueOf(this.getMultiplier() * multiplicand);
}
/**
* Multiplies this prefix by {@code other}.
- *
+ *
* @param other prefix to multiply by
* @return product of prefixes
* @since 2019-04-13
@@ -189,7 +219,7 @@ public final class UnitPrefix implements Nameable {
/**
* Raises this prefix to an exponent.
- *
+ *
* @param exponent exponent to raise to
* @return result of exponentiation.
* @since 2019-04-13
@@ -199,25 +229,23 @@ public final class UnitPrefix implements Nameable {
return valueOf(Math.pow(this.getMultiplier(), exponent));
}
- /**
- * @return a string describing the prefix and its multiplier
- */
+ /** @return a string describing the prefix and its multiplier */
@Override
public String toString() {
if (this.getPrimaryName().isPresent())
return String.format("%s (\u00D7 %s)", this.getPrimaryName().get(),
this.multiplier);
- else if (this.getSymbol().isPresent())
+ 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);
+ 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
+ * @since v0.3.0
* @throws NullPointerException if ns is null
*/
public UnitPrefix withName(final NameSymbol ns) {