Skip to content

Commit 9339b1b

Browse files
committed
Improve formatting
1 parent 5d51804 commit 9339b1b

File tree

1 file changed

+31
-53
lines changed
  • core/src/main/java/io/snabble/sdk/codes/gs1

1 file changed

+31
-53
lines changed

core/src/main/java/io/snabble/sdk/codes/gs1/GS1Code.kt

Lines changed: 31 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,17 @@ import java.math.BigDecimal
66
import java.math.RoundingMode
77
import java.text.DecimalFormat
88
import kotlin.math.min
9-
import kotlin.math.round
109

1110
class GS1Code(val code: String) {
1211
companion object {
1312
const val GS = "\u001D"
1413

1514
private val symbologyIdentifiers = listOf(
16-
"]C1", // = GS1-128
17-
"]e0", // = GS1 DataBar
18-
"]d2", // = GS1 DataMatrix
19-
"]Q3", // = GS1 QR Code
20-
"]J1" // = GS1 DotCode
15+
"]C1", // = GS1-128
16+
"]e0", // = GS1 DataBar
17+
"]d2", // = GS1 DataMatrix
18+
"]Q3", // = GS1 QR Code
19+
"]J1" // = GS1 DotCode
2120
)
2221
}
2322

@@ -37,7 +36,7 @@ class GS1Code(val code: String) {
3736
}
3837

3938
@Suppress("ControlFlowWithEmptyBody")
40-
while (nextElement()){}
39+
while (nextElement()) {}
4140
}
4241

4342
private fun nextElement(): Boolean {
@@ -91,19 +90,16 @@ class GS1Code(val code: String) {
9190
return false
9291
}
9392

94-
private fun firstValue(prefix: String): String? {
95-
return elements.firstOrNull{
93+
private fun firstValue(prefix: String): String? =
94+
elements.firstOrNull {
9695
it.identifier.prefix.startsWith(prefix)
9796
}?.values?.firstOrNull()
98-
}
9997

100-
private fun firstDecimal(prefix: String): BigDecimal? {
101-
return elements.firstOrNull{ it.identifier.prefix.startsWith(prefix) }?.decimal
102-
}
98+
private fun firstDecimal(prefix: String): BigDecimal? =
99+
elements.firstOrNull { it.identifier.prefix.startsWith(prefix) }?.decimal
103100

104-
private fun firstElement(prefix: String): Element? {
105-
return elements.firstOrNull{ it.identifier.prefix.startsWith(prefix) }
106-
}
101+
private fun firstElement(prefix: String): Element? =
102+
elements.firstOrNull { it.identifier.prefix.startsWith(prefix) }
107103

108104
private fun BigDecimal.trim(): BigDecimal {
109105
val df = DecimalFormat()
@@ -132,9 +128,7 @@ class GS1Code(val code: String) {
132128
}
133129

134130
val weight: Int?
135-
get() {
136-
return getWeight(Unit.GRAM)?.toInt()
137-
}
131+
get() = getWeight(Unit.GRAM)?.toInt()
138132

139133
fun getLength(unit: Unit): BigDecimal? {
140134
if (unit.dimension == Dimension.DISTANCE) {
@@ -154,9 +148,7 @@ class GS1Code(val code: String) {
154148
}
155149

156150
val length: Int?
157-
get() {
158-
return getLength(Unit.MILLIMETER)?.toInt()
159-
}
151+
get() = getLength(Unit.MILLIMETER)?.toInt()
160152

161153
fun getArea(unit: Unit): BigDecimal? {
162154
if (unit.dimension == Dimension.AREA) {
@@ -177,9 +169,7 @@ class GS1Code(val code: String) {
177169
}
178170

179171
val area: Int?
180-
get() {
181-
return getArea(Unit.SQUARE_CENTIMETER)?.toInt()
182-
}
172+
get() = getArea(Unit.SQUARE_CENTIMETER)?.toInt()
183173

184174
fun getLiters(unit: Unit): BigDecimal? {
185175
if (unit.dimension == Dimension.VOLUME) {
@@ -199,9 +189,7 @@ class GS1Code(val code: String) {
199189
}
200190

201191
val liters: Int?
202-
get() {
203-
return getLiters(Unit.MILLILITER)?.toInt()
204-
}
192+
get() = getLiters(Unit.MILLILITER)?.toInt()
205193

206194
fun getVolume(unit: Unit): BigDecimal? {
207195
if (unit.dimension == Dimension.CAPACITY) {
@@ -219,17 +207,15 @@ class GS1Code(val code: String) {
219207
}
220208

221209
val volume: Int?
222-
get() {
223-
return getVolume(Unit.CUBIC_CENTIMETER)?.toInt()
224-
}
210+
get() = getVolume(Unit.CUBIC_CENTIMETER)?.toInt()
225211

226212
val gtin: String?
227-
get() {
228-
return firstValue("01")
229-
}
213+
get() = firstValue("01")
230214

231-
data class Price (val price: BigDecimal,
232-
val currencyCode: String?)
215+
data class Price(
216+
val price: BigDecimal,
217+
val currencyCode: String?
218+
)
233219

234220
val price: Price?
235221
get() {
@@ -248,26 +234,20 @@ class GS1Code(val code: String) {
248234
return null
249235
}
250236

251-
fun getPrice(digits: Int, roundingMode: RoundingMode): Price? {
252-
return price?.let { price ->
253-
return Price(
254-
price = price.price.setScale(digits, roundingMode),
255-
currencyCode = price.currencyCode
237+
fun getPrice(digits: Int, roundingMode: RoundingMode): Price? =
238+
price?.let { price ->
239+
Price(
240+
price = price.price.setScale(digits, roundingMode),
241+
currencyCode = price.currencyCode
256242
)
257243
}
258-
}
259244

260245
val amount: Int?
261-
get() {
262-
return firstValue("30")?.toIntOrNull()
263-
}
246+
get() = firstValue("30")?.toIntOrNull()
264247

265-
fun getEmbeddedData(encodingUnit: Unit?, digits: Int?, roundingMode: RoundingMode?): BigDecimal? {
266-
if (encodingUnit == null) {
267-
return null
268-
}
269-
270-
return when (encodingUnit.dimension) {
248+
fun getEmbeddedData(encodingUnit: Unit?, digits: Int?, roundingMode: RoundingMode?): BigDecimal? =
249+
when (encodingUnit?.dimension) {
250+
null -> null
271251
Dimension.VOLUME -> getLiters(encodingUnit)
272252
Dimension.CAPACITY -> getVolume(encodingUnit)
273253
Dimension.AREA -> getArea(encodingUnit)
@@ -281,7 +261,5 @@ class GS1Code(val code: String) {
281261
this.price?.price
282262
}
283263
}
284-
else -> null
285264
}
286-
}
287265
}

0 commit comments

Comments
 (0)