|
4 | 4 | */ |
5 | 5 | package aws.smithy.kotlin.runtime.content |
6 | 6 |
|
7 | | -public actual class BigInteger actual constructor(value: String) : |
| 7 | +import com.ionspin.kotlin.bignum.integer.util.fromTwosComplementByteArray |
| 8 | +import com.ionspin.kotlin.bignum.integer.util.toTwosComplementByteArray |
| 9 | +import com.ionspin.kotlin.bignum.integer.BigInteger as IonSpinBigInteger |
| 10 | + |
| 11 | +public actual class BigInteger private constructor(private val delegate: IonSpinBigInteger) : |
8 | 12 | Number(), |
9 | 13 | Comparable<BigInteger> { |
10 | | - public actual constructor(bytes: ByteArray) : this("Not yet implemented") |
11 | 14 |
|
12 | | - actual override fun toByte(): Byte { |
13 | | - TODO("Not yet implemented") |
| 15 | + private companion object { |
| 16 | + fun coalesceOrCreate(value: IonSpinBigInteger, left: BigInteger, right: BigInteger): BigInteger = when (value) { |
| 17 | + left.delegate -> left |
| 18 | + right.delegate -> right |
| 19 | + else -> BigInteger(value) |
| 20 | + } |
14 | 21 | } |
15 | 22 |
|
16 | | - actual override fun toDouble(): Double { |
17 | | - TODO("Not yet implemented") |
18 | | - } |
| 23 | + public actual constructor(value: String) : this(IonSpinBigInteger.parseString(value, 10)) |
| 24 | + public actual constructor(bytes: ByteArray) : this(IonSpinBigInteger.fromTwosComplementByteArray(bytes)) |
19 | 25 |
|
20 | | - actual override fun toFloat(): Float { |
21 | | - TODO("Not yet implemented") |
22 | | - } |
| 26 | + actual override fun toByte(): Byte = delegate.byteValue(exactRequired = false) |
| 27 | + actual override fun toDouble(): Double = delegate.doubleValue(exactRequired = false) |
| 28 | + actual override fun toFloat(): Float = delegate.floatValue(exactRequired = false) |
| 29 | + actual override fun toInt(): Int = delegate.intValue(exactRequired = false) |
| 30 | + actual override fun toLong(): Long = delegate.longValue(exactRequired = false) |
| 31 | + actual override fun toShort(): Short = delegate.shortValue(exactRequired = false) |
23 | 32 |
|
24 | | - actual override fun toInt(): Int { |
25 | | - TODO("Not yet implemented") |
26 | | - } |
| 33 | + public actual operator fun plus(other: BigInteger): BigInteger = |
| 34 | + coalesceOrCreate(delegate + other.delegate, this, other) |
27 | 35 |
|
28 | | - actual override fun toLong(): Long { |
29 | | - TODO("Not yet implemented") |
30 | | - } |
31 | | - |
32 | | - actual override fun toShort(): Short { |
33 | | - TODO("Not yet implemented") |
34 | | - } |
| 36 | + public actual operator fun minus(other: BigInteger): BigInteger = |
| 37 | + coalesceOrCreate(delegate - other.delegate, this, other) |
35 | 38 |
|
36 | | - public actual operator fun plus(other: BigInteger): BigInteger = TODO("Not yet implemented") |
37 | | - public actual operator fun minus(other: BigInteger): BigInteger = TODO("Not yet implemented") |
38 | | - public actual fun toByteArray(): ByteArray = TODO("Not yet implemented") |
39 | | - actual override fun compareTo(other: BigInteger): Int = TODO("Not yet implemented") |
| 39 | + public actual fun toByteArray(): ByteArray = delegate.toTwosComplementByteArray() |
| 40 | + actual override fun compareTo(other: BigInteger): Int = delegate.compare(other.delegate) |
40 | 41 | } |
0 commit comments