You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@causeway.apache.org by ah...@apache.org on 2023/02/26 06:34:50 UTC
[causeway] branch master updated: CAUSEWAY-3304: adds DataSource methods for convenience
This is an automated email from the ASF dual-hosted git repository.
ahuber pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/causeway.git
The following commit(s) were added to refs/heads/master by this push:
new 315c50859f CAUSEWAY-3304: adds DataSource methods for convenience
315c50859f is described below
commit 315c50859f52543d0220b5075497545573d3cc1e
Author: Andi Huber <ah...@apache.org>
AuthorDate: Sun Feb 26 07:34:46 2023 +0100
CAUSEWAY-3304: adds DataSource methods for convenience
---
.../org/apache/causeway/commons/io/DataSource.java | 24 +++++++++++++++++++
.../org/apache/causeway/commons/io/FileUtils.java | 28 ++++++++++++----------
2 files changed, 39 insertions(+), 13 deletions(-)
diff --git a/commons/src/main/java/org/apache/causeway/commons/io/DataSource.java b/commons/src/main/java/org/apache/causeway/commons/io/DataSource.java
index 40c7c3f5bd..8387b1e6b4 100644
--- a/commons/src/main/java/org/apache/causeway/commons/io/DataSource.java
+++ b/commons/src/main/java/org/apache/causeway/commons/io/DataSource.java
@@ -24,11 +24,14 @@ import java.io.FileInputStream;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
+import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import org.springframework.lang.Nullable;
+import org.apache.causeway.commons.functional.ThrowingConsumer;
+import org.apache.causeway.commons.functional.ThrowingFunction;
import org.apache.causeway.commons.functional.Try;
import org.apache.causeway.commons.internal.base._Strings;
@@ -44,6 +47,27 @@ public interface DataSource {
<T> Try<T> readAll(@NonNull Function<InputStream, Try<T>> consumingMapper);
+ /**
+ * Passes an {@link InputStream} to given {@link Function} for application.
+ * @return either a successful or failed {@link Try} (non-null),
+ * where in the success case, the returned Try is holding the returned value from the given {@link Function inputStreamMapper};
+ * if the InputStream is absent or not readable, the returned Try will hold the underlying {@link Exception}
+ */
+ default <T> Try<T> tryReadAndApply(final @NonNull ThrowingFunction<InputStream, T> inputStreamMapper) {
+ return readAll(inputStream->
+ Try.call(()->inputStreamMapper.apply(inputStream)));
+ }
+
+ /**
+ * Passes an {@link InputStream} to given {@link Consumer} for consumption.
+ * @return either a successful or failed {@link Try} (non-null);
+ * if the InputStream is absent or not readable, the returned Try will hold the underlying {@link Exception}
+ */
+ default Try<Void> tryReadAndAccept(final @NonNull ThrowingConsumer<InputStream> inputStreamConsumer) {
+ return readAll(inputStream->
+ Try.run(()->inputStreamConsumer.accept(inputStream)));
+ }
+
// -- FACTORIES
/**
diff --git a/commons/src/main/java/org/apache/causeway/commons/io/FileUtils.java b/commons/src/main/java/org/apache/causeway/commons/io/FileUtils.java
index 9c4cc02910..89f345661c 100644
--- a/commons/src/main/java/org/apache/causeway/commons/io/FileUtils.java
+++ b/commons/src/main/java/org/apache/causeway/commons/io/FileUtils.java
@@ -57,31 +57,33 @@ public class FileUtils {
/**
* Opens an {@link InputStream} for give {@link File}
- * and passes it to given {@link Consumer} for consumption,
+ * and passes it to given {@link Function} for application,
* then finally closes it.
- * @return either a successful or failed {@link Try} (non-null);
- * if the file is null or not readable, the failure may hold a {@link NoSuchFileException} or other i/o related exceptions
+ * @return either a successful or failed {@link Try} (non-null),
+ * where in the success case, the Try is holding the returned value from the given {@link Function inputStreamMapper};
+ * if the file is null or not readable, the failure may hold a {@link NoSuchFileException} or other i/o related exceptions
+ * @see DataSource#ofFile(File)
*/
- public Try<Void> tryReadAndAccept(final @Nullable File file, final @NonNull ThrowingConsumer<InputStream> inputStreamConsumer) {
- return Try.run(()->{
+ public <T> Try<T> tryReadAndApply(final @Nullable File file, final @NonNull ThrowingFunction<InputStream, T> inputStreamMapper) {
+ return Try.call(()->{
try(val inputStream = new FileInputStream(existingFileElseFail(file))){
- inputStreamConsumer.accept(inputStream);
+ return inputStreamMapper.apply(inputStream);
}
});
}
/**
* Opens an {@link InputStream} for give {@link File}
- * and passes it to given {@link Function} for applying,
+ * and passes it to given {@link Consumer} for consumption,
* then finally closes it.
- * @return either a successful or failed {@link Try} (non-null),
- * where in the success case, the Try is holding the returned value from the given {@link Function inputStreamMapper};
- * if the file is null or not readable, the failure may hold a {@link NoSuchFileException} or other i/o related exceptions
+ * @return either a successful or failed {@link Try} (non-null);
+ * if the file is null or not readable, the failure may hold a {@link NoSuchFileException} or other i/o related exceptions
+ * @see DataSource#ofFile(File)
*/
- public <T> Try<T> tryReadAndApply(final @Nullable File file, final @NonNull ThrowingFunction<InputStream, T> inputStreamMapper) {
- return Try.call(()->{
+ public Try<Void> tryReadAndAccept(final @Nullable File file, final @NonNull ThrowingConsumer<InputStream> inputStreamConsumer) {
+ return Try.run(()->{
try(val inputStream = new FileInputStream(existingFileElseFail(file))){
- return inputStreamMapper.apply(inputStream);
+ inputStreamConsumer.accept(inputStream);
}
});
}