11package de.no3x.kbytes
22
33import kotlin.math.abs
4+ import java.math.BigDecimal
45
56@JvmInline
6- value class StorageSize private constructor (val bytes : Long ) : Comparable<StorageSize> {
7+ value class StorageSize (val bytes : Long ) : Comparable<StorageSize> {
78
89 companion object {
910 fun ofBytes (bytes : Long ) = StorageSize (bytes)
@@ -13,6 +14,19 @@ value class StorageSize private constructor(val bytes: Long) : Comparable<Storag
1314
1415 fun of (value : Long , unit : DecimalUnit ): StorageSize =
1516 StorageSize (value * unit.bytes)
17+
18+ fun of (value : Double , unit : BinaryUnit ): StorageSize =
19+ fromExactDouble(value, unit.bytes)
20+
21+ fun of (value : Double , unit : DecimalUnit ): StorageSize =
22+ fromExactDouble(value, unit.bytes)
23+
24+ private fun fromExactDouble (value : Double , unitBytes : Long ): StorageSize {
25+ val bytes = BigDecimal .valueOf(value)
26+ .multiply(BigDecimal .valueOf(unitBytes))
27+ .longValueExact() // fail fast if fractional bytes would be required
28+ return StorageSize (bytes)
29+ }
1630 }
1731
1832 // Arithmetic (mirrors add/subtract/multiply/divide)
@@ -74,6 +88,15 @@ fun mebibyte(value: Long): StorageSize =
7488fun gibibyte (value : Long ): StorageSize =
7589 StorageSize .of(value, BinaryUnit .GIBIBYTE )
7690
91+ fun tebibyte (value : Long ): StorageSize =
92+ StorageSize .of(value, BinaryUnit .TEBIBYTE )
93+
94+ fun pebibyte (value : Long ): StorageSize =
95+ StorageSize .of(value, BinaryUnit .PEBIBYTE )
96+
97+ fun exbibyte (value : Long ): StorageSize =
98+ StorageSize .of(value, BinaryUnit .EXBIBYTE )
99+
77100fun kilobyte (value : Long ): StorageSize =
78101 StorageSize .of(value, DecimalUnit .KILOBYTE )
79102
@@ -83,20 +106,41 @@ fun megabyte(value: Long): StorageSize =
83106fun gigabyte (value : Long ): StorageSize =
84107 StorageSize .of(value, DecimalUnit .GIGABYTE )
85108
109+ fun terabyte (value : Long ): StorageSize =
110+ StorageSize .of(value, DecimalUnit .TERABYTE )
111+
112+ fun petabyte (value : Long ): StorageSize =
113+ StorageSize .of(value, DecimalUnit .PETABYTE )
114+
115+ fun exabyte (value : Long ): StorageSize =
116+ StorageSize .of(value, DecimalUnit .EXABYTE )
117+
86118// --- Nice Kotlin DSL-style extensions ---------------------------------------
87119
88120val Int .KiB : StorageSize get() = kibibyte(this .toLong())
89121val Int .MiB : StorageSize get() = mebibyte(this .toLong())
90122val Int .GiB : StorageSize get() = gibibyte(this .toLong())
123+ val Int .TiB : StorageSize get() = tebibyte(this .toLong())
124+ val Int .PiB : StorageSize get() = pebibyte(this .toLong())
125+ val Int .EiB : StorageSize get() = exbibyte(this .toLong())
91126
92127val Int .KB : StorageSize get() = kilobyte(this .toLong())
93128val Int .MB : StorageSize get() = megabyte(this .toLong())
94129val Int .GB : StorageSize get() = gigabyte(this .toLong())
130+ val Int .TB : StorageSize get() = terabyte(this .toLong())
131+ val Int .PB : StorageSize get() = petabyte(this .toLong())
132+ val Int .EB : StorageSize get() = exabyte(this .toLong())
95133
96134val Long .KiB : StorageSize get() = kibibyte(this )
97135val Long .MiB : StorageSize get() = mebibyte(this )
98136val Long .GiB : StorageSize get() = gibibyte(this )
137+ val Long .TiB : StorageSize get() = tebibyte(this )
138+ val Long .PiB : StorageSize get() = pebibyte(this )
139+ val Long .EiB : StorageSize get() = exbibyte(this )
99140
100141val Long .KB : StorageSize get() = kilobyte(this )
101142val Long .MB : StorageSize get() = megabyte(this )
102- val Long .GB : StorageSize get() = gigabyte(this )
143+ val Long .GB : StorageSize get() = gigabyte(this )
144+ val Long .TB : StorageSize get() = terabyte(this .toLong())
145+ val Long .PB : StorageSize get() = petabyte(this .toLong())
146+ val Long .EB : StorageSize get() = exabyte(this .toLong())
0 commit comments