Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
plugins {
alias(libs.plugins.kotlin.multiplatform) apply false
alias(libs.plugins.kotlin.jvm) apply false
alias(libs.plugins.shadow) apply false
alias(libs.plugins.bcv) apply false
Expand Down
43 changes: 22 additions & 21 deletions datasize-core/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,48 +1,49 @@
import com.vanniktech.maven.publish.JavadocJar
import com.vanniktech.maven.publish.SourcesJar
import org.jetbrains.kotlin.gradle.dsl.JvmTarget

plugins {
alias(libs.plugins.bcv)
alias(libs.plugins.dokka)
alias(libs.plugins.kotlin.jvm)
alias(libs.plugins.kotlin.multiplatform)
alias(libs.plugins.mcentral.publish)
}

group = "io.github.ardiien.datasize"
version = "2.1.0"

java {
withSourcesJar()
}

kotlin {
jvmToolchain(17)

explicitApi()
compilerOptions {
optIn.add("io.github.ardiien.datasize.ExperimentalDataSizeApi")
}

sourceSets.all {
kotlin.srcDirs("$name/src")
resources.srcDirs("$name/resources")
jvmToolchain(17)
jvm {
withSourcesJar()
compilerOptions {
jvmTarget.set(JvmTarget.JVM_1_8)
freeCompilerArgs.add("-Xjdk-release=8")
}
}
}

tasks.test {
useJUnitPlatform()
}

dependencies {
implementation(libs.kotlinx.collections)
testImplementation(libs.kotlin.test)
sourceSets {
commonMain.dependencies {
implementation(libs.kotlinx.collections)
}
commonTest.dependencies {
implementation(libs.kotlin.test)
}
}
}

dokka {
dokkaSourceSets.main {
dokkaSourceSets.configureEach {
skipDeprecated.set(true)

sourceLink {
localDirectory.set(file("src/main/kotlin"))
remoteUrl("https://github.com/ardiien/datasize")
localDirectory.set(file("src/$name/kotlin"))
remoteUrl("https://github.com/ardiien/datasize/tree/main")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import io.github.ardiien.datasize.DataSize.Companion.MaxValue
import io.github.ardiien.datasize.DataSize.Companion.Zero
import io.github.ardiien.datasize.formatter.SimpleDataSizeFormatter
import io.github.ardiien.datasize.localizer.SimpleDataSizeUnitLocalizer
import io.github.ardiien.datasize.util.addExact
import io.github.ardiien.datasize.util.multiplyExact
import io.github.ardiien.datasize.util.subtractExact
import kotlin.math.roundToLong


Expand Down Expand Up @@ -139,7 +142,7 @@ public value class DataSize internal constructor(internal val rawValue: Long) :
if (other == 0) return Zero

val newValue = try {
Math.multiplyExact(rawValue, other)
multiplyExact(rawValue, other.toLong())
} catch (_: ArithmeticException) {
return MaxValue
}
Expand All @@ -153,7 +156,7 @@ public value class DataSize internal constructor(internal val rawValue: Long) :
*/
public operator fun plus(other: DataSize): DataSize {
val newValue = try {
Math.addExact(rawValue, other.rawValue)
addExact(rawValue, other.rawValue)
} catch (_: ArithmeticException) {
return MaxValue
}
Expand All @@ -166,7 +169,7 @@ public value class DataSize internal constructor(internal val rawValue: Long) :
*/
public operator fun minus(other: DataSize): DataSize {
val newValue = try {
Math.subtractExact(rawValue, other.rawValue)
subtractExact(rawValue, other.rawValue)
} catch (_: ArithmeticException) {
return Zero
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright 2026 ardiien
* Licensed under the Apache License, Version 2.0.
* See http://www.apache.org/licenses/LICENSE-2.0
*/

package io.github.ardiien.datasize.util


/**
* Returns the product of the arguments, throwing an exception if the result
* overflows a [Long].
*
* @param x the first value
* @param y the second value
* @return the result
* @throws ArithmeticException if the result overflows a long
*/
@Throws(ArithmeticException::class)
internal expect fun multiplyExact(x: Long, y: Long): Long

/**
* Returns the sum of its arguments,
* throwing an exception if the result overflows a [Long].
*
* @param x the first value
* @param y the second value
* @return the result
* @throws ArithmeticException if the result overflows a long
*/
@Throws(ArithmeticException::class)
internal expect fun addExact(x: Long, y: Long): Long

/**
* Returns the difference of the arguments,
* throwing an exception if the result overflows a [Long].
*
* @param x the first value
* @param y the second value to subtract from the first
* @return the result
* @throws ArithmeticException if the result overflows a long
*/
@Throws(ArithmeticException::class)
internal expect fun subtractExact(x: Long, y: Long): Long
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ import io.github.ardiien.datasize.DataSize.Companion.mebibytes
import io.github.ardiien.datasize.DataSize.Companion.megabytes
import io.github.ardiien.datasize.DataSize.Companion.tebibytes
import io.github.ardiien.datasize.DataSize.Companion.toDataSize
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertThrows
import org.junit.Assert.assertThrows
import kotlin.test.Ignore
import kotlin.test.Test
import kotlin.test.assertEquals


class DataSizeTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
* Licensed under the Apache License, Version 2.0.
* See http://www.apache.org/licenses/LICENSE-2.0
*/
package io.github.ardiien.datasize
package io.github.ardiien.datasize.formatter

import io.github.ardiien.datasize.DataSize.Companion.bytes
import io.github.ardiien.datasize.DataSize.Companion.gibibytes
import io.github.ardiien.datasize.formatter.SimpleDataSizeFormatter
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import io.github.ardiien.datasize.DefaultDataSizeUnitFormatter
import io.github.ardiien.datasize.IecUnit
import kotlin.test.Test
import kotlin.test.assertEquals


class SimpleDataSizeFormatterTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
* Licensed under the Apache License, Version 2.0.
* See http://www.apache.org/licenses/LICENSE-2.0
*/
package io.github.ardiien.datasize
package io.github.ardiien.datasize.localizer

import io.github.ardiien.datasize.localizer.SimpleDataSizeUnitLocalizer
import io.github.ardiien.datasize.DataSizeTag
import io.github.ardiien.datasize.DataSizeUnit
import io.github.ardiien.datasize.IecUnit
import io.github.ardiien.datasize.SiUnit
import io.github.ardiien.datasize.util.castTo
import kotlinx.collections.immutable.ImmutableList
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import kotlin.test.Test
import kotlin.test.assertEquals


class SimpleDataSizeUnitLocalizerTest {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* Copyright 2026 ardiien
* Licensed under the Apache License, Version 2.0.
* See http://www.apache.org/licenses/LICENSE-2.0
*/
package io.github.ardiien.datasize.util


@Throws(ArithmeticException::class)
internal actual fun multiplyExact(x: Long, y: Long): Long = Math.multiplyExact(x, y)

@Throws(ArithmeticException::class)
internal actual fun addExact(x: Long, y: Long): Long = Math.addExact(x, y)

@Throws(ArithmeticException::class)
internal actual fun subtractExact(x: Long, y: Long): Long = Math.subtractExact(x, y)
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ kotlin-test = { module = "org.jetbrains.kotlin:kotlin-test", version.ref = "kotl

[plugins]
kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }
kotlin-multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" }
shadow = { id = "com.gradleup.shadow", version.ref = "shadow" }
bcv = { id = "org.jetbrains.kotlinx.binary-compatibility-validator", version.ref = "bcv" }
dokka = { id = "org.jetbrains.dokka", version.ref = "dokka" }
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#Fri Jan 16 11:14:46 CET 2026
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-9.4.1-all.zip
distributionSha256Sum=708d2c6ecc97ca9a11838ef64a6c2301151b8dd10387e22dc1a12c30557cab5b
distributionUrl=https\://services.gradle.org/distributions/gradle-9.6.1-all.zip
distributionSha256Sum=61ba77b3ff7167e60962763eb4bae79db7120c189b9544358d0ade3c1e712a83
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
3 changes: 1 addition & 2 deletions samples/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,4 @@ kotlin {

dependencies {
implementation(projects.datasizeCore)
testImplementation(libs.kotlin.test)
}
}
5 changes: 3 additions & 2 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
pluginManagement {
repositories {
gradlePluginPortal()
google()
mavenCentral()
gradlePluginPortal()
}

plugins {
Expand All @@ -11,7 +12,7 @@ pluginManagement {

dependencyResolutionManagement {
repositories {
gradlePluginPortal()
google()
mavenCentral()
}
}
Expand Down