diff --git a/src/main/java/htsjdk/samtools/util/CodeUtil.java b/src/main/java/htsjdk/samtools/util/CodeUtil.java index 29e9348a85..efe38249e3 100644 --- a/src/main/java/htsjdk/samtools/util/CodeUtil.java +++ b/src/main/java/htsjdk/samtools/util/CodeUtil.java @@ -1,14 +1,66 @@ +/* + * The MIT License + * + * Copyright (c) 2019 The Broad Institute + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + package htsjdk.samtools.util; +import java.util.function.Consumer; + /** * Miscellaneous util methods that don't fit anywhere else. */ public class CodeUtil { - /** Mimic of Oracle's nvl() - returns the first value if not null, otherwise the second value. */ + /** + * Mimic of Oracle's nvl() - returns the first value if not null, otherwise the second value. + */ public static T getOrElse(final T value1, final T value2) { - if (value1 != null) return value1; - else return value2; + if (value1 != null) { + return value1; + } else { + return value2; + } } + /** + * Applied the Consumer on the input if the input is not null, + * serves as a way to avoid writing: + * + * if(input != null){ + * action.apply(input); + * } + * + * and replacing it with + * + * applyIfNotNull(input,action); + * + * @param input a nullable object + * @param action a Consumer that will be applied to the input if it isn't null. + * @param the type of the input. + */ + public static void applyIfNotNull(final T input, Consumer action) { + if (input != null) { + action.accept(input); + } + } } diff --git a/src/main/java/htsjdk/samtools/util/IOUtil.java b/src/main/java/htsjdk/samtools/util/IOUtil.java index d32b7f9d40..f4d0958fce 100755 --- a/src/main/java/htsjdk/samtools/util/IOUtil.java +++ b/src/main/java/htsjdk/samtools/util/IOUtil.java @@ -30,7 +30,6 @@ import htsjdk.samtools.seekablestream.SeekableHTTPStream; import htsjdk.samtools.seekablestream.SeekableStream; import htsjdk.samtools.util.nio.DeleteOnExitPathHook; -import htsjdk.tribble.Tribble; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; @@ -68,7 +67,6 @@ import java.util.Collection; import java.util.Collections; import java.util.HashMap; -import java.util.HashSet; import java.util.LinkedList; import java.util.List; import java.util.Scanner; diff --git a/src/test/java/htsjdk/samtools/util/CodeUtilTest.java b/src/test/java/htsjdk/samtools/util/CodeUtilTest.java index c4978c1961..b968350bd3 100644 --- a/src/test/java/htsjdk/samtools/util/CodeUtilTest.java +++ b/src/test/java/htsjdk/samtools/util/CodeUtilTest.java @@ -4,6 +4,8 @@ import org.testng.Assert; import org.testng.annotations.Test; +import java.util.List; + public class CodeUtilTest extends HtsjdkTest { @Test @@ -13,4 +15,19 @@ public void getOrElseTest() { Assert.assertEquals(CodeUtil.getOrElse(null, notNull), notNull); Assert.assertEquals((Object) CodeUtil.getOrElse(null, null), (Object) null); } + + @Test + public void applyIfNotNullNegativeTest() { + CodeUtil.applyIfNotNull((Object) null, (o) -> Assert.fail("this shouldn't have been called")); + CodeUtil.applyIfNotNull((Integer) null, (o) -> Assert.fail("this shouldn't have been called")); + CodeUtil.applyIfNotNull((Double) null, (o) -> Assert.fail("this shouldn't have been called")); + CodeUtil.applyIfNotNull((String) null, (o) -> Assert.fail("this shouldn't have been called")); + CodeUtil.applyIfNotNull((List) null, (o) -> Assert.fail("this shouldn't have been called")); + } + + @Test(expectedExceptions = AssertionError.class) + public void applyIfNotNullPositiveTest() { + CodeUtil.applyIfNotNull(1, (o) -> Assert.assertNotEquals(o, 1, + "Throwing this proves that the method was called with value 1")); + } }