Skip to content

Commit d92f120

Browse files
committed
added missing converters for BigDecimal, BigInteger, Byte, Char, Short
1 parent ca65923 commit d92f120

File tree

1 file changed

+53
-0
lines changed
  • core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/impl/api

1 file changed

+53
-0
lines changed

core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/impl/api/convert.kt

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import org.jetbrains.kotlinx.dataframe.impl.createStarProjectedType
4040
import org.jetbrains.kotlinx.dataframe.path
4141
import org.jetbrains.kotlinx.dataframe.type
4242
import java.math.BigDecimal
43+
import java.math.BigInteger
4344
import java.net.URL
4445
import java.util.Locale
4546
import kotlin.math.roundToInt
@@ -272,6 +273,8 @@ internal fun createConverter(from: KType, to: KType, options: ParserOptions? = n
272273

273274
BigDecimal::class -> convert<Boolean> { if (it) BigDecimal.ONE else BigDecimal.ZERO }
274275

276+
BigInteger::class -> convert<Boolean> { if (it) BigInteger.ONE else BigInteger.ZERO }
277+
275278
else -> null
276279
}
277280

@@ -283,10 +286,19 @@ internal fun createConverter(from: KType, to: KType, options: ParserOptions? = n
283286
Short::class -> convert<Number> { it.toShort() }
284287
Long::class -> convert<Number> { it.toLong() }
285288
Boolean::class -> convert<Number> { it.toDouble() != 0.0 }
289+
BigDecimal::class -> convert<Number> { it.toBigDecimal() }
290+
BigInteger::class -> convert<Number> { it.toBigInteger() }
291+
else -> null
292+
}
293+
294+
Char::class -> when (toClass) {
295+
Int::class -> convert<Char> { it.code }
286296
else -> null
287297
}
288298

289299
Int::class -> when (toClass) {
300+
Char::class -> convert<Int> { it.toChar() }
301+
290302
Double::class -> convert<Int> { it.toDouble() }
291303

292304
Float::class -> convert<Int> { it.toFloat() }
@@ -299,6 +311,8 @@ internal fun createConverter(from: KType, to: KType, options: ParserOptions? = n
299311

300312
BigDecimal::class -> convert<Int> { it.toBigDecimal() }
301313

314+
BigInteger::class -> convert<Int> { it.toBigInteger() }
315+
302316
Boolean::class -> convert<Int> { it != 0 }
303317

304318
LocalDateTime::class -> convert<Int> { it.toLong().toLocalDateTime(defaultTimeZone) }
@@ -327,7 +341,9 @@ internal fun createConverter(from: KType, to: KType, options: ParserOptions? = n
327341
Float::class -> convert<Double> { it.toFloat() }
328342
Long::class -> convert<Double> { it.roundToLong() }
329343
Short::class -> convert<Double> { it.roundToInt().toShort() }
344+
Byte::class -> convert<Double> { it.roundToInt().toByte() }
330345
BigDecimal::class -> convert<Double> { it.toBigDecimal() }
346+
BigInteger::class -> convert<Double> { it.toBigInteger() }
331347
Boolean::class -> convert<Double> { it != 0.0 }
332348
else -> null
333349
}
@@ -345,6 +361,8 @@ internal fun createConverter(from: KType, to: KType, options: ParserOptions? = n
345361

346362
BigDecimal::class -> convert<Long> { it.toBigDecimal() }
347363

364+
BigInteger::class -> convert<Long> { it.toBigInteger() }
365+
348366
Boolean::class -> convert<Long> { it != 0L }
349367

350368
LocalDateTime::class -> convert<Long> { it.toLocalDateTime(defaultTimeZone) }
@@ -422,21 +440,38 @@ internal fun createConverter(from: KType, to: KType, options: ParserOptions? = n
422440
Double::class -> convert<Float> { it.toDouble() }
423441
Long::class -> convert<Float> { it.roundToLong() }
424442
Int::class -> convert<Float> { it.roundToInt() }
443+
Byte::class -> convert<Float> { it.roundToInt().toByte() }
425444
Short::class -> convert<Float> { it.roundToInt().toShort() }
426445
BigDecimal::class -> convert<Float> { it.toBigDecimal() }
446+
BigInteger::class -> convert<Float> { it.toBigInteger() }
427447
Boolean::class -> convert<Float> { it != 0.0F }
428448
else -> null
429449
}
430450

431451
BigDecimal::class -> when (toClass) {
432452
Double::class -> convert<BigDecimal> { it.toDouble() }
433453
Int::class -> convert<BigDecimal> { it.toInt() }
454+
Byte::class -> convert<BigDecimal> { it.toByte() }
455+
Short::class -> convert<BigDecimal> { it.toShort() }
434456
Float::class -> convert<BigDecimal> { it.toFloat() }
435457
Long::class -> convert<BigDecimal> { it.toLong() }
458+
BigInteger::class -> convert<BigDecimal> { it.toBigInteger() }
436459
Boolean::class -> convert<BigDecimal> { it != BigDecimal.ZERO }
437460
else -> null
438461
}
439462

463+
BigInteger::class -> when (toClass) {
464+
Double::class -> convert<BigInteger> { it.toDouble() }
465+
Int::class -> convert<BigInteger> { it.toInt() }
466+
Byte::class -> convert<BigInteger> { it.toByte() }
467+
Short::class -> convert<BigInteger> { it.toShort() }
468+
Float::class -> convert<BigInteger> { it.toFloat() }
469+
Long::class -> convert<BigInteger> { it.toLong() }
470+
BigDecimal::class -> convert<BigInteger> { it.toBigDecimal() }
471+
Boolean::class -> convert<BigInteger> { it != BigInteger.ZERO }
472+
else -> null
473+
}
474+
440475
LocalDateTime::class -> when (toClass) {
441476
LocalDate::class -> convert<LocalDateTime> { it.date }
442477
LocalTime::class -> convert<LocalDateTime> { it.time }
@@ -540,3 +575,21 @@ internal fun Instant.toLocalDate(zone: TimeZone = defaultTimeZone) = toLocalDate
540575
internal fun Instant.toLocalTime(zone: TimeZone = defaultTimeZone) = toLocalDateTime(zone).time
541576

542577
internal val defaultTimeZone = TimeZone.currentSystemDefault()
578+
579+
internal fun Number.toBigDecimal(): BigDecimal =
580+
when (this) {
581+
is BigDecimal -> this
582+
is BigInteger -> BigDecimal(this)
583+
is Double -> BigDecimal.valueOf(this)
584+
is Int -> BigDecimal(this)
585+
is Long -> BigDecimal.valueOf(this)
586+
else -> BigDecimal.valueOf(this.toDouble())
587+
}
588+
589+
internal fun Number.toBigInteger(): BigInteger =
590+
when (this) {
591+
is BigInteger -> this
592+
is BigDecimal -> this.toBigInteger()
593+
is Long -> BigInteger.valueOf(this)
594+
else -> BigInteger.valueOf(this.toLong())
595+
}

0 commit comments

Comments
 (0)