From a520b91bb96e067aeb0073777c58ec71d0f7a7c6 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Thu, 21 Jun 2018 07:25:46 +0100 Subject: [PATCH] Move constants from interface to a class --- .../java/net/kemitix/gitdb/FormatVersion.java | 51 +++++++++++++++++++ src/main/java/net/kemitix/gitdb/GitDB.java | 7 --- .../net/kemitix/gitdb/impl/InitGitDBRepo.java | 4 +- .../net/kemitix/gitdb/test/GitDBTest.java | 7 +-- 4 files changed, 57 insertions(+), 12 deletions(-) create mode 100644 src/main/java/net/kemitix/gitdb/FormatVersion.java diff --git a/src/main/java/net/kemitix/gitdb/FormatVersion.java b/src/main/java/net/kemitix/gitdb/FormatVersion.java new file mode 100644 index 0000000..7a66ac7 --- /dev/null +++ b/src/main/java/net/kemitix/gitdb/FormatVersion.java @@ -0,0 +1,51 @@ +/* + The MIT License (MIT) + + Copyright (c) 2018 Paul Campbell + + Permission is hereby granted, free of charge, to any person obtaining a copy of this software + and associated documentation files (the "Software"), to deal in the Software without restriction, + including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, + and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, + subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all copies + or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, + INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE + AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package net.kemitix.gitdb; + +import com.github.zafarkhaja.semver.Version; + +import java.nio.charset.StandardCharsets; + +/** + * Defines the version of the GitDB repository format. + * + * @author Paul Campbell (pcampbell@kemitix.net) + */ +public final class FormatVersion { + + private final int major = 1; + private final int minor = 0; + private final int patch = 0; + + /** + * Formats the version as a UTF 8 byte array. + * + * @return a bytes array + */ + public byte[] toBytes() { + return getVersion().toString().getBytes(StandardCharsets.UTF_8); + } + + public Version getVersion() { + return Version.forIntegers(major, minor, patch); + } +} diff --git a/src/main/java/net/kemitix/gitdb/GitDB.java b/src/main/java/net/kemitix/gitdb/GitDB.java index 06c58e7..c458467 100644 --- a/src/main/java/net/kemitix/gitdb/GitDB.java +++ b/src/main/java/net/kemitix/gitdb/GitDB.java @@ -21,7 +21,6 @@ package net.kemitix.gitdb; -import com.github.zafarkhaja.semver.Version; import net.kemitix.gitdb.impl.LocalGitDB; import java.io.IOException; @@ -35,12 +34,6 @@ import java.util.Optional; */ public interface GitDB { - int MAJOR = 1; - int MINOR = 0; - int PATCH = 0; - - Version VERSION = Version.forIntegers(MAJOR, MINOR, PATCH); - /** * Initialise a new local gitdb. * diff --git a/src/main/java/net/kemitix/gitdb/impl/InitGitDBRepo.java b/src/main/java/net/kemitix/gitdb/impl/InitGitDBRepo.java index d0c18e9..a2bd757 100644 --- a/src/main/java/net/kemitix/gitdb/impl/InitGitDBRepo.java +++ b/src/main/java/net/kemitix/gitdb/impl/InitGitDBRepo.java @@ -21,7 +21,7 @@ package net.kemitix.gitdb.impl; -import net.kemitix.gitdb.GitDB; +import net.kemitix.gitdb.FormatVersion; import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.lib.RepositoryCache; @@ -65,7 +65,7 @@ class InitGitDBRepo { private void createInitialBranchOnMaster(final Repository repository) throws IOException { final GitDBRepo repo = new GitDBRepo(repository); final ValueWriter valueWriter = new ValueWriter(repository); - final ObjectId objectId = valueWriter.write(GitDB.VERSION.toString().getBytes(StandardCharsets.UTF_8)); + final ObjectId objectId = valueWriter.write(new FormatVersion().toBytes()); final ObjectId treeId = repo.insertNewTree(GIT_DB_VERSION, objectId); final ObjectId commitId = repo.initialCommit(treeId, INIT_MESSAGE, INIT_USER, INIT_EMAIL); createBranch(repository, commitId, MASTER); diff --git a/src/test/java/net/kemitix/gitdb/test/GitDBTest.java b/src/test/java/net/kemitix/gitdb/test/GitDBTest.java index 01a9f3d..c2736b7 100644 --- a/src/test/java/net/kemitix/gitdb/test/GitDBTest.java +++ b/src/test/java/net/kemitix/gitdb/test/GitDBTest.java @@ -1,6 +1,7 @@ package net.kemitix.gitdb.test; import com.github.zafarkhaja.semver.Version; +import net.kemitix.gitdb.FormatVersion; import net.kemitix.gitdb.GitDB; import net.kemitix.gitdb.GitDBBranch; import net.kemitix.gitdb.InvalidRepositoryException; @@ -8,7 +9,6 @@ import org.assertj.core.api.WithAssertions; import org.eclipse.jgit.api.Git; import org.eclipse.jgit.api.errors.GitAPIException; import org.eclipse.jgit.lib.Constants; -import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.Repository; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -244,8 +244,9 @@ class GitDBTest implements WithAssertions { //when final Optional formatVersion = gitDBBranch.getFormatVersion(); //then - assertThat(formatVersion).contains(GitDB.VERSION); - assertThat(formatVersion.get()).isNotSameAs(GitDB.VERSION); + final Version version = new FormatVersion().getVersion(); + assertThat(formatVersion).contains(version); + assertThat(formatVersion.get()).isNotSameAs(version); } // When putting a key/value pair then a GitDbBranch is returned