Move constants from interface to a class

This commit is contained in:
Paul Campbell 2018-06-21 07:25:46 +01:00
parent b10da3eac6
commit a520b91bb9
4 changed files with 57 additions and 12 deletions

View file

@ -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);
}
}

View file

@ -21,7 +21,6 @@
package net.kemitix.gitdb; package net.kemitix.gitdb;
import com.github.zafarkhaja.semver.Version;
import net.kemitix.gitdb.impl.LocalGitDB; import net.kemitix.gitdb.impl.LocalGitDB;
import java.io.IOException; import java.io.IOException;
@ -35,12 +34,6 @@ import java.util.Optional;
*/ */
public interface GitDB { public interface GitDB {
int MAJOR = 1;
int MINOR = 0;
int PATCH = 0;
Version VERSION = Version.forIntegers(MAJOR, MINOR, PATCH);
/** /**
* Initialise a new local gitdb. * Initialise a new local gitdb.
* *

View file

@ -21,7 +21,7 @@
package net.kemitix.gitdb.impl; 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.ObjectId;
import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.lib.RepositoryCache; import org.eclipse.jgit.lib.RepositoryCache;
@ -65,7 +65,7 @@ class InitGitDBRepo {
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);
final ValueWriter valueWriter = new ValueWriter(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 treeId = repo.insertNewTree(GIT_DB_VERSION, objectId);
final ObjectId commitId = repo.initialCommit(treeId, INIT_MESSAGE, INIT_USER, INIT_EMAIL); final ObjectId commitId = repo.initialCommit(treeId, INIT_MESSAGE, INIT_USER, INIT_EMAIL);
createBranch(repository, commitId, MASTER); createBranch(repository, commitId, MASTER);

View file

@ -1,6 +1,7 @@
package net.kemitix.gitdb.test; package net.kemitix.gitdb.test;
import com.github.zafarkhaja.semver.Version; import com.github.zafarkhaja.semver.Version;
import net.kemitix.gitdb.FormatVersion;
import net.kemitix.gitdb.GitDB; import net.kemitix.gitdb.GitDB;
import net.kemitix.gitdb.GitDBBranch; import net.kemitix.gitdb.GitDBBranch;
import net.kemitix.gitdb.InvalidRepositoryException; 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.Git;
import org.eclipse.jgit.api.errors.GitAPIException; import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.lib.Repository;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtendWith;
@ -244,8 +244,9 @@ class GitDBTest implements WithAssertions {
//when //when
final Optional<Version> formatVersion = gitDBBranch.getFormatVersion(); final Optional<Version> formatVersion = gitDBBranch.getFormatVersion();
//then //then
assertThat(formatVersion).contains(GitDB.VERSION); final Version version = new FormatVersion().getVersion();
assertThat(formatVersion.get()).isNotSameAs(GitDB.VERSION); assertThat(formatVersion).contains(version);
assertThat(formatVersion.get()).isNotSameAs(version);
} }
// When putting a key/value pair then a GitDbBranch is returned // When putting a key/value pair then a GitDbBranch is returned