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:43:32 UTC

[causeway] branch master updated: CAUSEWAY-3304: make DataSource#ofFile null-able

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 033b6f7d48 CAUSEWAY-3304: make DataSource#ofFile null-able
033b6f7d48 is described below

commit 033b6f7d483c261cc9709f65bbb511965e49200d
Author: Andi Huber <ah...@apache.org>
AuthorDate: Sun Feb 26 07:43:28 2023 +0100

    CAUSEWAY-3304: make DataSource#ofFile null-able
---
 .../java/org/apache/causeway/commons/io/DataSource.java    | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 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 8387b1e6b4..3156974b49 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
@@ -99,8 +99,18 @@ public interface DataSource {
         return ofInputStreamSupplier(()->cls.getResourceAsStream(resourcePath));
     }
 
-    static DataSource ofFile(final @NonNull File file) {
-        return ofInputStreamSupplier(()->Try.call(()->new FileInputStream(file)).ifFailureFail().getValue().orElseThrow());
+    /**
+     * Creates a {@link DataSource} for given {@link File}.
+     * If <code>null</code> an 'empty' DataSource is returned.
+     */
+    static DataSource ofFile(final @Nullable File file) {
+        if(file==null) {
+            return DataSource.none();
+        }
+        return ofInputStreamSupplier(
+                ()->Try.call(()->new FileInputStream(FileUtils.existingFileElseFail(file)))
+                    .ifFailureFail()
+                    .getValue().orElseThrow());
     }
 
     static DataSource ofString(final @Nullable String string, final Charset charset) {