diff --git a/library/api/api.txt b/library/api/api.txt index 37699ef..77c2e08 100644 --- a/library/api/api.txt +++ b/library/api/api.txt @@ -15,6 +15,7 @@ package me.saket.bytesize { method public inline operator long plus(me.saket.bytesize.ByteSize other); method public inline long times(Number other); method public inline String toString(); + method public operator long unaryMinus(); property public inline long inWholeBytes; property public final inline long inWholeGibibytes; property public final inline long inWholeKibibytes; @@ -32,6 +33,7 @@ package me.saket.bytesize { method public operator me.saket.bytesize.ByteSize minus(me.saket.bytesize.ByteSize other); method public operator me.saket.bytesize.ByteSize plus(me.saket.bytesize.ByteSize other); method public operator me.saket.bytesize.ByteSize times(Number other); + method public operator me.saket.bytesize.ByteSize unaryMinus(); property public abstract long inWholeBytes; } @@ -57,6 +59,7 @@ package me.saket.bytesize { method public inline long plus(me.saket.bytesize.ByteSize other); method public inline long times(Number other); method public inline String toString(); + method public operator long unaryMinus(); property public inline long inWholeBits; property public inline long inWholeBytes; property public final inline long inWholeGigabits; @@ -82,6 +85,7 @@ package me.saket.bytesize { method public inline operator long plus(me.saket.bytesize.ByteSize other); method public inline long times(Number other); method public inline String toString(); + method public operator long unaryMinus(); property public inline long inWholeBytes; property public final inline long inWholeGigabytes; property public final inline long inWholeKilobytes; diff --git a/library/src/commonMain/kotlin/me/saket/bytesize/BinaryByteSize.kt b/library/src/commonMain/kotlin/me/saket/bytesize/BinaryByteSize.kt index 8517e45..c316e57 100644 --- a/library/src/commonMain/kotlin/me/saket/bytesize/BinaryByteSize.kt +++ b/library/src/commonMain/kotlin/me/saket/bytesize/BinaryByteSize.kt @@ -73,6 +73,9 @@ value class BinaryByteSize( override inline fun div(other: Number): BinaryByteSize = BinaryByteSize(commonDiv(other)) + override operator fun unaryMinus(): BinaryByteSize = + BinaryByteSize(-bytes) + override inline fun compareTo(other: ByteSize): Int = commonCompareTo(other) diff --git a/library/src/commonMain/kotlin/me/saket/bytesize/ByteSize.kt b/library/src/commonMain/kotlin/me/saket/bytesize/ByteSize.kt index 8d8f4fc..25f7631 100644 --- a/library/src/commonMain/kotlin/me/saket/bytesize/ByteSize.kt +++ b/library/src/commonMain/kotlin/me/saket/bytesize/ByteSize.kt @@ -22,6 +22,7 @@ sealed interface ByteSize : Comparable { operator fun times(other: Number): ByteSize operator fun div(other: ByteSize): Double operator fun div(other: Number): ByteSize + operator fun unaryMinus(): ByteSize } inline operator fun Number.times(other: ByteSize): ByteSize = diff --git a/library/src/commonMain/kotlin/me/saket/bytesize/DecimalBitSize.kt b/library/src/commonMain/kotlin/me/saket/bytesize/DecimalBitSize.kt index bf03907..cdb6d79 100644 --- a/library/src/commonMain/kotlin/me/saket/bytesize/DecimalBitSize.kt +++ b/library/src/commonMain/kotlin/me/saket/bytesize/DecimalBitSize.kt @@ -83,6 +83,9 @@ value class DecimalBitSize( return DecimalBitSize(bits = commonDiv(other)) } + override operator fun unaryMinus(): DecimalBitSize = + DecimalBitSize(-bits) + override inline fun compareTo(other: ByteSize): Int { return commonCompareTo(other) } diff --git a/library/src/commonMain/kotlin/me/saket/bytesize/DecimalByteSize.kt b/library/src/commonMain/kotlin/me/saket/bytesize/DecimalByteSize.kt index d84b096..9952803 100644 --- a/library/src/commonMain/kotlin/me/saket/bytesize/DecimalByteSize.kt +++ b/library/src/commonMain/kotlin/me/saket/bytesize/DecimalByteSize.kt @@ -73,6 +73,9 @@ value class DecimalByteSize( override inline fun div(other: Number): DecimalByteSize = DecimalByteSize(commonDiv(other)) + override operator fun unaryMinus(): DecimalByteSize = + DecimalByteSize(-bytes) + override inline fun compareTo(other: ByteSize): Int = commonCompareTo(other) diff --git a/library/src/commonTest/kotlin/me/saket/bytesize/BinaryByteSizeTest.kt b/library/src/commonTest/kotlin/me/saket/bytesize/BinaryByteSizeTest.kt index 2828df6..fbd6048 100644 --- a/library/src/commonTest/kotlin/me/saket/bytesize/BinaryByteSizeTest.kt +++ b/library/src/commonTest/kotlin/me/saket/bytesize/BinaryByteSizeTest.kt @@ -115,4 +115,12 @@ class BinaryByteSizeTest { BinaryByteSize(500.50) }.hasMessage(BytePrecisionLossErrorMessage) } + + @Test fun unary_minus() { + val twelve = 12.binaryBytes + val negativeTwelve = -twelve + assertThat(negativeTwelve).isEqualTo((-12).binaryBytes) + val positiveTwelve = -negativeTwelve + assertThat(positiveTwelve).isEqualTo(twelve) + } } diff --git a/library/src/commonTest/kotlin/me/saket/bytesize/DecimalBitSizeTest.kt b/library/src/commonTest/kotlin/me/saket/bytesize/DecimalBitSizeTest.kt index 42cf20a..0b949e8 100644 --- a/library/src/commonTest/kotlin/me/saket/bytesize/DecimalBitSizeTest.kt +++ b/library/src/commonTest/kotlin/me/saket/bytesize/DecimalBitSizeTest.kt @@ -113,4 +113,12 @@ class DecimalBitSizeTest { DecimalBitSize(123.45) }.hasMessage(BitPrecisionLossErrorMessage) } + + @Test fun unary_minus() { + val twelve = 12.decimalBits + val negativeTwelve = -twelve + assertThat(negativeTwelve).isEqualTo((-12).decimalBits) + val positiveTwelve = -negativeTwelve + assertThat(positiveTwelve).isEqualTo(twelve) + } } diff --git a/library/src/commonTest/kotlin/me/saket/bytesize/DecimalByteSizeTest.kt b/library/src/commonTest/kotlin/me/saket/bytesize/DecimalByteSizeTest.kt index a86f3bb..b57fb9f 100644 --- a/library/src/commonTest/kotlin/me/saket/bytesize/DecimalByteSizeTest.kt +++ b/library/src/commonTest/kotlin/me/saket/bytesize/DecimalByteSizeTest.kt @@ -160,4 +160,12 @@ class DecimalByteSizeTest { DecimalByteSize(500.50) }.hasMessage(BytePrecisionLossErrorMessage) } + + @Test fun unary_minus() { + val twelve = 12.decimalBytes + val negativeTwelve = -twelve + assertThat(negativeTwelve).isEqualTo((-12).decimalBytes) + val positiveTwelve = -negativeTwelve + assertThat(positiveTwelve).isEqualTo(twelve) + } }