Skip to content

Commit a6600b0

Browse files
feat(core): do not implement Number
1 parent d3a3315 commit a6600b0

File tree

10 files changed

+43
-105
lines changed

10 files changed

+43
-105
lines changed

kbigint/src/androidMain/kotlin/io/github/observeroftime/kbigint/KBigInt.kt

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import android.annotation.TargetApi
66
import android.os.Build.VERSION_CODES
77
import java.math.BigInteger
88

9-
actual class KBigInt private constructor(private var value: BigInteger) : Comparable<KBigInt>, Number() {
9+
actual class KBigInt private constructor(private var value: BigInteger) : Comparable<KBigInt> {
1010
actual constructor(number: String) : this(BigInteger(number))
1111

1212
actual constructor(number: Int) : this(number.toBigInteger())
@@ -93,17 +93,26 @@ actual class KBigInt private constructor(private var value: BigInteger) : Compar
9393

9494
actual override fun toString() = value.toString()
9595

96-
override fun toByte() = value.toByte()
97-
98-
override fun toDouble() = value.toDouble()
99-
100-
override fun toFloat() = value.toFloat()
101-
102-
override fun toInt() = value.toInt()
96+
/**
97+
* Convert the value to an [Int].
98+
*
99+
* @see [BigInteger.toInt]
100+
*/
101+
fun toInt() = value.toInt()
103102

104-
override fun toLong() = value.toLong()
103+
/**
104+
* Convert the value to a [Long].
105+
*
106+
* @see [BigInteger.toLong]
107+
*/
108+
fun toLong() = value.toLong()
105109

106-
override fun toShort() = value.toShort()
110+
/**
111+
* Convert the value to a [Double].
112+
*
113+
* @see [BigInteger.toDouble]
114+
*/
115+
fun toDouble() = value.toDouble()
107116

108117
/** Convert the value to a [ByteArray]. */
109118
@Suppress("unused")

kbigint/src/androidUnitTest/kotlin/io/github/observeroftime/kbigint/KBigIntTest.kt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,9 @@ actual class KBigIntTest {
9696
}
9797

9898
@Test
99-
actual fun testToNumber() {
100-
assertEquals(OVER_MAX_INT.toByte(), long.toByte())
101-
assertEquals(OVER_MAX_INT.toDouble(), long.toDouble())
102-
assertEquals(OVER_MAX_INT.toFloat(), long.toFloat())
99+
fun testToNumber() {
103100
assertEquals(OVER_MAX_INT.toInt(), long.toInt())
104101
assertEquals(OVER_MAX_INT, long.toLong())
105-
assertEquals(OVER_MAX_INT.toShort(), long.toShort())
102+
assertEquals(OVER_MAX_INT.toDouble(), long.toDouble())
106103
}
107104
}

kbigint/src/commonTest/kotlin/io/github/observeroftime/kbigint/KBigIntTest.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,4 @@ expect class KBigIntTest {
1818
fun testEquals()
1919
fun testHashCode()
2020
fun testToString()
21-
fun testToNumber()
2221
}

kbigint/src/jsMain/kotlin/io/github/observeroftime/kbigint/KBigInt.kt

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ package io.github.observeroftime.kbigint
33
@JsExport
44
@Suppress("UNUSED_VARIABLE")
55
@OptIn(ExperimentalStdlibApi::class, ExperimentalJsExport::class)
6-
actual class KBigInt private constructor(
7-
@JsExternalArgument private var value: BigInt
8-
) : Comparable<KBigInt>, Number() {
6+
actual class KBigInt private constructor(@JsExternalArgument private var value: BigInt) : Comparable<KBigInt> {
97
@JsName("fromString")
108
actual constructor(number: String) : this(BigInt(number))
119

@@ -140,38 +138,15 @@ actual class KBigInt private constructor(
140138

141139
actual override fun hashCode() = toString().hashCode()
142140

143-
/**
144-
* Convert the value to a [Byte].
145-
*
146-
* @throws [NumberFormatException] if the value does not fit in [Byte]
147-
*/
148-
override fun toByte() = toString().toByte()
149-
150-
override fun toDouble() = toString().toDouble()
151-
152-
override fun toFloat() = toString().toFloat()
153-
154141
/**
155142
* Convert the value to an [Int].
156143
*
157144
* @throws [NumberFormatException] if the value does not fit in [Int]
158145
*/
159-
override fun toInt() = toString().toInt()
146+
fun toInt() = toString().toInt()
160147

161-
/**
162-
* Convert the value to a [Long].
163-
*
164-
* @throws [NumberFormatException] if the value does not fit in [Long]
165-
*/
166-
@Suppress("NON_EXPORTABLE_TYPE")
167-
override fun toLong() = toString().toLong()
168-
169-
/**
170-
* Convert the value to a [Short].
171-
*
172-
* @throws [NumberFormatException] if the value does not fit in [Short]
173-
*/
174-
override fun toShort() = toString().toShort()
148+
/** Convert the value to a [Double]. */
149+
fun toDouble() = toString().toDouble()
175150

176151
actual override fun toString() = value.toString()
177152
}

kbigint/src/jsTest/kotlin/io/github/observeroftime/kbigint/KBigIntTest.kt

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,8 @@ actual class KBigIntTest {
9999
}
100100

101101
@Test
102-
actual fun testToNumber() {
103-
assertFailsWith(NumberFormatException::class) { long.toByte() }
104-
assertFailsWith(NumberFormatException::class) { long.toShort() }
102+
fun testToNumber() {
105103
assertFailsWith(NumberFormatException::class) { long.toInt() }
106-
107104
assertEquals(OVER_MAX_INT.toDouble(), long.toDouble())
108-
assertEquals(OVER_MAX_INT.toFloat(), long.toFloat())
109-
assertEquals(OVER_MAX_INT, long.toLong())
110105
}
111106
}

kbigint/src/jvmMain/kotlin/io/github/observeroftime/kbigint/KBigInt.kt

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ package io.github.observeroftime.kbigint
44

55
import java.math.BigInteger
66

7-
actual class KBigInt private constructor(private var value: BigInteger) : Comparable<KBigInt>, Number() {
7+
actual class KBigInt private constructor(private var value: BigInteger) : Comparable<KBigInt> {
88
actual constructor(number: String) : this(BigInteger(number))
99

1010
actual constructor(number: Int) : this(number.toBigInteger())
@@ -88,27 +88,14 @@ actual class KBigInt private constructor(private var value: BigInteger) : Compar
8888

8989
actual override fun toString() = value.toString()
9090

91-
/**
92-
* Convert the value to a [Byte].
93-
*
94-
* @throws [ArithmeticException] if the value does not fit in [Byte]
95-
* @see [BigInteger.byteValueExact]
96-
*/
97-
@Throws(ArithmeticException::class)
98-
override fun toByte() = value.byteValueExact()
99-
100-
override fun toDouble() = value.toDouble()
101-
102-
override fun toFloat() = value.toFloat()
103-
10491
/**
10592
* Convert the value to an [Int].
10693
*
10794
* @throws [ArithmeticException] if the value does not fit in [Int]
10895
* @see [BigInteger.intValueExact]
10996
*/
11097
@Throws(ArithmeticException::class)
111-
override fun toInt() = value.intValueExact()
98+
fun toInt() = value.intValueExact()
11299

113100
/**
114101
* Convert the value to a [Long].
@@ -117,16 +104,14 @@ actual class KBigInt private constructor(private var value: BigInteger) : Compar
117104
* @see [BigInteger.longValueExact]
118105
*/
119106
@Throws(ArithmeticException::class)
120-
override fun toLong() = value.longValueExact()
107+
fun toLong() = value.longValueExact()
121108

122109
/**
123-
* Convert the value to a [Short].
110+
* Convert the value to a [Double].
124111
*
125-
* @throws [ArithmeticException] if the value does not fit in [Short]
126-
* @see [BigInteger.shortValueExact]
112+
* @see [BigInteger.toDouble]
127113
*/
128-
@Throws(ArithmeticException::class)
129-
override fun toShort() = value.shortValueExact()
114+
fun toDouble() = value.toDouble()
130115

131116
/**
132117
* Convert the value to a [ByteArray].

kbigint/src/jvmTest/kotlin/io/github/observeroftime/kbigint/KBigIntTest.kt

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,9 @@ actual class KBigIntTest {
9696
}
9797

9898
@Test
99-
actual fun testToNumber() {
100-
assertFailsWith(ArithmeticException::class) { long.toByte() }
101-
assertFailsWith(ArithmeticException::class) { long.toShort() }
99+
fun testToNumber() {
102100
assertFailsWith(ArithmeticException::class) { long.toInt() }
103-
104-
assertEquals(OVER_MAX_INT.toDouble(), long.toDouble())
105-
assertEquals(OVER_MAX_INT.toFloat(), long.toFloat())
106101
assertEquals(OVER_MAX_INT, long.toLong())
102+
assertEquals(OVER_MAX_INT.toDouble(), long.toDouble())
107103
}
108104
}

kbigint/src/nativeInterop/cinterop/tommath.def

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,3 @@ static inline int kbi_mp_sign(mp_int *a) {
5858
static inline mp_err kbi_mp_pow(mp_int *a, int n, mp_int *b) {
5959
return n == 2 ? mp_sqr(a, b) : mp_expt_n(a, n, b);
6060
}
61-
62-
static inline char kbi_mp_get_byte(mp_int *a) {
63-
return (char) mp_get_i32(a);
64-
}
65-
66-
static inline short kbi_mp_get_short(mp_int *a) {
67-
return (short) mp_get_i32(a);
68-
}
69-
70-
static inline float kbi_mp_get_float(mp_int *a) {
71-
return (float) mp_get_double(a);
72-
}

kbigint/src/nativeMain/kotlin/io/github/observeroftime/kbigint/KBigInt.kt

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import net.libtom.libtommath.*
88

99
@ObjCName("KBigInt")
1010
@OptIn(ExperimentalForeignApi::class, ExperimentalObjCName::class)
11-
actual class KBigInt private constructor(private var value: mp_int) : Comparable<KBigInt>, Number() {
11+
actual class KBigInt private constructor(private var value: mp_int) : Comparable<KBigInt> {
1212
private constructor() : this(nativeHeap.alloc<mp_int>())
1313

1414
@Suppress("unused")
@@ -346,17 +346,14 @@ actual class KBigInt private constructor(private var value: mp_int) : Comparable
346346
return string
347347
}
348348

349-
override fun toByte() = kbi_mp_get_byte(value.ptr)
349+
/** Convert the value to an [Int]. */
350+
fun toInt() = mp_get_i32(value.ptr)
350351

351-
override fun toDouble() = mp_get_double(value.ptr)
352+
/** Convert the value to a [Long]. */
353+
fun toLong() = mp_get_i64(value.ptr)
352354

353-
override fun toFloat() = kbi_mp_get_float(value.ptr)
354-
355-
override fun toInt() = mp_get_i32(value.ptr)
356-
357-
override fun toLong() = mp_get_i64(value.ptr)
358-
359-
override fun toShort() = kbi_mp_get_short(value.ptr)
355+
/** Convert the value to a [Double]. */
356+
fun toDouble() = mp_get_double(value.ptr)
360357

361358
private inline fun dispose(value: mp_int) {
362359
mp_clear(value.ptr)

kbigint/src/nativeTest/kotlin/io/github/observeroftime/kbigint/KBigIntTest.kt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,9 @@ actual class KBigIntTest {
112112
}
113113

114114
@Test
115-
actual fun testToNumber() {
116-
assertEquals(OVER_MAX_INT.toByte(), long.toByte())
117-
assertEquals(OVER_MAX_INT.toDouble(), long.toDouble())
118-
assertEquals(OVER_MAX_INT.toFloat(), long.toFloat())
115+
fun testToNumber() {
119116
assertEquals(OVER_MAX_INT.toInt(), long.toInt())
120117
assertEquals(OVER_MAX_INT, long.toLong())
121-
assertEquals(OVER_MAX_INT.toShort(), long.toShort())
118+
assertEquals(OVER_MAX_INT.toDouble(), long.toDouble())
122119
}
123120
}

0 commit comments

Comments
 (0)