Skip to content

Commit 20d1021

Browse files
author
Dr. Brandon Wiley
committed
Converted symbols to use BigInteger and made the code smaller
1 parent eb5847f commit 20d1021

File tree

11 files changed

+182
-332
lines changed

11 files changed

+182
-332
lines changed

Codex/src/main/java/org/operatorfoundation/codex/symbols/Binary.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@ class Binary : Symbol {
1111

1212
override fun decode(encodedValue: ByteArray): BigInteger {
1313
return when (encodedValue.decodeToString()) {
14-
"0" -> 0
15-
"1" -> 1
14+
"0" -> 0.toBigInteger()
15+
"1" -> 1.toBigInteger()
1616
else -> throw IllegalArgumentException("Binary, bad value $encodedValue")
1717
}
1818
}
1919

2020
override fun encode(numericValue: BigInteger): ByteArray {
2121
return when (numericValue) {
22-
0 -> "0".toByteArray()
23-
1 -> "1".toByteArray()
22+
0.toBigInteger() -> "0".toByteArray()
23+
1.toBigInteger() -> "1".toByteArray()
2424
else -> throw IllegalArgumentException("Unknown value $numericValue for Binary")
2525
}
2626
}
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.operatorfoundation.codex.symbols
22

3+
import java.math.BigInteger
4+
35
/**
46
* Byte symbol - encodes/decodes byte values (0-255).
57
* Used for binary data encoding.
@@ -8,19 +10,19 @@ class Byte : Symbol {
810
override fun size() = 256
911
override fun toString() = "Byte"
1012

11-
override fun encode(numericValue: Int): ByteArray {
12-
if (numericValue < 0 || numericValue > 255) {
13+
override fun encode(numericValue: BigInteger): ByteArray {
14+
if (numericValue < 0.toBigInteger() || numericValue > 255.toBigInteger()) {
1315
throw Exception("Invalid value $numericValue for Byte (must be 0-255)")
1416
}
15-
return numericValue.toChar().toString().toByteArray()
17+
return numericValue.toInt().toChar().toString().toByteArray()
1618
}
1719

18-
override fun decode(encodedValue: ByteArray): Int {
20+
override fun decode(encodedValue: ByteArray): BigInteger {
1921
if (encodedValue.size != 1)
2022
{
2123
throw Exception("Too many bytes")
2224
}
2325

24-
return encodedValue[0].toInt()
26+
return encodedValue[0].toInt().toBigInteger()
2527
}
2628
}

Codex/src/main/java/org/operatorfoundation/codex/symbols/CallAny.kt

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
11
package org.operatorfoundation.codex.symbols
22

3+
import java.math.BigInteger
4+
35
class CallAny : Symbol {
46
companion object {
57
private val charToValue = mapOf(
6-
"A" to 0, "B" to 1, "C" to 2, "D" to 3, "E" to 4, "F" to 5,
7-
"G" to 6, "H" to 7, "I" to 8, "J" to 9, "K" to 10, "L" to 11,
8-
"M" to 12, "N" to 13, "O" to 14, "P" to 15, "Q" to 16, "R" to 17,
9-
"S" to 18, "T" to 19, "U" to 20, "V" to 21, "W" to 22, "X" to 23,
10-
"Y" to 24, "Z" to 25, "0" to 26, "1" to 27, "2" to 28, "3" to 29,
11-
"4" to 30, "5" to 31, "6" to 32, "7" to 33, "8" to 34, "9" to 35,
12-
" " to 36
8+
"A" to BigInteger.valueOf(0), "B" to BigInteger.valueOf(1), "C" to BigInteger.valueOf(2),
9+
"D" to BigInteger.valueOf(3), "E" to BigInteger.valueOf(4), "F" to BigInteger.valueOf(5),
10+
"G" to BigInteger.valueOf(6), "H" to BigInteger.valueOf(7), "I" to BigInteger.valueOf(8),
11+
"J" to BigInteger.valueOf(9), "K" to BigInteger.valueOf(10), "L" to BigInteger.valueOf(11),
12+
"M" to BigInteger.valueOf(12), "N" to BigInteger.valueOf(13), "O" to BigInteger.valueOf(14),
13+
"P" to BigInteger.valueOf(15), "Q" to BigInteger.valueOf(16), "R" to BigInteger.valueOf(17),
14+
"S" to BigInteger.valueOf(18), "T" to BigInteger.valueOf(19), "U" to BigInteger.valueOf(20),
15+
"V" to BigInteger.valueOf(21), "W" to BigInteger.valueOf(22), "X" to BigInteger.valueOf(23),
16+
"Y" to BigInteger.valueOf(24), "Z" to BigInteger.valueOf(25), "0" to BigInteger.valueOf(26),
17+
"1" to BigInteger.valueOf(27), "2" to BigInteger.valueOf(28), "3" to BigInteger.valueOf(29),
18+
"4" to BigInteger.valueOf(30), "5" to BigInteger.valueOf(31), "6" to BigInteger.valueOf(32),
19+
"7" to BigInteger.valueOf(33), "8" to BigInteger.valueOf(34), "9" to BigInteger.valueOf(35),
20+
" " to BigInteger.valueOf(36)
1321
)
1422

1523
private val valueToChar = charToValue.map { (k, v) -> v to k }.toMap()
@@ -19,12 +27,12 @@ class CallAny : Symbol {
1927

2028
override fun toString(): String = "CallAny"
2129

22-
override fun decode(encodedValue: ByteArray): Int {
30+
override fun decode(encodedValue: ByteArray): BigInteger {
2331
return charToValue[encodedValue.toString()]
2432
?: throw IllegalArgumentException("CallAny, bad value: $encodedValue")
2533
}
2634

27-
override fun encode(numericValue: Int): ByteArray {
35+
override fun encode(numericValue: BigInteger): ByteArray {
2836
return (valueToChar[numericValue]
2937
?: throw IllegalArgumentException("Unknown value $numericValue for CallAny"))
3038
.toByteArray()

Codex/src/main/java/org/operatorfoundation/codex/symbols/CallLetter.kt

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
package org.operatorfoundation.codex.symbols
2+
import java.math.BigInteger
23

34
class CallLetter : Symbol {
45
companion object {
56
private val charToValue = mapOf(
6-
"A" to 0, "B" to 1, "C" to 2, "D" to 3, "E" to 4, "F" to 5,
7-
"G" to 6, "H" to 7, "I" to 8, "J" to 9, "K" to 10, "L" to 11,
8-
"M" to 12, "N" to 13, "O" to 14, "P" to 15, "Q" to 16, "R" to 17,
9-
"S" to 18, "T" to 19, "U" to 20, "V" to 21, "W" to 22, "X" to 23,
10-
"Y" to 24, "Z" to 25
7+
"A" to 0.toBigInteger(), "B" to 1.toBigInteger(), "C" to 2.toBigInteger(),
8+
"D" to 3.toBigInteger(), "E" to 4.toBigInteger(), "F" to 5.toBigInteger(),
9+
"G" to 6.toBigInteger(), "H" to 7.toBigInteger(), "I" to 8.toBigInteger(),
10+
"J" to 9.toBigInteger(), "K" to 10.toBigInteger(), "L" to 11.toBigInteger(),
11+
"M" to 12.toBigInteger(), "N" to 13.toBigInteger(), "O" to 14.toBigInteger(),
12+
"P" to 15.toBigInteger(), "Q" to 16.toBigInteger(), "R" to 17.toBigInteger(),
13+
"S" to 18.toBigInteger(), "T" to 19.toBigInteger(), "U" to 20.toBigInteger(),
14+
"V" to 21.toBigInteger(), "W" to 22.toBigInteger(), "X" to 23.toBigInteger(),
15+
"Y" to 24.toBigInteger(), "Z" to 25.toBigInteger()
1116
)
1217

1318
private val valueToChar = charToValue.map { (k, v) -> v to k }.toMap()
@@ -17,12 +22,12 @@ class CallLetter : Symbol {
1722

1823
override fun toString(): String = "CallLetter"
1924

20-
override fun decode(encodedValue: ByteArray): Int {
25+
override fun decode(encodedValue: ByteArray): BigInteger {
2126
return charToValue[encodedValue.toString()]
2227
?: throw IllegalArgumentException("CallLetter, bad value: $encodedValue")
2328
}
2429

25-
override fun encode(numericValue: Int): ByteArray {
30+
override fun encode(numericValue: BigInteger): ByteArray {
2631
return (valueToChar[numericValue]
2732
?: throw IllegalArgumentException("Unknown value $numericValue for CallLetter"))
2833
.toByteArray()
Lines changed: 28 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,38 @@
11
package org.operatorfoundation.codex.symbols
2+
import java.math.BigInteger
23

34
class CallLetterNumber : Symbol {
4-
override fun size(): Int {
5-
return 26 + 10
6-
}
5+
companion object {
6+
private val charToValue = mapOf(
7+
"A" to 0.toBigInteger(), "B" to 1.toBigInteger(), "C" to 2.toBigInteger(),
8+
"D" to 3.toBigInteger(), "E" to 4.toBigInteger(), "F" to 5.toBigInteger(),
9+
"G" to 6.toBigInteger(), "H" to 7.toBigInteger(), "I" to 8.toBigInteger(),
10+
"J" to 9.toBigInteger(), "K" to 10.toBigInteger(), "L" to 11.toBigInteger(),
11+
"M" to 12.toBigInteger(), "N" to 13.toBigInteger(), "O" to 14.toBigInteger(),
12+
"P" to 15.toBigInteger(), "Q" to 16.toBigInteger(), "R" to 17.toBigInteger(),
13+
"S" to 18.toBigInteger(), "T" to 19.toBigInteger(), "U" to 20.toBigInteger(),
14+
"V" to 21.toBigInteger(), "W" to 22.toBigInteger(), "X" to 23.toBigInteger(),
15+
"Y" to 24.toBigInteger(), "Z" to 25.toBigInteger(), "0" to 26.toBigInteger(),
16+
"1" to 27.toBigInteger(), "2" to 28.toBigInteger(), "3" to 29.toBigInteger(),
17+
"4" to 30.toBigInteger(), "5" to 31.toBigInteger(), "6" to 32.toBigInteger(),
18+
"7" to 33.toBigInteger(), "8" to 34.toBigInteger(), "9" to 35.toBigInteger()
19+
)
720

8-
override fun toString(): String {
9-
return "CallLetterNumber"
21+
private val valueToChar = charToValue.map { (k, v) -> v to k }.toMap()
1022
}
1123

12-
override fun decode(encodedValue: ByteArray): Int {
13-
return when (encodedValue.toString()) {
14-
"A" -> 0
15-
"B" -> 1
16-
"C" -> 2
17-
"D" -> 3
18-
"E" -> 4
19-
"F" -> 5
20-
"G" -> 6
21-
"H" -> 7
22-
"I" -> 8
23-
"J" -> 9
24-
"K" -> 10
25-
"L" -> 11
26-
"M" -> 12
27-
"N" -> 13
28-
"O" -> 14
29-
"P" -> 15
30-
"Q" -> 16
31-
"R" -> 17
32-
"S" -> 18
33-
"T" -> 19
34-
"U" -> 20
35-
"V" -> 21
36-
"W" -> 22
37-
"X" -> 23
38-
"Y" -> 24
39-
"Z" -> 25
40-
"0" -> 26
41-
"1" -> 27
42-
"2" -> 28
43-
"3" -> 29
44-
"4" -> 30
45-
"5" -> 31
46-
"6" -> 32
47-
"7" -> 33
48-
"8" -> 34
49-
"9" -> 35
50-
else -> throw IllegalArgumentException("CallLetterNumber, bad value: $encodedValue")
51-
}
24+
override fun size(): Int = 26 + 10
25+
26+
override fun toString(): String = "CallLetterNumber"
27+
28+
override fun decode(encodedValue: ByteArray): BigInteger {
29+
return charToValue[encodedValue.toString()]
30+
?: throw IllegalArgumentException("CallLetterNumber, bad value: $encodedValue")
5231
}
5332

54-
override fun encode(numericValue: Int): ByteArray {
55-
return when (numericValue) {
56-
0 -> "A".toByteArray()
57-
1 -> "B".toByteArray()
58-
2 -> "C".toByteArray()
59-
3 -> "D".toByteArray()
60-
4 -> "E".toByteArray()
61-
5 -> "F".toByteArray()
62-
6 -> "G".toByteArray()
63-
7 -> "H".toByteArray()
64-
8 -> "I".toByteArray()
65-
9 -> "J".toByteArray()
66-
10 -> "K".toByteArray()
67-
11 -> "L".toByteArray()
68-
12 -> "M".toByteArray()
69-
13 -> "N".toByteArray()
70-
14 -> "O".toByteArray()
71-
15 -> "P".toByteArray()
72-
16 -> "Q".toByteArray()
73-
17 -> "R".toByteArray()
74-
18 -> "S".toByteArray()
75-
19 -> "T".toByteArray()
76-
20 -> "U".toByteArray()
77-
21 -> "V".toByteArray()
78-
22 -> "W".toByteArray()
79-
23 -> "X".toByteArray()
80-
24 -> "Y".toByteArray()
81-
25 -> "Z".toByteArray()
82-
26 -> "0".toByteArray()
83-
27 -> "1".toByteArray()
84-
28 -> "2".toByteArray()
85-
29 -> "3".toByteArray()
86-
30 -> "4".toByteArray()
87-
31 -> "5".toByteArray()
88-
32 -> "6".toByteArray()
89-
33 -> "7".toByteArray()
90-
34 -> "8".toByteArray()
91-
35 -> "9".toByteArray()
92-
else -> throw IllegalArgumentException("Unknown value $numericValue for CallLetterNumber")
93-
}
33+
override fun encode(numericValue: BigInteger): ByteArray {
34+
return (valueToChar[numericValue]
35+
?: throw IllegalArgumentException("Unknown value $numericValue for CallLetterNumber"))
36+
.toByteArray()
9437
}
9538
}
Lines changed: 25 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,35 @@
11
package org.operatorfoundation.codex.symbols
2+
import java.math.BigInteger
23

34
class CallLetterSpace : Symbol {
4-
override fun size(): Int
5-
{
6-
return 27
7-
}
5+
companion object {
6+
private val charToValue = mapOf(
7+
"A" to 0.toBigInteger(), "B" to 1.toBigInteger(), "C" to 2.toBigInteger(),
8+
"D" to 3.toBigInteger(), "E" to 4.toBigInteger(), "F" to 5.toBigInteger(),
9+
"G" to 6.toBigInteger(), "H" to 7.toBigInteger(), "I" to 8.toBigInteger(),
10+
"J" to 9.toBigInteger(), "K" to 10.toBigInteger(), "L" to 11.toBigInteger(),
11+
"M" to 12.toBigInteger(), "N" to 13.toBigInteger(), "O" to 14.toBigInteger(),
12+
"P" to 15.toBigInteger(), "Q" to 16.toBigInteger(), "R" to 17.toBigInteger(),
13+
"S" to 18.toBigInteger(), "T" to 19.toBigInteger(), "U" to 20.toBigInteger(),
14+
"V" to 21.toBigInteger(), "W" to 22.toBigInteger(), "X" to 23.toBigInteger(),
15+
"Y" to 24.toBigInteger(), "Z" to 25.toBigInteger(), " " to 26.toBigInteger()
16+
)
817

9-
override fun toString(): String {
10-
return "CallLetterSpace"
18+
private val valueToChar = charToValue.map { (k, v) -> v to k }.toMap()
1119
}
1220

13-
override fun decode(encodedValue: ByteArray): Int {
14-
return when (encodedValue.toString()) {
15-
"A" -> 0
16-
"B" -> 1
17-
"C" -> 2
18-
"D" -> 3
19-
"E" -> 4
20-
"F" -> 5
21-
"G" -> 6
22-
"H" -> 7
23-
"I" -> 8
24-
"J" -> 9
25-
"K" -> 10
26-
"L" -> 11
27-
"M" -> 12
28-
"N" -> 13
29-
"O" -> 14
30-
"P" -> 15
31-
"Q" -> 16
32-
"R" -> 17
33-
"S" -> 18
34-
"T" -> 19
35-
"U" -> 20
36-
"V" -> 21
37-
"W" -> 22
38-
"X" -> 23
39-
"Y" -> 24
40-
"Z" -> 25
41-
" " -> 26
42-
else -> throw IllegalArgumentException("CallLetterSpace, bad value: $encodedValue")
43-
}
21+
override fun size(): Int = 27
22+
23+
override fun toString(): String = "CallLetterSpace"
24+
25+
override fun decode(encodedValue: ByteArray): BigInteger {
26+
return charToValue[encodedValue.toString()]
27+
?: throw IllegalArgumentException("CallLetterSpace, bad value: $encodedValue")
4428
}
4529

46-
override fun encode(numericValue: Int): ByteArray {
47-
return when (numericValue) {
48-
0 -> "A".toByteArray()
49-
1 -> "B".toByteArray()
50-
2 -> "C".toByteArray()
51-
3 -> "D".toByteArray()
52-
4 -> "E".toByteArray()
53-
5 -> "F".toByteArray()
54-
6 -> "G".toByteArray()
55-
7 -> "H".toByteArray()
56-
8 -> "I".toByteArray()
57-
9 -> "J".toByteArray()
58-
10 -> "K".toByteArray()
59-
11 -> "L".toByteArray()
60-
12 -> "M".toByteArray()
61-
13 -> "N".toByteArray()
62-
14 -> "O".toByteArray()
63-
15 -> "P".toByteArray()
64-
16 -> "Q".toByteArray()
65-
17 -> "R".toByteArray()
66-
18 -> "S".toByteArray()
67-
19 -> "T".toByteArray()
68-
20 -> "U".toByteArray()
69-
21 -> "V".toByteArray()
70-
22 -> "W".toByteArray()
71-
23 -> "X".toByteArray()
72-
24 -> "Y".toByteArray()
73-
25 -> "Z".toByteArray()
74-
26 -> " ".toByteArray()
75-
else -> throw IllegalArgumentException("Unknown value $numericValue for CallLetterSpace")
76-
}
30+
override fun encode(numericValue: BigInteger): ByteArray {
31+
return (valueToChar[numericValue]
32+
?: throw IllegalArgumentException("Unknown value $numericValue for CallLetterSpace"))
33+
.toByteArray()
7734
}
7835
}

0 commit comments

Comments
 (0)