From 7233a7e8d10ec1e6acb644fbda41565c186fe4a2 Mon Sep 17 00:00:00 2001 From: Gleb Koval Date: Tue, 5 Dec 2023 02:00:35 +0000 Subject: [PATCH] Cleanup and privatise commit comparator --- README.md | 4 ++-- src/main/kotlin/tinyvm/Commit.kt | 16 ---------------- src/main/kotlin/tinyvm/Object.kt | 17 +++++++++++++++++ src/main/kotlin/tinyvm/Repository.kt | 2 +- src/main/kotlin/tinyvm/Tree.kt | 6 +++--- 5 files changed, 23 insertions(+), 22 deletions(-) create mode 100644 src/main/kotlin/tinyvm/Object.kt diff --git a/README.md b/README.md index 1aa11e0..fcdee1a 100644 --- a/README.md +++ b/README.md @@ -21,13 +21,13 @@ repositories { dependencies { // other dependencies - implementation("net.koval.teamcity-gitea-test-task:tinyvm:0.1.0") + implementation("net.koval.teamcity-gitea-test-task:tinyvm:0.1.1") } ``` ### Documentation Use autocompletion and hover menus in your IDE, or download the -[generated HTML documentation](https://git.koval.net/cyclane/teamcity-gitea-test-task/releases/download/v0.1.0/tinyvm-0.1.0-javadoc.zip) +[generated HTML documentation](https://git.koval.net/cyclane/teamcity-gitea-test-task/releases/download/v0.1.1/tinyvm-0.1.1-javadoc.zip) from the [latest release](https://git.koval.net/cyclane/teamcity-gitea-test-task/releases). ## Instructions diff --git a/src/main/kotlin/tinyvm/Commit.kt b/src/main/kotlin/tinyvm/Commit.kt index 36823e1..125e688 100644 --- a/src/main/kotlin/tinyvm/Commit.kt +++ b/src/main/kotlin/tinyvm/Commit.kt @@ -1,22 +1,6 @@ package tinyvm -import java.security.MessageDigest import java.time.Instant -import java.util.HexFormat - -/** - * Represents an arbitrary version manager object. - */ -abstract class Object(val type: String) { - abstract val data: String - - fun hash(): String = - HexFormat.of().formatHex( - MessageDigest - .getInstance("SHA-1") - .digest("$type ${data.length}\u0000$data".toByteArray()), - ) -} /** * Commits are a pointer to a 'head' tree with some metadata. diff --git a/src/main/kotlin/tinyvm/Object.kt b/src/main/kotlin/tinyvm/Object.kt new file mode 100644 index 0000000..7240a90 --- /dev/null +++ b/src/main/kotlin/tinyvm/Object.kt @@ -0,0 +1,17 @@ +package tinyvm + +import java.security.MessageDigest +import java.util.HexFormat + +/** + * Represents an arbitrary version manager object. + */ +abstract class Object(val type: String) { + abstract val data: String + + fun hash(): String = + HexFormat.of().formatHex( + MessageDigest.getInstance("SHA-1") + .digest("$type ${data.length}\u0000$data".toByteArray()), + ) +} \ No newline at end of file diff --git a/src/main/kotlin/tinyvm/Repository.kt b/src/main/kotlin/tinyvm/Repository.kt index 813b4ee..f48dcc2 100644 --- a/src/main/kotlin/tinyvm/Repository.kt +++ b/src/main/kotlin/tinyvm/Repository.kt @@ -2,7 +2,7 @@ package tinyvm class HashCollisionException(hash: String) : Exception("Different object types with identical hash '$hash'") -class CommitTimeComparator : Comparator { +private class CommitTimeComparator : Comparator { override fun compare( o1: Commit, o2: Commit, diff --git a/src/main/kotlin/tinyvm/Tree.kt b/src/main/kotlin/tinyvm/Tree.kt index 404aa96..052b371 100644 --- a/src/main/kotlin/tinyvm/Tree.kt +++ b/src/main/kotlin/tinyvm/Tree.kt @@ -22,7 +22,7 @@ class Tree(val nodes: Map) : Node("tree") { when (node) { is Tree -> "+$name/\n" is Blob -> "+$name\n" - } + node.toString().leftMargin() + } + node.toString().insertLeftMargin() }.sorted().joinToString("\n") } @@ -30,7 +30,7 @@ class Tree(val nodes: Map) : Node("tree") { * A blob is a data container. */ class Blob(override val data: String) : Node("blob") { - override fun toString(): String = "blob ${hash()}\n${data.leftMargin()}" + override fun toString(): String = "blob ${hash()}\n${data.insertLeftMargin()}" } -private fun String.leftMargin(): String = split('\n').joinToString("\n") { "| $it" } \ No newline at end of file +private fun String.insertLeftMargin(): String = split('\n').joinToString("\n") { "| $it" } \ No newline at end of file