summaryrefslogtreecommitdiff
path: root/src/unitConverter/unit/BaseUnit.java
diff options
context:
space:
mode:
authorAdrien Hopkins <adrien.p.hopkins@gmail.com>2019-01-25 16:47:35 -0500
committerAdrien Hopkins <adrien.p.hopkins@gmail.com>2019-01-25 16:47:35 -0500
commite7d6b4ee2286dd9320550d95cb27020ee71bb9d1 (patch)
treefa7a6b228d9dc616e0ae53f9fbd6c4bd922948f0 /src/unitConverter/unit/BaseUnit.java
parentdb854ce6e5038900c4ca1ad79154a4cd13ee257e (diff)
Added multiplication, division and exponentitation to linear/base
Diffstat (limited to 'src/unitConverter/unit/BaseUnit.java')
-rwxr-xr-xsrc/unitConverter/unit/BaseUnit.java76
1 files changed, 76 insertions, 0 deletions
diff --git a/src/unitConverter/unit/BaseUnit.java b/src/unitConverter/unit/BaseUnit.java
index 339b7bf..46316bf 100755
--- a/src/unitConverter/unit/BaseUnit.java
+++ b/src/unitConverter/unit/BaseUnit.java
@@ -16,6 +16,8 @@
*/
package unitConverter.unit;
+import java.util.Objects;
+
import unitConverter.dimension.UnitDimension;
/**
@@ -61,6 +63,80 @@ public final class BaseUnit extends AbstractUnit {
return value;
}
+ /**
+ * Divides this unit by another unit.
+ *
+ * @param other
+ * unit to divide by
+ * @return quotient of two units
+ * @throws IllegalArgumentException
+ * if this unit's system is not other's system
+ * @throws NullPointerException
+ * if other is null
+ * @since 2018-12-22
+ */
+ public BaseUnit dividedBy(final BaseUnit other) {
+ Objects.requireNonNull(other, "other must not be null.");
+ if (!this.getSystem().equals(other.getSystem()))
+ throw new IllegalArgumentException("Incompatible base units for division.");
+ return new BaseUnit(this.getDimension().dividedBy(other.getDimension()), this.getSystem());
+ }
+
+ /**
+ * Divides this unit by a divisor
+ *
+ * @param divisor
+ * amount to divide by
+ * @return quotient
+ * @since 2018-12-23
+ */
+ public LinearUnit dividedBy(final double divisor) {
+ return new LinearUnit(this, 1 / divisor);
+ }
+
+ /**
+ * Multiplies this unit by another unit.
+ *
+ * @param other
+ * unit to multiply by
+ * @return product of two units
+ * @throws IllegalArgumentException
+ * if this unit's system is not other's system
+ * @throws NullPointerException
+ * if other is null
+ * @since 2018-12-22
+ */
+ public BaseUnit times(final BaseUnit other) {
+ Objects.requireNonNull(other, "other must not be null.");
+ if (!this.getSystem().equals(other.getSystem()))
+ throw new IllegalArgumentException("Incompatible base units for multiplication.");
+ return new BaseUnit(this.getDimension().times(other.getDimension()), this.getSystem());
+ }
+
+ /**
+ * Multiplies this unit by a multiplier.
+ *
+ * @param multiplier
+ * amount to multiply by
+ * @return product
+ * @since 2018-12-23B
+ */
+ public LinearUnit times(final double multiplier) {
+ return new LinearUnit(this, multiplier);
+ }
+
+ /**
+ * Returns this unit, but to an exponent.
+ *
+ * @param exponent
+ * exponent
+ * @return result of exponentiation
+ * @since 2019-01-15
+ */
+ public BaseUnit toExponent(final int exponent) {
+ return this.toExponent(exponent);
+ }
+
@Override
public String toString() {
return String.format("%s base unit of%s dimension %s", this.getSystem(), this.isFullBase ? " base" : "",