Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a method to conditionally apply a Consumer on a nullable input #1419

Merged
merged 3 commits into from
Sep 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 55 additions & 3 deletions src/main/java/htsjdk/samtools/util/CodeUtil.java
Original file line number Diff line number Diff line change
@@ -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> 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:
* <code>
* if(input != null){
* action.apply(input);
* }
* </code>
* and replacing it with
* <code>
* applyIfNotNull(input,action);
* </code>
* @param input a nullable object
* @param action a Consumer that will be applied to the input if it isn't null.
* @param <T> the type of the input.
*/
public static <T> void applyIfNotNull(final T input, Consumer<T> action) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

javadoc

if (input != null) {
action.accept(input);
}
}
}
2 changes: 0 additions & 2 deletions src/main/java/htsjdk/samtools/util/IOUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
17 changes: 17 additions & 0 deletions src/test/java/htsjdk/samtools/util/CodeUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import org.testng.Assert;
import org.testng.annotations.Test;

import java.util.List;

public class CodeUtilTest extends HtsjdkTest {

@Test
Expand All @@ -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<Object>) 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"));
}
}