Use try-with-resources for closables

This commit is contained in:
Paul Campbell 2018-06-20 22:16:22 +01:00
parent 32354fe8ea
commit b4e6cee636
2 changed files with 13 additions and 14 deletions

View file

@ -30,10 +30,7 @@ import org.eclipse.jgit.util.FS;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.DirectoryNotEmptyException; import java.nio.file.*;
import java.nio.file.Files;
import java.nio.file.NotDirectoryException;
import java.nio.file.Path;
/** /**
* Initialise a new GitDB Repo. * Initialise a new GitDB Repo.
@ -59,10 +56,11 @@ class InitGitDBRepo {
final InitGitDBRepo initRepo = new InitGitDBRepo(); final InitGitDBRepo initRepo = new InitGitDBRepo();
final File validDbDir = initRepo.validDbDir(dbDir.toFile()); final File validDbDir = initRepo.validDbDir(dbDir.toFile());
validDbDir.mkdirs(); validDbDir.mkdirs();
final Repository repository = RepositoryCache.FileKey.exact(validDbDir, FS.DETECTED).open(false); try (final Repository repository = RepositoryCache.FileKey.exact(validDbDir, FS.DETECTED).open(false)) {
repository.create(true); repository.create(true);
initRepo.createInitialBranchOnMaster(repository); initRepo.createInitialBranchOnMaster(repository);
} }
}
private void createInitialBranchOnMaster(final Repository repository) throws IOException { private void createInitialBranchOnMaster(final Repository repository) throws IOException {
final GitDBRepo repo = new GitDBRepo(repository); final GitDBRepo repo = new GitDBRepo(repository);
@ -102,10 +100,12 @@ class InitGitDBRepo {
} }
private void verifyIsEmpty(final File dbDir) throws IOException { private void verifyIsEmpty(final File dbDir) throws IOException {
if (Files.newDirectoryStream(dbDir.toPath()).iterator().hasNext()) { try (final DirectoryStream<Path> directoryStream = Files.newDirectoryStream(dbDir.toPath())) {
if (directoryStream.iterator().hasNext()) {
throw new DirectoryNotEmptyException(dbDir.toString()); throw new DirectoryNotEmptyException(dbDir.toString());
} }
} }
}
private void verifyIsNotAFile(final File dbDir) throws NotDirectoryException { private void verifyIsNotAFile(final File dbDir) throws NotDirectoryException {
if (dbDir.isFile()) { if (dbDir.isFile()) {

View file

@ -22,10 +22,7 @@
package net.kemitix.gitdb.impl; package net.kemitix.gitdb.impl;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.*;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.lib.TreeFormatter;
import java.io.IOException; import java.io.IOException;
import java.util.Optional; import java.util.Optional;
@ -108,6 +105,8 @@ class KeyRemover {
* @throws IOException the object could not be stored. * @throws IOException the object could not be stored.
*/ */
private ObjectId insertTree(final TreeFormatter treeFormatter) throws IOException { private ObjectId insertTree(final TreeFormatter treeFormatter) throws IOException {
return repository.getObjectDatabase().newInserter().insert(treeFormatter); try (final ObjectInserter inserter = repository.getObjectDatabase().newInserter()) {
return inserter.insert(treeFormatter);
}
} }
} }