From f358dca35ce72c31455f88efe07bc525f5102e96 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Sebastian=20Ho=C3=9F?= Date: Wed, 22 Jun 2016 20:14:47 +0200 Subject: [PATCH] add some tests MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Sebastian Hoß --- .../xn__ho_hia/quality/null_analysis/Nullsafe.java | 69 ++++++++++++++++++++ .../quality/null_analysis/NullsafeMath.java | 74 ---------------------- .../quality/null_analysis/NullsafeTest.java | 37 +++++++++++ 3 files changed, 106 insertions(+), 74 deletions(-) delete mode 100644 src/main/java/de/xn__ho_hia/quality/null_analysis/NullsafeMath.java create mode 100644 src/test/java/de/xn__ho_hia/quality/null_analysis/NullsafeTest.java diff --git a/src/main/java/de/xn__ho_hia/quality/null_analysis/Nullsafe.java b/src/main/java/de/xn__ho_hia/quality/null_analysis/Nullsafe.java index c34db4a..8f33a82 100755 --- a/src/main/java/de/xn__ho_hia/quality/null_analysis/Nullsafe.java +++ b/src/main/java/de/xn__ho_hia/quality/null_analysis/Nullsafe.java @@ -6,6 +6,7 @@ */ package de.xn__ho_hia.quality.null_analysis; +import java.math.BigInteger; import java.util.Collection; import java.util.Collections; import java.util.List; @@ -152,4 +153,72 @@ public final class Nullsafe { return string == null || string.isEmpty(); } + /** + * @param value + * The value to wrap + * @return A @NonNull {@link BigInteger} + */ + @NonNull + public static BigInteger asBigInteger(final long value) { + return Nullsafe.nonNull(BigInteger.valueOf(value)); + } + + /** + * @param value + * The value to wrap + * @return A @NonNull {@link Long} + */ + @NonNull + public static Long asLong(final long value) { + return Nullsafe.nonNull(Long.valueOf(value)); + } + + /** + * @param first + * The first value to add + * @param second + * The second value to add + * @return The sum of both values as a @NonNull {@link BigInteger} + */ + @NonNull + public static BigInteger addNullsafe(@Nullable final BigInteger first, @Nullable final BigInteger second) { + return Nullsafe.nonNull(Nullsafe.nonNull(first).add(second)); + } + + /** + * @param first + * The first value to subtract + * @param second + * The second value to subtract + * @return The difference of both values as a @NonNull {@link BigInteger} + */ + @NonNull + public static BigInteger subtractNullsafe(@Nullable final BigInteger first, @Nullable final BigInteger second) { + return Nullsafe.nonNull(Nullsafe.nonNull(first).subtract(second)); + } + + /** + * @param first + * The first value to divide + * @param second + * The second value to divide + * @return The division of both values as a @NonNull {@link BigInteger} + */ + @NonNull + public static BigInteger divideNullsafe(@Nullable final BigInteger first, @Nullable final BigInteger second) { + return Nullsafe.nonNull(Nullsafe.nonNull(first).divide(second)); + } + + /** + * @param first + * The first value to multiply + * @param second + * The second value to multiply + * @return The multiplication of both values as a @NonNull {@link BigInteger} + */ + @NonNull + public static BigInteger multiplyNullsafe(@Nullable final BigInteger first, @Nullable final BigInteger second) { + return Nullsafe.nonNull(Nullsafe.nonNull(first).multiply(second)); + } + } diff --git a/src/main/java/de/xn__ho_hia/quality/null_analysis/NullsafeMath.java b/src/main/java/de/xn__ho_hia/quality/null_analysis/NullsafeMath.java deleted file mode 100644 index 44dd07d..0000000 --- a/src/main/java/de/xn__ho_hia/quality/null_analysis/NullsafeMath.java +++ /dev/null @@ -1,74 +0,0 @@ -package de.xn__ho_hia.quality.null_analysis; - -import java.math.BigInteger; - -import org.eclipse.jdt.annotation.NonNull; - -/** - * Utility class to work with @NonNull match operations. - */ -public final class NullsafeMath { - - /** - * @param value - * The value to wrap - * @return A @NonNull {@link BigInteger} - */ - public static @NonNull BigInteger asBigInteger(final long value) { - return Nullsafe.nonNull(BigInteger.valueOf(value)); - } - - /** - * @param value - * The value to wrap - * @return A @NonNull {@link Long} - */ - public static @NonNull Long asLong(final long value) { - return Nullsafe.nonNull(Long.valueOf(value)); - } - - /** - * @param first - * The first value to add - * @param second - * The second value to add - * @return The sum of both values as a @NonNull {@link BigInteger} - */ - public static @NonNull BigInteger addNullsafe(final BigInteger first, final BigInteger second) { - return Nullsafe.nonNull(first.add(second)); - } - - /** - * @param first - * The first value to subtract - * @param second - * The second value to subtract - * @return The difference of both values as a @NonNull {@link BigInteger} - */ - public static @NonNull BigInteger subtractNullsafe(final BigInteger first, final BigInteger second) { - return Nullsafe.nonNull(first.subtract(second)); - } - - /** - * @param first - * The first value to divide - * @param second - * The second value to divide - * @return The division of both values as a @NonNull {@link BigInteger} - */ - public static @NonNull BigInteger divideNullsafe(final BigInteger first, final BigInteger second) { - return Nullsafe.nonNull(first.divide(second)); - } - - /** - * @param first - * The first value to multiply - * @param second - * The second value to multiply - * @return The multiplication of both values as a @NonNull {@link BigInteger} - */ - public static @NonNull BigInteger multiplyNullsafe(final BigInteger first, final BigInteger second) { - return Nullsafe.nonNull(first.multiply(second)); - } - -} diff --git a/src/test/java/de/xn__ho_hia/quality/null_analysis/NullsafeTest.java b/src/test/java/de/xn__ho_hia/quality/null_analysis/NullsafeTest.java new file mode 100644 index 0000000..4817b18 --- /dev/null +++ b/src/test/java/de/xn__ho_hia/quality/null_analysis/NullsafeTest.java @@ -0,0 +1,37 @@ +/* + * Copyright © 2016 Sebastian Hoß + * This work is free. You can redistribute it and/or modify it under the + * terms of the Do What The Fuck You Want To Public License, Version 2, + * as published by Sam Hocevar. See http://www.wtfpl.net/ for more details. + */ +package de.xn__ho_hia.quality.null_analysis; + +import org.eclipse.jdt.annotation.NonNull; +import org.eclipse.jdt.annotation.Nullable; +import org.junit.Assert; +import org.junit.Test; + +/** + * + */ +@SuppressWarnings({ "nls", "static-method" }) +public class NullsafeTest { + + /** + * Test method for {@link de.xn__ho_hia.quality.null_analysis.Nullsafe#nonNull(java.lang.Object)}. + */ + @Test + public final void shouldConvertNullableIntoNonNull() { + // given + @Nullable + final Object test = "unit test"; + + // when + @NonNull + final Object nonNullObject = Nullsafe.nonNull(test); + + // then + Assert.assertNotNull(nonNullObject); + } + +} -- 2.11.4.GIT