From 81cd1626f73018506e949bfeb5668f6b947c30d1 Mon Sep 17 00:00:00 2001 From: Tom White Date: Tue, 3 Sep 2019 17:36:24 +0100 Subject: [PATCH] Serializable identityFunction --- .../hellbender/engine/FeatureDataSource.java | 6 +++--- .../org/broadinstitute/hellbender/utils/Utils.java | 12 ++++++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/broadinstitute/hellbender/engine/FeatureDataSource.java b/src/main/java/org/broadinstitute/hellbender/engine/FeatureDataSource.java index 9710bcdf810..460dc1a2241 100644 --- a/src/main/java/org/broadinstitute/hellbender/engine/FeatureDataSource.java +++ b/src/main/java/org/broadinstitute/hellbender/engine/FeatureDataSource.java @@ -269,8 +269,8 @@ public FeatureDataSource(final FeatureInput featureInput, final int queryLook Utils.nonNull(genomicsDBOptions, "GenomicsDBOptions must not be null. Calling tool may not read from a GenomicsDB data source."); } - final Function cloudWrapper = (cloudPrefetchBuffer > 0 ? is -> SeekableByteChannelPrefetcher.addPrefetcher(cloudPrefetchBuffer, is) : Function.identity()); - final Function cloudIndexWrapper = (cloudIndexPrefetchBuffer > 0 ? is -> SeekableByteChannelPrefetcher.addPrefetcher(cloudIndexPrefetchBuffer, is) : Function.identity()); + final Function cloudWrapper = (cloudPrefetchBuffer > 0 ? is -> SeekableByteChannelPrefetcher.addPrefetcher(cloudPrefetchBuffer, is) : Utils.identityFunction()); + final Function cloudIndexWrapper = (cloudIndexPrefetchBuffer > 0 ? is -> SeekableByteChannelPrefetcher.addPrefetcher(cloudIndexPrefetchBuffer, is) : Utils.identityFunction()); // Create a feature reader without requiring an index. We will require one ourselves as soon as // a query by interval is attempted. @@ -369,7 +369,7 @@ private static FeatureReader getFeatureReader(final Featu if (BucketUtils.isCloudStorageUrl(featureInput)) { return AbstractFeatureReader.getFeatureReader(absoluteRawPath, null, codec, requireIndex, cloudWrapper, cloudIndexWrapper); } else { - return AbstractFeatureReader.getFeatureReader(absoluteRawPath, null, codec, requireIndex, Function.identity(), Function.identity()); + return AbstractFeatureReader.getFeatureReader(absoluteRawPath, null, codec, requireIndex, Utils.identityFunction(), Utils.identityFunction()); } } catch (final TribbleException e) { throw new GATKException("Error initializing feature reader for path " + featureInput.getFeaturePath(), e); diff --git a/src/main/java/org/broadinstitute/hellbender/utils/Utils.java b/src/main/java/org/broadinstitute/hellbender/utils/Utils.java index b51658114b7..af4f808088d 100644 --- a/src/main/java/org/broadinstitute/hellbender/utils/Utils.java +++ b/src/main/java/org/broadinstitute/hellbender/utils/Utils.java @@ -19,6 +19,7 @@ import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; +import java.io.Serializable; import java.lang.reflect.Array; import java.math.BigInteger; import java.nio.file.Files; @@ -1102,6 +1103,17 @@ public static Stream stream(final Iterator iterator) { return stream(() -> iterator); } + /** + * Returns a function that always returns its input argument. Unlike {@link Function#identity()} the returned + * function is also serializable. + * + * @param the type of the input and output objects to the function + * @return a function that always returns its input argument + */ + public static Function identityFunction() { + return (Function & Serializable) t -> t; + } + /** * Like Guava's {@link Iterators#transform(Iterator, com.google.common.base.Function)}, but runs a fixed number * ({@code numThreads}) of transformations in parallel, while maintaining ordering of the output iterator.