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 1 commit
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
18 changes: 15 additions & 3 deletions src/main/java/htsjdk/samtools/util/CodeUtil.java
Original file line number Diff line number Diff line change
@@ -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> 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 <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
4 changes: 4 additions & 0 deletions src/test/java/htsjdk/samtools/util/IOUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Member

Choose a reason for hiding this comment

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

This is a strange test for this method... Why not give it its own test case in a new CodeUtilUnitTest class?


f.deleteOnExit();

OutputStream os = IOUtil.openFileForWriting(f);
Expand Down