summaryrefslogtreecommitdiff
path: root/src/org/unitConverter/unit/Unit.java
diff options
context:
space:
mode:
authorAdrien Hopkins <adrien.p.hopkins@gmail.com>2019-05-22 17:32:40 -0400
committerAdrien Hopkins <adrien.p.hopkins@gmail.com>2019-05-22 17:32:40 -0400
commit987fd8406d65505aedecd17e51216eb0ce393fbb (patch)
treeb10a551a57cbd099450ffe539cb8d6d8e230459d /src/org/unitConverter/unit/Unit.java
parent50a195ef78af5d15dd6e548d4d6928c281bbaac2 (diff)
Added new default methods to the Unit interface.
Diffstat (limited to 'src/org/unitConverter/unit/Unit.java')
-rwxr-xr-xsrc/org/unitConverter/unit/Unit.java46
1 files changed, 46 insertions, 0 deletions
diff --git a/src/org/unitConverter/unit/Unit.java b/src/org/unitConverter/unit/Unit.java
index 86fc5a2..2ac107e 100755
--- a/src/org/unitConverter/unit/Unit.java
+++ b/src/org/unitConverter/unit/Unit.java
@@ -17,6 +17,7 @@
package org.unitConverter.unit;
import java.util.Objects;
+import java.util.function.DoubleUnaryOperator;
import org.unitConverter.dimension.UnitDimension;
@@ -29,6 +30,31 @@ import org.unitConverter.dimension.UnitDimension;
*/
public interface Unit {
/**
+ * Returns a unit from its base and the functions it uses to convert to and from its base.
+ *
+ * <p>
+ * For example, to get a unit representing the degree Celsius, the following code can be used:
+ *
+ * {@code Unit.fromConversionFunctions(SI.KELVIN, tempK -> tempK - 273.15, tempC -> tempC + 273.15);}
+ * </p>
+ *
+ * @param base
+ * unit's base
+ * @param converterFrom
+ * function that accepts a value expressed in the unit's base and returns that value expressed in this
+ * unit.
+ * @param converterTo
+ * function that accepts a value expressed in the unit and returns that value expressed in the unit's
+ * base.
+ * @return a unit that uses the provided functions to convert.
+ * @since 2019-05-22
+ */
+ public static Unit fromConversionFunctions(final BaseUnit base, final DoubleUnaryOperator converterFrom,
+ final DoubleUnaryOperator converterTo) {
+ return FunctionalUnit.valueOf(base, converterFrom, converterTo);
+ }
+
+ /**
* Checks if a value expressed in this unit can be converted to a value expressed in {@code other}
*
* @param other
@@ -60,6 +86,26 @@ public interface Unit {
double convertFromBase(double value);
/**
+ * Converts a value expressed in this unit to a value expressed in {@code other}.
+ *
+ * @param other
+ * unit to convert to
+ * @param value
+ * value to convert
+ * @return converted value
+ * @since 2019-05-22
+ * @throws IllegalArgumentException
+ * if {@code other} is incompatible for conversion with this unit (as tested by
+ * {@link Unit#canConvertTo}).
+ */
+ default double convertTo(final Unit other, final double value) {
+ if (this.canConvertTo(other))
+ return other.convertFromBase(this.convertToBase(value));
+ else
+ throw new IllegalArgumentException(String.format("Cannot convert from %s to %s.", this, other));
+ }
+
+ /**
* Converts from a value expressed in this unit to a value expressed in this unit's base unit.
* <p>
* This must be the inverse of {@code convertFromBase}, so {@code convertToBase(convertFromBase(value))} must be