summaryrefslogtreecommitdiff
path: root/src/org/unitConverter/unit/UnitPrefix.java
diff options
context:
space:
mode:
authorAdrien Hopkins <masterofnumbers17@gmail.com>2019-10-17 14:25:17 -0400
committerAdrien Hopkins <masterofnumbers17@gmail.com>2019-10-17 14:25:17 -0400
commit54ab9c05234b09547e2a01b1eab812420c6a3dda (patch)
treec9f699fada5b692725f3c4b884db23f24b1d8c4f /src/org/unitConverter/unit/UnitPrefix.java
parentf309ef0b9ed24629146d1d92a5c869946a6d65a2 (diff)
Implemented the new Units system
Fahrenheit has temporarily been removed; it will be back.
Diffstat (limited to 'src/org/unitConverter/unit/UnitPrefix.java')
-rw-r--r--src/org/unitConverter/unit/UnitPrefix.java104
1 files changed, 89 insertions, 15 deletions
diff --git a/src/org/unitConverter/unit/UnitPrefix.java b/src/org/unitConverter/unit/UnitPrefix.java
index 9f9645d..514fa1c 100644
--- a/src/org/unitConverter/unit/UnitPrefix.java
+++ b/src/org/unitConverter/unit/UnitPrefix.java
@@ -1,5 +1,5 @@
/**
- * Copyright (C) 2018 Adrien Hopkins
+ * Copyright (C) 2019 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
@@ -16,14 +16,57 @@
*/
package org.unitConverter.unit;
+import org.unitConverter.math.DecimalComparison;
+
/**
- * A prefix that can be attached onto the front of any unit, which multiplies it by a certain value
+ * A prefix that can be applied to a {@code LinearUnit} to multiply it by some value
*
* @author Adrien Hopkins
- * @since 2019-01-14
- * @since v0.1.0
+ * @since 2019-10-16
*/
-public interface UnitPrefix {
+public final class UnitPrefix {
+ /**
+ * Gets a {@code UnitPrefix} from a multiplier
+ *
+ * @param multiplier
+ * multiplier of prefix
+ * @return prefix
+ * @since 2019-10-16
+ */
+ public static UnitPrefix valueOf(final double multiplier) {
+ return new UnitPrefix(multiplier);
+ }
+
+ /**
+ * The number that this prefix multiplies units by
+ *
+ * @since 2019-10-16
+ */
+ private final double multiplier;
+
+ /**
+ * Creates the {@code DefaultUnitPrefix}.
+ *
+ * @param multiplier
+ * @since 2019-01-14
+ * @since v0.2.0
+ */
+ private UnitPrefix(final double multiplier) {
+ this.multiplier = multiplier;
+ }
+
+ /**
+ * Divides this prefix by a scalar
+ *
+ * @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}.
*
@@ -33,16 +76,42 @@ public interface UnitPrefix {
* @since 2019-04-13
* @since v0.2.0
*/
- default UnitPrefix dividedBy(final UnitPrefix other) {
- return new DefaultUnitPrefix(this.getMultiplier() / other.getMultiplier());
+ public UnitPrefix dividedBy(final UnitPrefix other) {
+ return valueOf(this.getMultiplier() / other.getMultiplier());
+ }
+
+ @Override
+ public boolean equals(final Object obj) {
+ if (this == obj)
+ return true;
+ if (obj == null)
+ return false;
+ if (!(obj instanceof UnitPrefix))
+ return false;
+ final UnitPrefix other = (UnitPrefix) obj;
+ return DecimalComparison.equals(this.getMultiplier(), other.getMultiplier());
+ }
+
+ public double getMultiplier() {
+ return this.multiplier;
+ }
+
+ @Override
+ public int hashCode() {
+ return DecimalComparison.hash(this.getMultiplier());
}
/**
- * @return this prefix's multiplier
- * @since 2019-01-14
- * @since v0.1.0
+ * Multiplies this prefix by a scalar
+ *
+ * @param multiplicand
+ * number to multiply by
+ * @return product of prefix and scalar
+ * @since 2019-10-16
*/
- double getMultiplier();
+ public UnitPrefix times(final double multiplicand) {
+ return valueOf(this.getMultiplier() * multiplicand);
+ }
/**
* Multiplies this prefix by {@code other}.
@@ -53,8 +122,8 @@ public interface UnitPrefix {
* @since 2019-04-13
* @since v0.2.0
*/
- default UnitPrefix times(final UnitPrefix other) {
- return new DefaultUnitPrefix(this.getMultiplier() * other.getMultiplier());
+ public UnitPrefix times(final UnitPrefix other) {
+ return valueOf(this.getMultiplier() * other.getMultiplier());
}
/**
@@ -66,7 +135,12 @@ public interface UnitPrefix {
* @since 2019-04-13
* @since v0.2.0
*/
- default UnitPrefix toExponent(final double exponent) {
- return new DefaultUnitPrefix(Math.pow(getMultiplier(), exponent));
+ public UnitPrefix toExponent(final double exponent) {
+ return valueOf(Math.pow(this.getMultiplier(), exponent));
+ }
+
+ @Override
+ public String toString() {
+ return String.format("Unit prefix equal to %s", this.multiplier);
}
}