diff --git a/README.md b/README.md index 2b95801..39b8325 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,8 @@ # Nano ID for Kotlin Maven central -Kotlin Version +Kotlin Version +Java Version license Apache 2.0 _Inspired by the following parent project: [ai/nanoid](https://github.com/ai/nanoid)_ @@ -25,7 +26,7 @@ Add nanoid-kotlin as a dependency to your project. Gradle: ```gradle dependencies { - implementation 'io.viascom.nanoid:nanoid:1.0.0' + implementation 'io.viascom.nanoid:nanoid:1.0.1' } ``` @@ -34,7 +35,7 @@ Maven: io.viascom.nanoid nanoid - 1.0.0 + 1.0.1 ``` diff --git a/build.gradle b/build.gradle index f7e2c9a..4c365e3 100644 --- a/build.gradle +++ b/build.gradle @@ -11,7 +11,7 @@ plugins { ext { major = 1 minor = 0 - patch = 0 + patch = 1 isCiServer = System.getenv("GITHUB_ACTIONS") != null || System.getProperty("GITHUB_ACTIONS") != null } @@ -33,6 +33,9 @@ jar { java { withSourcesJar() withJavadocJar() + toolchain { + languageVersion.set(JavaLanguageVersion.of(17)) + } } repositories { @@ -45,7 +48,7 @@ dependencies { } tasks.withType(KotlinCompile).configureEach { - kotlinOptions.jvmTarget = JavaVersion.VERSION_19 + kotlinOptions.jvmTarget = JavaVersion.VERSION_17 kotlinOptions.freeCompilerArgs = ["-Xjvm-default=all"] } diff --git a/src/main/kotlin/io/viascom/nanoid/NanoId.kt b/src/main/kotlin/io/viascom/nanoid/NanoId.kt index 75a325d..5b3acae 100644 --- a/src/main/kotlin/io/viascom/nanoid/NanoId.kt +++ b/src/main/kotlin/io/viascom/nanoid/NanoId.kt @@ -51,6 +51,7 @@ object NanoId { * @throws IllegalArgumentException if the alphabet is empty or larger than 255 characters, or if the size is not greater than zero. */ @JvmOverloads + @JvmStatic fun generate( @NotNull size: Int = 21, @@ -84,6 +85,7 @@ object NanoId { * @return The generated optimized string. */ @JvmOverloads + @JvmStatic fun generateOptimized(@NotNull size: Int, @NotNull alphabet: String, @NotNull mask: Int, @NotNull step: Int, @NotNull random: Random = SecureRandom()): String { val idBuilder = StringBuilder(size) val bytes = ByteArray(step) @@ -107,6 +109,7 @@ object NanoId { * @param alphabet The set of characters to use for generating the string. * @return The additional bytes factor, rounded to two decimal places. */ + @JvmStatic fun calculateAdditionalBytesFactor(@NotNull alphabet: String): Double { val mask = calculateMask(alphabet) return (1 + abs((mask - alphabet.length.toDouble()) / alphabet.length)).round(2) @@ -118,6 +121,7 @@ object NanoId { * @param alphabet The set of characters to use for generating the string. * @return The calculated mask value. */ + @JvmStatic fun calculateMask(@NotNull alphabet: String) = (2 shl (Integer.SIZE - 1 - Integer.numberOfLeadingZeros(alphabet.length - 1))) - 1 /** @@ -128,6 +132,7 @@ object NanoId { * @param additionalBytesFactor The additional bytes factor. Default value is calculated using `calculateAdditionalBytesFactor()`. * @return The number of random bytes to generate in each iteration. */ + @JvmStatic @JvmOverloads fun calculateStep(@NotNull size: Int, @NotNull alphabet: String, @NotNull additionalBytesFactor: Double = calculateAdditionalBytesFactor(alphabet)) = ceil(additionalBytesFactor * calculateMask(alphabet) * size / alphabet.length).toInt()