Skip to content

Commit

Permalink
Refactor FileHashStoreUtility method 'isDirectoryEmpty' to 'dirContai…
Browse files Browse the repository at this point in the history
…nsFiles' to improve method readability
  • Loading branch information
doulikecookiedough committed Jan 31, 2024
1 parent 23ec383 commit 32db8be
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ protected void verifyHashStoreProperties(
);

if (Files.isDirectory(storePath)) {
if (!FileHashStoreUtility.isDirectoryEmpty(storePath)) {
if (FileHashStoreUtility.dirContainsFiles(storePath)) {
String errMsg = "FileHashStore - Missing 'hashstore.yaml' but directories"
+ " and/or objects found.";
logFileHashStore.fatal(errMsg);
Expand Down Expand Up @@ -1212,8 +1212,8 @@ public void deleteObject(String pid) throws IllegalArgumentException, IOExceptio
Path expectedPidMetadataDirectory = METADATA_STORE_DIRECTORY.resolve(pidRelativePath);

// Check that directory exists and is not empty before attempting to delete metadata docs
if (Files.isDirectory(expectedPidMetadataDirectory) && !FileHashStoreUtility
.isDirectoryEmpty(expectedPidMetadataDirectory)) {
if (Files.isDirectory(expectedPidMetadataDirectory) && FileHashStoreUtility.dirContainsFiles(
expectedPidMetadataDirectory)) {
try (Stream<Path> stream = Files.walk(expectedPidMetadataDirectory)) {
stream.map(Path::toFile).forEach(File::delete);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public static String getPidHexDigest(String pid, String algorithm)
* @return True if a file is found or the directory is empty, False otherwise
* @throws IOException If I/O occurs when accessing directory
*/
public static boolean isDirectoryEmpty(Path directory) throws IOException {
public static boolean dirContainsFiles(Path directory) throws IOException {
try (Stream<Path> stream = Files.list(directory)) {
// The findFirst() method is called on the stream created from the given
// directory to retrieve the first element. If the stream is empty (i.e., the
Expand All @@ -112,7 +112,7 @@ public static boolean isDirectoryEmpty(Path directory) throws IOException {
// findFirst(). If the Optional contains a value (i.e., an element was found),
// isPresent() returns true. If the Optional is empty (i.e., the stream is
// empty), isPresent() returns false.
return !stream.findFirst().isPresent();
return stream.findFirst().isPresent();
}
}

Expand Down

0 comments on commit 32db8be

Please sign in to comment.