com.mitteloupe.randomgenkt.fielddataprovider.PaddedFieldDataProvider.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of randomgenkt Show documentation
Show all versions of randomgenkt Show documentation
Initialize instances of any class with generated data.
The newest version!
package com.mitteloupe.randomgenkt.fielddataprovider
import com.mitteloupe.randomgenkt.FieldDataProvider
class PaddedFieldDataProvider(
private val minimumLength: Int,
private val paddingString: String,
private val fieldDataProvider: FieldDataProvider
) : FieldDataProvider() {
override fun invoke(instance: OUTPUT_TYPE?): String {
val generatedString = getGeneratedString(instance)
val charactersMissing = minimumLength - generatedString.length
val stringBuilder = getStringBuilderWithPadding(charactersMissing)
return stringBuilder
.append(generatedString)
.toString()
}
private fun getStringBuilderWithPadding(paddingLength: Int): StringBuilder {
val stringBuilder = StringBuilder()
while (paddingString.isNotEmpty() && stringBuilder.length < paddingLength) {
stringBuilder.append(paddingString)
}
if (isPaddingProvided(paddingLength) && isStringLongerThanPadding(
stringBuilder,
paddingLength
)
) {
stringBuilder.delete(paddingLength, stringBuilder.length)
}
return stringBuilder
}
private fun isStringLongerThanPadding(stringBuilder: StringBuilder, paddingLength: Int) =
stringBuilder.length > paddingLength
private fun isPaddingProvided(paddingLength: Int) = paddingLength > 0
private fun getGeneratedString(instance: OUTPUT_TYPE?) = fieldDataProvider(instance).toString()
}