Skip to content

Commit

Permalink
Fix warning Classes that contain only a companion object should be re…
Browse files Browse the repository at this point in the history
…placed with a named object declaration
  • Loading branch information
yufengwangca committed Aug 15, 2023
1 parent cdcadfa commit b842cc4
Show file tree
Hide file tree
Showing 2 changed files with 168 additions and 174 deletions.
40 changes: 19 additions & 21 deletions src/controller/java/src/chip/onboardingpayload/Verhoeff.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,27 @@

package chip.onboardingpayload

class Verhoeff {
companion object {
fun dihedralMultiply(x: Int, y: Int, n: Int): Int {
val n2 = n * 2
var newX = x % n2
var newY = y % n2
if (newX < n) {
if (newY < n) return (newX + newY) % n
return ((newX + (newY - n)) % n) + n
}
if (newY < n) return ((n + (newX - n) - newY) % n) + n
return (n + (newX - n) - (newY - n)) % n
object Verhoeff {
fun dihedralMultiply(x: Int, y: Int, n: Int): Int {
val n2 = n * 2
var newX = x % n2
var newY = y % n2
if (newX < n) {
if (newY < n) return (newX + newY) % n
return ((newX + (newY - n)) % n) + n
}
if (newY < n) return ((n + (newX - n) - newY) % n) + n
return (n + (newX - n) - (newY - n)) % n
}

fun dihedralInvert(value: Int, n: Int): Int {
if (value > 0 && value < n) return n - value
return value
}
fun dihedralInvert(value: Int, n: Int): Int {
if (value > 0 && value < n) return n - value
return value
}

fun permute(value: Int, permTable: ByteArray, permTableLen: Int, iterCount: Int): Int {
var newValue = value % permTableLen
if (iterCount == 0) return newValue
return permute(permTable[newValue].toInt(), permTable, permTableLen, iterCount - 1)
}
fun permute(value: Int, permTable: ByteArray, permTableLen: Int, iterCount: Int): Int {
var newValue = value % permTableLen
if (iterCount == 0) return newValue
return permute(permTable[newValue].toInt(), permTable, permTableLen, iterCount - 1)
}
}
302 changes: 149 additions & 153 deletions src/controller/java/src/chip/onboardingpayload/Verhoeff10.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,171 +18,167 @@
package chip.onboardingpayload

/** Implements Verhoeff's check-digit algorithm for base-10 strings. */
class Verhoeff10 {
companion object {
const val Base = 10
const val PolygonSize = 5
object Verhoeff10 {
const val Base = 10
const val PolygonSize = 5

val sMultiplyTable =
intArrayOf(
0,
1,
2,
3,
4,
5,
6,
7,
8,
9,
1,
2,
3,
4,
0,
6,
7,
8,
9,
5,
2,
3,
4,
0,
1,
7,
8,
9,
5,
6,
3,
4,
0,
1,
2,
8,
9,
5,
6,
7,
4,
0,
1,
2,
3,
9,
5,
6,
7,
8,
5,
9,
8,
7,
6,
0,
4,
3,
2,
1,
6,
5,
9,
8,
7,
1,
0,
4,
3,
2,
7,
6,
5,
9,
8,
2,
1,
0,
4,
3,
8,
7,
6,
5,
9,
3,
2,
1,
0,
4,
9,
8,
7,
6,
5,
4,
3,
2,
1,
0
)
val sMultiplyTable =
intArrayOf(
0,
1,
2,
3,
4,
5,
6,
7,
8,
9,
1,
2,
3,
4,
0,
6,
7,
8,
9,
5,
2,
3,
4,
0,
1,
7,
8,
9,
5,
6,
3,
4,
0,
1,
2,
8,
9,
5,
6,
7,
4,
0,
1,
2,
3,
9,
5,
6,
7,
8,
5,
9,
8,
7,
6,
0,
4,
3,
2,
1,
6,
5,
9,
8,
7,
1,
0,
4,
3,
2,
7,
6,
5,
9,
8,
2,
1,
0,
4,
3,
8,
7,
6,
5,
9,
3,
2,
1,
0,
4,
9,
8,
7,
6,
5,
4,
3,
2,
1,
0
)

// Permutation table for the algorithm.
val sPermTable = byteArrayOf(1, 5, 7, 6, 2, 8, 3, 0, 9, 4)
// Permutation table for the algorithm.
val sPermTable = byteArrayOf(1, 5, 7, 6, 2, 8, 3, 0, 9, 4)

// Compute a check character for a given string.
fun computeCheckChar(str: String): Char {
return computeCheckChar(str, str.length)
}
// Compute a check character for a given string.
fun computeCheckChar(str: String): Char {
return computeCheckChar(str, str.length)
}

fun computeCheckChar(str: String, strLen: Int): Char {
var c = 0
for (i in 1..strLen) {
val ch = str[strLen - i]
val value = charToVal(ch)
val p = Verhoeff.permute(value, sPermTable, Base, i)
c = sMultiplyTable[c * Base + p]
}
c = Verhoeff.dihedralInvert(c, PolygonSize)
return valToChar(c)
fun computeCheckChar(str: String, strLen: Int): Char {
var c = 0
for (i in 1..strLen) {
val ch = str[strLen - i]
val value = charToVal(ch)
val p = Verhoeff.permute(value, sPermTable, Base, i)
c = sMultiplyTable[c * Base + p]
}
c = Verhoeff.dihedralInvert(c, PolygonSize)
return valToChar(c)
}

// Verify a check character against a given string.
fun validateCheckChar(checkChar: Char, str: String): Boolean {
return validateCheckChar(checkChar, str, str.length)
}
// Verify a check character against a given string.
fun validateCheckChar(checkChar: Char, str: String): Boolean {
return validateCheckChar(checkChar, str, str.length)
}

fun validateCheckChar(checkChar: Char, str: String, strLen: Int): Boolean {
return computeCheckChar(str, strLen) == checkChar
}
fun validateCheckChar(checkChar: Char, str: String, strLen: Int): Boolean {
return computeCheckChar(str, strLen) == checkChar
}

// Verify a check character at the end of a given string.
fun validateCheckChar(str: String): Boolean {
return validateCheckChar(str, str.length)
}
// Verify a check character at the end of a given string.
fun validateCheckChar(str: String): Boolean {
return validateCheckChar(str, str.length)
}

fun validateCheckChar(str: String, strLen: Int): Boolean {
if (strLen == 0) return false
return validateCheckChar(str[strLen - 1], str, strLen - 1)
}
fun validateCheckChar(str: String, strLen: Int): Boolean {
if (strLen == 0) return false
return validateCheckChar(str[strLen - 1], str, strLen - 1)
}

// Convert between a character and its corresponding value.
fun charToVal(ch: Char): Int {
if (ch in '0'..'9') {
return ch - '0'
} else {
throw IllegalArgumentException("Input character must be a digit")
}
// Convert between a character and its corresponding value.
fun charToVal(ch: Char): Int {
if (ch in '0'..'9') {
return ch - '0'
} else {
throw IllegalArgumentException("Input character must be a digit")
}
}

private fun valToChar(value: Int): Char {
if (value in 0..Base) {
return ('0'.code + value).toChar()
} else {
throw IllegalArgumentException("Input value must be a digit")
}
private fun valToChar(value: Int): Char {
if (value in 0..Base) {
return ('0'.code + value).toChar()
} else {
throw IllegalArgumentException("Input value must be a digit")
}
}

private constructor()
}

0 comments on commit b842cc4

Please sign in to comment.