Skip to content

Commit

Permalink
Serializable identityFunction
Browse files Browse the repository at this point in the history
  • Loading branch information
tomwhite committed Sep 9, 2019
1 parent 470c186 commit 81cd162
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,8 @@ public FeatureDataSource(final FeatureInput<T> featureInput, final int queryLook
Utils.nonNull(genomicsDBOptions, "GenomicsDBOptions must not be null. Calling tool may not read from a GenomicsDB data source.");
}

final Function<SeekableByteChannel, SeekableByteChannel> cloudWrapper = (cloudPrefetchBuffer > 0 ? is -> SeekableByteChannelPrefetcher.addPrefetcher(cloudPrefetchBuffer, is) : Function.identity());
final Function<SeekableByteChannel, SeekableByteChannel> cloudIndexWrapper = (cloudIndexPrefetchBuffer > 0 ? is -> SeekableByteChannelPrefetcher.addPrefetcher(cloudIndexPrefetchBuffer, is) : Function.identity());
final Function<SeekableByteChannel, SeekableByteChannel> cloudWrapper = (cloudPrefetchBuffer > 0 ? is -> SeekableByteChannelPrefetcher.addPrefetcher(cloudPrefetchBuffer, is) : Utils.identityFunction());
final Function<SeekableByteChannel, SeekableByteChannel> 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.
Expand Down Expand Up @@ -369,7 +369,7 @@ private static <T extends Feature> FeatureReader<T> 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);
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/org/broadinstitute/hellbender/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1102,6 +1103,17 @@ public static <T> Stream<T> stream(final Iterator<T> iterator) {
return stream(() -> iterator);
}

/**
* Returns a function that always returns its input argument. Unlike {@link Function#identity()} the returned
* function is also serializable.
*
* @param <T> the type of the input and output objects to the function
* @return a function that always returns its input argument
*/
public static <T> Function<T, T> 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.
Expand Down

0 comments on commit 81cd162

Please sign in to comment.