Dave Jarvis' Repositories

git clone https://repo.autonoma.ca/repo/keenwrite.git

Makes null-safe calls to paths

AuthorDaveJarvis <email>
Date2023-08-02 14:44:04 GMT-0700
Commit154efef2bc710a1c2b4117ffed477659bc572a0e
Parenta503449
Delta25 lines added, 15 lines removed, 10-line increase
src/test/java/com/keenwrite/io/UserDataDirTest.java
import org.junit.jupiter.api.Test;
-import java.io.FileNotFoundException;
-
-import static org.junit.jupiter.api.Assertions.*;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
class UserDataDirTest {
@Test
- void test_Unix_GetAppDirectory_DirectoryExists()
- throws FileNotFoundException {
+ void test_Unix_GetAppDirectory_DirectoryExists() {
final var path = UserDataDir.getAppPath( "test" );
final var file = path.toFile();
src/main/java/com/keenwrite/ui/explorer/FilesView.java
import static com.keenwrite.constants.Constants.UI_CONTROL_SPACING;
import static com.keenwrite.events.StatusEvent.clue;
+import static com.keenwrite.io.SysFile.toFile;
import static com.keenwrite.ui.fonts.IconFactory.createFileIcon;
import static java.nio.file.Files.size;
final var filename = entry.nameProperty().get();
final var path = Path.of( dir.toString(), filename );
- final var file = path.toFile();
+ final var file = toFile( path );
if( file.isFile() ) {
FileOpenEvent.fire( path.toUri() );
}
else if( file.isDirectory() ) {
- mDirectory.set( path.normalize().toFile() );
+ mDirectory.set( toFile( path.normalize() ) );
}
}
SysFile.getFileName( path ),
size( path ),
- ofEpochMilli( path.toFile().lastModified() )
+ ofEpochMilli( toFile( path ).lastModified() )
);
}
src/main/java/com/keenwrite/typesetting/installer/panes/TypesetterThemesDownloadPane.java
import static com.keenwrite.Messages.get;
import static com.keenwrite.events.StatusEvent.clue;
+import static com.keenwrite.io.SysFile.toFile;
import static com.keenwrite.preferences.AppKeys.KEY_TYPESET_CONTEXT_THEMES_PATH;
// Replace the default themes directory with the downloaded version.
- final var root = Zip.root( target.toPath() ).toFile();
+ final var root = toFile( Zip.root( target.toPath() ) );
// Make sure the typesetter will know where to find the themes.
src/main/java/com/keenwrite/io/SysFile.java
* deleted or moved.
*
- * @param path The path to verify existence.
+ * @param path The path to verify existence, may be null.
* @return The given path, if it exists, otherwise the user's home directory.
*/
public static Path normalize( final Path path ) {
- assert path != null;
+ return path == null
+ ? USER_DIRECTORY.toPath()
+ : path.toFile().exists()
+ ? path
+ : USER_DIRECTORY.toPath();
+ }
- return path.toFile().exists() ? path : USER_DIRECTORY.toPath();
+ public static File toFile( final Path path ) {
+ return path == null
+ ? USER_DIRECTORY
+ : path.toFile();
}
src/main/java/com/keenwrite/io/UserDataDir.java
import java.nio.file.Path;
+import static com.keenwrite.io.SysFile.toFile;
import static java.lang.System.getProperty;
import static java.lang.System.getenv;
*/
private static boolean ensureExists( final Path path ) {
- final var file = path.toFile();
+ final var file = toFile( path );
return file.exists() || file.mkdirs();
}
src/main/java/com/keenwrite/io/Zip.java
import java.util.zip.ZipFile;
+import static com.keenwrite.io.SysFile.toFile;
import static java.nio.file.Files.createDirectories;
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
final BiConsumer<ZipFile, ZipEntry> consumer )
throws IOException {
- assert zipPath.toFile().isFile();
+ assert toFile( zipPath ).isFile();
- try( final var zipFile = new ZipFile( zipPath.toFile() ) ) {
+ try( final var zipFile = new ZipFile( toFile( zipPath ) ) ) {
final var entries = zipFile.entries();