From 20cd703f92d46b2fd223f757ecf8054257ac28f7 Mon Sep 17 00:00:00 2001 From: Yossi Farjoun Date: Thu, 19 Sep 2019 22:44:19 -0400 Subject: [PATCH 1/3] - add a method to conditionally apply a Consumer on a nullable input --- .../java/htsjdk/samtools/util/CodeUtil.java | 18 +++++++++++++++--- src/main/java/htsjdk/samtools/util/IOUtil.java | 2 -- .../java/htsjdk/samtools/util/IOUtilTest.java | 4 ++++ 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/main/java/htsjdk/samtools/util/CodeUtil.java b/src/main/java/htsjdk/samtools/util/CodeUtil.java index 29e9348a85..214965111f 100644 --- a/src/main/java/htsjdk/samtools/util/CodeUtil.java +++ b/src/main/java/htsjdk/samtools/util/CodeUtil.java @@ -1,14 +1,26 @@ 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; + } } + 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/IOUtilTest.java b/src/test/java/htsjdk/samtools/util/IOUtilTest.java index 8bdfd0cca9..458093e9c2 100644 --- a/src/test/java/htsjdk/samtools/util/IOUtilTest.java +++ b/src/test/java/htsjdk/samtools/util/IOUtilTest.java @@ -114,6 +114,10 @@ public void testFileReadingAndWriting() throws IOException { String randomizedTestString = TEST_STRING + System.currentTimeMillis(); for (String ext : TEST_FILE_EXTENSIONS) { File f = File.createTempFile(TEST_FILE_PREFIX, ext); + IOUtil.assertFileIsReadable(f); + + CodeUtil.applyIfNotNull(f,IOUtil::assertFileIsReadable); + f.deleteOnExit(); OutputStream os = IOUtil.openFileForWriting(f); From 7e12947a8c35bfc41af791efd5eeb4350672e578 Mon Sep 17 00:00:00 2001 From: Yossi Farjoun Date: Mon, 23 Sep 2019 09:58:28 -0400 Subject: [PATCH 2/3] - added javadoc, license, test --- .../java/htsjdk/samtools/util/CodeUtil.java | 40 +++++++++++++++++++ .../htsjdk/samtools/util/CodeUtilTest.java | 17 ++++++++ 2 files changed, 57 insertions(+) diff --git a/src/main/java/htsjdk/samtools/util/CodeUtil.java b/src/main/java/htsjdk/samtools/util/CodeUtil.java index 214965111f..efe38249e3 100644 --- a/src/main/java/htsjdk/samtools/util/CodeUtil.java +++ b/src/main/java/htsjdk/samtools/util/CodeUtil.java @@ -1,3 +1,27 @@ +/* + * 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; @@ -18,6 +42,22 @@ public static T getOrElse(final T value1, final T 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/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")); + } } From d127245cefa5204768754ddced2e336e91cf0135 Mon Sep 17 00:00:00 2001 From: Yossi Farjoun Date: Mon, 23 Sep 2019 10:01:15 -0400 Subject: [PATCH 3/3] reverting inappropriate test --- src/test/java/htsjdk/samtools/util/IOUtilTest.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/test/java/htsjdk/samtools/util/IOUtilTest.java b/src/test/java/htsjdk/samtools/util/IOUtilTest.java index 458093e9c2..8bdfd0cca9 100644 --- a/src/test/java/htsjdk/samtools/util/IOUtilTest.java +++ b/src/test/java/htsjdk/samtools/util/IOUtilTest.java @@ -114,10 +114,6 @@ public void testFileReadingAndWriting() throws IOException { String randomizedTestString = TEST_STRING + System.currentTimeMillis(); for (String ext : TEST_FILE_EXTENSIONS) { File f = File.createTempFile(TEST_FILE_PREFIX, ext); - IOUtil.assertFileIsReadable(f); - - CodeUtil.applyIfNotNull(f,IOUtil::assertFileIsReadable); - f.deleteOnExit(); OutputStream os = IOUtil.openFileForWriting(f);