-
Notifications
You must be signed in to change notification settings - Fork 74
/
SizeInBytesDecoder.kt
159 lines (127 loc) · 4.42 KB
/
SizeInBytesDecoder.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
@file:Suppress("unused")
package com.sksamuel.hoplite.decoder
import com.sksamuel.hoplite.ConfigFailure.DecodeError
import com.sksamuel.hoplite.ConfigResult
import com.sksamuel.hoplite.DecoderContext
import com.sksamuel.hoplite.Node
import com.sksamuel.hoplite.StringNode
import com.sksamuel.hoplite.fp.invalid
import com.sksamuel.hoplite.fp.valid
import kotlin.reflect.KType
data class SizeInBytes(val size: Long) {
fun bytes() = size
fun octets() = convert(InformationUnit.Octets)
fun kilobytes() = convert(InformationUnit.Kilobytes)
fun kibibytes() = convert(InformationUnit.Kibibytes)
fun megabytes() = convert(InformationUnit.Megabytes)
fun mebibytes() = convert(InformationUnit.Mebibytes)
fun gigabytes() = convert(InformationUnit.Gigabytes)
fun gibibytes() = convert(InformationUnit.Gibibytes)
fun convert(unit: InformationUnit): Long = (size / unit.ratioToPrimary).toLong()
companion object {
fun parse(input: String): SizeInBytes? {
val (size, symbol) = "(\\d+)(.*?)".toRegex().matchEntire(input.trim())?.destructured ?: return null
val unit = InformationUnit::class.sealedSubclasses
.mapNotNull { it.objectInstance }
.find { it.symbol.equals(symbol.trim(), ignoreCase = true) }
?: return null
val bytes = unit.ratioToPrimary * size.toLong()
return SizeInBytes(bytes.toLong())
}
}
}
sealed class InformationUnit {
abstract val symbol: String
abstract val ratioToPrimary: Double
object Bytes : InformationUnit() {
override val symbol = "B"
override val ratioToPrimary: Double = 1.0
}
object Octets : InformationUnit() {
override val ratioToPrimary = 1 / 8.0
override val symbol = "o"
}
object Kilobytes : InformationUnit() {
override val ratioToPrimary = MetricSystem.Kilo
override val symbol = "KB"
}
object Kibibytes : InformationUnit() {
override val ratioToPrimary = BinarySystem.Kilo
override val symbol = "KiB"
}
object Megabytes : InformationUnit() {
override val ratioToPrimary = MetricSystem.Mega
override val symbol = "MB"
}
object Mebibytes : InformationUnit() {
override val ratioToPrimary = BinarySystem.Mega
override val symbol = "MiB"
}
object Gigabytes : InformationUnit() {
override val ratioToPrimary = MetricSystem.Giga
override val symbol = "GB"
}
object Gibibytes : InformationUnit() {
override val ratioToPrimary = BinarySystem.Giga
override val symbol = "GiB"
}
object Terabytes : InformationUnit() {
override val ratioToPrimary = MetricSystem.Tera
override val symbol = "TB"
}
object Tebibytes : InformationUnit() {
override val ratioToPrimary = BinarySystem.Tera
override val symbol = "TiB"
}
object Petabytes : InformationUnit() {
override val ratioToPrimary = MetricSystem.Peta
override val symbol = "PB"
}
object Pebibytes : InformationUnit() {
override val ratioToPrimary = BinarySystem.Peta
override val symbol = "PiB"
}
object Exabytes : InformationUnit() {
override val ratioToPrimary = MetricSystem.Exa
override val symbol = "EB"
}
object Exbibytes : InformationUnit() {
override val ratioToPrimary = BinarySystem.Exa
override val symbol = "EiB"
}
object Zettabytes : InformationUnit() {
override val ratioToPrimary = MetricSystem.Zetta
override val symbol = "ZB"
}
object Zebibytes : InformationUnit() {
override val ratioToPrimary = BinarySystem.Zetta
override val symbol = "ZiB"
}
object Yottabytes : InformationUnit() {
override val ratioToPrimary = MetricSystem.Yotta
override val symbol = "YB"
}
object Yobibytes : InformationUnit() {
override val ratioToPrimary = BinarySystem.Yotta
override val symbol = "YiB"
}
object Bits : InformationUnit() {
override val ratioToPrimary = 0.125
override val symbol = "bit"
}
}
class SizeInBytesDecoder : NonNullableLeafDecoder<SizeInBytes> {
override fun supports(type: KType): Boolean = type.classifier == SizeInBytes::class
override fun safeLeafDecode(node: Node,
type: KType,
context: DecoderContext): ConfigResult<SizeInBytes> {
fun parse(input: String): ConfigResult<SizeInBytes> {
val size = SizeInBytes.parse(input)
return size?.valid() ?: DecodeError(node, type).invalid()
}
return when (node) {
is StringNode -> parse(node.value)
else -> DecodeError(node, type).invalid()
}
}
}