From e064e78ce14fb83ed28bfd79803370b5e1494932 Mon Sep 17 00:00:00 2001 From: Tara Drwenski Date: Wed, 26 Apr 2023 11:06:12 -0600 Subject: [PATCH] Change MFiles.create to be Nonnull --- .../java/thredds/inventory/MFileProvider.java | 5 +---- .../src/main/java/thredds/inventory/MFiles.java | 16 ++++++++-------- .../java/ucar/nc2/internal/ncml/AggDataset.java | 3 ++- .../ucar/nc2/iosp/AbstractIOServiceProvider.java | 2 +- .../main/java/thredds/inventory/s3/MFileS3.java | 1 - .../java/thredds/inventory/zarr/MFileZip.java | 1 - 6 files changed, 12 insertions(+), 16 deletions(-) diff --git a/cdm/core/src/main/java/thredds/inventory/MFileProvider.java b/cdm/core/src/main/java/thredds/inventory/MFileProvider.java index bb6dd417e2..6c214853dc 100644 --- a/cdm/core/src/main/java/thredds/inventory/MFileProvider.java +++ b/cdm/core/src/main/java/thredds/inventory/MFileProvider.java @@ -6,7 +6,6 @@ package thredds.inventory; import java.io.IOException; -import javax.annotation.Nullable; /** * A Service Provider of {@link thredds.inventory.MFile}. @@ -22,12 +21,10 @@ default boolean canProvide(String location) { } /** - * Create an {@link thredds.inventory.MFile} from a given location if it exists and is readable, otherwise return - * null. + * Create an {@link thredds.inventory.MFile} from a given location, the file may or may not exist * * @param location location of a file or . * @return {@link thredds.inventory.MFile} */ - @Nullable MFile create(String location) throws IOException; } diff --git a/cdm/core/src/main/java/thredds/inventory/MFiles.java b/cdm/core/src/main/java/thredds/inventory/MFiles.java index b07d5a99c1..25ed49d2c3 100644 --- a/cdm/core/src/main/java/thredds/inventory/MFiles.java +++ b/cdm/core/src/main/java/thredds/inventory/MFiles.java @@ -7,7 +7,7 @@ import java.io.IOException; import java.util.ServiceLoader; -import javax.annotation.Nullable; +import javax.annotation.Nonnull; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import thredds.filesystem.MFileOS; @@ -22,14 +22,13 @@ public class MFiles { private static final Logger logger = LoggerFactory.getLogger(NcmlReader.class); /** - * Create an {@link thredds.inventory.MFile} from a given location if it exists and is readable, otherwise return - * null. + * Create an {@link thredds.inventory.MFile} from a given location, the file may or may not exist * * @param location location of file (local or remote) to be used to back the MFile object * @return {@link thredds.inventory.MFile} */ - @Nullable - public static MFile create(String location) { + @Nonnull + public static MFile create(@Nonnull String location) { MFileProvider mFileProvider = null; // look for dynamically loaded MFileProviders @@ -40,12 +39,13 @@ public static MFile create(String location) { } } - MFile mfile = null; try { - mfile = mFileProvider != null ? mFileProvider.create(location) : MFileOS.getExistingFile(location); + if (mFileProvider != null) { + return mFileProvider.create(location); + } } catch (IOException ioe) { logger.error("Error creating MFile at {}.", location, ioe); } - return mfile; + return new MFileOS(location); } } diff --git a/cdm/core/src/main/java/ucar/nc2/internal/ncml/AggDataset.java b/cdm/core/src/main/java/ucar/nc2/internal/ncml/AggDataset.java index 6ef7bb8bf3..cf540fbbc4 100644 --- a/cdm/core/src/main/java/ucar/nc2/internal/ncml/AggDataset.java +++ b/cdm/core/src/main/java/ucar/nc2/internal/ncml/AggDataset.java @@ -30,6 +30,7 @@ class AggDataset implements Comparable { private static final boolean debugOpenFile = false; private static final boolean debugRead = false; + @Nullable private final MFile mfile; private final Set enhance; // used by Fmrc to read enhanced datasets @Nullable @@ -71,7 +72,7 @@ protected AggDataset(MFile mfile, @Nullable Object spiObject, @Nullable Element protected AggDataset(String cacheLocation, String location, @Nullable String id, @Nullable EnumSet wantEnhance, @Nullable ucar.nc2.util.cache.FileFactory reader, @Nullable Object spiObject, @Nullable Element ncmlElem) { - this.mfile = MFiles.create(location); // may be null + this.mfile = location == null ? null : MFiles.create(location); this.cacheLocation = cacheLocation; this.id = id; this.enhance = (wantEnhance == null) ? NetcdfDataset.getEnhanceNone() : wantEnhance; diff --git a/cdm/core/src/main/java/ucar/nc2/iosp/AbstractIOServiceProvider.java b/cdm/core/src/main/java/ucar/nc2/iosp/AbstractIOServiceProvider.java index 84fbc9f44c..87cb02d2b7 100644 --- a/cdm/core/src/main/java/ucar/nc2/iosp/AbstractIOServiceProvider.java +++ b/cdm/core/src/main/java/ucar/nc2/iosp/AbstractIOServiceProvider.java @@ -165,7 +165,7 @@ public boolean syncExtend() throws IOException { public long getLastModified() { if (location != null) { MFile file = MFiles.create(location); - return file != null ? file.getLastModified() : 0; + return file.getLastModified(); } else { return 0; } diff --git a/cdm/s3/src/main/java/thredds/inventory/s3/MFileS3.java b/cdm/s3/src/main/java/thredds/inventory/s3/MFileS3.java index 8cb80d70f1..374f9a6563 100644 --- a/cdm/s3/src/main/java/thredds/inventory/s3/MFileS3.java +++ b/cdm/s3/src/main/java/thredds/inventory/s3/MFileS3.java @@ -332,7 +332,6 @@ public String getProtocol() { return protocol; } - @Nullable @Override public MFile create(String location) throws IOException { try { diff --git a/cdm/zarr/src/main/java/thredds/inventory/zarr/MFileZip.java b/cdm/zarr/src/main/java/thredds/inventory/zarr/MFileZip.java index 7a5835d41e..3062f52217 100644 --- a/cdm/zarr/src/main/java/thredds/inventory/zarr/MFileZip.java +++ b/cdm/zarr/src/main/java/thredds/inventory/zarr/MFileZip.java @@ -200,7 +200,6 @@ public boolean canProvide(String location) { return location != null && location.contains(ext); } - @Nullable @Override public MFile create(String location) throws IOException { return new MFileZip(location);