Skip to content

Commit

Permalink
feat(protected-string): add more functionalities for conversion and c…
Browse files Browse the repository at this point in the history
…oncatenation
  • Loading branch information
vfmunhoz committed Jul 4, 2021
1 parent 45b2362 commit 79af8bc
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 12 deletions.
1 change: 1 addition & 0 deletions .sv4git.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@ commit-message:
"protected-long",
"protected-float",
"protected-double",
"protected-string",
"tests"
]
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,39 @@ package com.vfmunhoz.protectedtypes.types

import com.vfmunhoz.protectedtypes.extensions.replaceFrom

@JvmInline
value class ProtectedString(val value: String) : Comparable<ProtectedString> {
class ProtectedString(
val value: String,
private val splitToken: String? = null,
private val replaceToken: String = "*"
) : Comparable<ProtectedString> {

companion object {
private const val SPLIT_TOKEN = " "
private const val REPLACE_TOKEN = "*"
}
operator fun plus(other: String): ProtectedString = ProtectedString(value + other)

operator fun plus(other: ProtectedString): ProtectedString = plus(other.value)

override fun toString(): String {
val builder = StringBuilder()
override fun toString(): String =
if(splitToken.isNullOrEmpty()) { maskValue(value, value.length / 2, value.length - 1) }
else { maskWithSplit() }

value.split(SPLIT_TOKEN).forEach { slice ->
builder.append(SPLIT_TOKEN)
builder.append(slice.replaceFrom(slice.length / 2, slice.length - 1, REPLACE_TOKEN))
private fun maskWithSplit() = StringBuilder().let { builder ->
value.split(splitToken!!).forEach { slice ->
builder.append(splitToken)
builder.append(maskValue(slice, slice.length / 2, slice.length - 1))
}

return builder.toString().trim()
builder.toString().trim()
}

private fun maskValue(valueToMask: String, start: Int, end: Int): String = valueToMask.replaceFrom(start, end, replaceToken)

override fun equals(other: Any?): Boolean = value == when(other) {
is ProtectedString -> other.value
else -> other
}

override fun hashCode(): Int = value.hashCode()

override fun compareTo(other: ProtectedString): Int = value.compareTo(other.value)
}

fun String.toProtected() = ProtectedString(this)
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.vfmunhoz.protectedtypes.types

import org.junit.jupiter.api.assertAll
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertIs
import kotlin.test.assertTrue

internal class ProtectedStringTest {

@Test
fun `convert and compares strings and protected strings successfully`() {
val testString = "Hello ProtectedTypes!"
val protectedTestString = testString.toProtected()

assertIs<ProtectedString>(protectedTestString)

assertFalse(testString.equals(protectedTestString), "String.equals is impossible to overload for Protected parameter")

assertAll("ProtectedString.equals",
{ assertTrue(protectedTestString.equals(testString)) },
{ assertEquals(protectedTestString, testString.toProtected()) }
)
}

@Test
fun `concat two strings with the plus operator`() {
val beginString = "Hello"
val endString = "ProtectedTypes!"

val beginProtectedString = beginString.toProtected()
val endProtectedString = endString.toProtected()

val nativeStringConcat = beginString + endString
val protectedStringConcat = beginProtectedString + endProtectedString

assertEquals(nativeStringConcat, protectedStringConcat.value)
}
}

0 comments on commit 79af8bc

Please sign in to comment.