summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdrien Hopkins <adrien.p.hopkins@gmail.com>2019-05-22 18:47:05 -0400
committerAdrien Hopkins <adrien.p.hopkins@gmail.com>2019-05-22 18:47:05 -0400
commit01b072b98fdd19a2d57afc15a4ee4a80d0bfc0cd (patch)
tree0b835f4196e0e34a9530e014352d89a41096ef97
parent987fd8406d65505aedecd17e51216eb0ce393fbb (diff)
Added null checks to Unit's methods, including the new methods.
-rw-r--r--src/org/unitConverter/unit/FunctionalUnit.java9
-rwxr-xr-xsrc/org/unitConverter/unit/Unit.java8
2 files changed, 15 insertions, 2 deletions
diff --git a/src/org/unitConverter/unit/FunctionalUnit.java b/src/org/unitConverter/unit/FunctionalUnit.java
index c2aae6d..e3db43a 100644
--- a/src/org/unitConverter/unit/FunctionalUnit.java
+++ b/src/org/unitConverter/unit/FunctionalUnit.java
@@ -16,6 +16,7 @@
*/
package org.unitConverter.unit;
+import java.util.Objects;
import java.util.function.DoubleUnaryOperator;
/**
@@ -38,6 +39,8 @@ final class FunctionalUnit extends AbstractUnit {
* base.
* @return a unit that uses the provided functions to convert.
* @since 2019-05-22
+ * @throws NullPointerException
+ * if any argument is null
*/
public static FunctionalUnit valueOf(final BaseUnit base, final DoubleUnaryOperator converterFrom,
final DoubleUnaryOperator converterTo) {
@@ -69,13 +72,15 @@ final class FunctionalUnit extends AbstractUnit {
* @param converterTo
* function that accepts a value expressed in the unit and returns that value expressed in the unit's
* base.
+ * @throws NullPointerException
+ * if any argument is null
* @since 2019-05-22
*/
private FunctionalUnit(final BaseUnit base, final DoubleUnaryOperator converterFrom,
final DoubleUnaryOperator converterTo) {
super(base);
- this.converterFrom = converterFrom;
- this.converterTo = converterTo;
+ this.converterFrom = Objects.requireNonNull(converterFrom, "converterFrom must not be null.");
+ this.converterTo = Objects.requireNonNull(converterTo, "converterTo must not be null.");
}
@Override
diff --git a/src/org/unitConverter/unit/Unit.java b/src/org/unitConverter/unit/Unit.java
index 2ac107e..54f0ab5 100755
--- a/src/org/unitConverter/unit/Unit.java
+++ b/src/org/unitConverter/unit/Unit.java
@@ -48,6 +48,8 @@ public interface Unit {
* base.
* @return a unit that uses the provided functions to convert.
* @since 2019-05-22
+ * @throws NullPointerException
+ * if any argument is null
*/
public static Unit fromConversionFunctions(final BaseUnit base, final DoubleUnaryOperator converterFrom,
final DoubleUnaryOperator converterTo) {
@@ -62,8 +64,11 @@ public interface Unit {
* @return true if the units are compatible
* @since 2019-01-13
* @since v0.1.0
+ * @throws NullPointerException
+ * if other is null
*/
default boolean canConvertTo(final Unit other) {
+ Objects.requireNonNull(other, "other must not be null.");
return Objects.equals(this.getBase(), other.getBase());
}
@@ -97,8 +102,11 @@ public interface Unit {
* @throws IllegalArgumentException
* if {@code other} is incompatible for conversion with this unit (as tested by
* {@link Unit#canConvertTo}).
+ * @throws NullPointerException
+ * if other is null
*/
default double convertTo(final Unit other, final double value) {
+ Objects.requireNonNull(other, "other must not be null.");
if (this.canConvertTo(other))
return other.convertFromBase(this.convertToBase(value));
else