Skip to content

Instantly share code, notes, and snippets.

@hannut91
Created August 23, 2023 14:52
Show Gist options
  • Save hannut91/bef82e49343ff4f891bfd93b13231c4f to your computer and use it in GitHub Desktop.
Save hannut91/bef82e49343ff4f891bfd93b13231c4f to your computer and use it in GitHub Desktop.
디자인 패턴의 아름다움 스터디 코드
import java.net.InetAddress
import java.util.Random
fun random(): String {
val randomChars = CharArray(8)
var count = 0
val random = Random()
while (count < 8) {
val randomAscii = random.nextInt(122)
when (randomAscii) {
in 48..57 -> {
randomChars[count] =
('0'.code + (randomAscii - 48)).toChar()
count++
}
in 65..90 -> {
randomChars[count] =
('A'.code + (randomAscii - 65)).toChar()
count++
}
in 97..122 -> {
randomChars[count] =
('a'.code + (randomAscii - 97)).toChar()
count++
}
else -> {
continue
}
}::class
}
return randomChars.toString()
}
class GenerateEffect(
val hostName: String,
val timestamp: Long,
val seed: String,
)
var defaultEffect = GenerateEffect(
try {
InetAddress.getLocalHost().hostName.split(
Regex("\\.")
).last()
} catch (e: Throwable) {
random()
},
System.currentTimeMillis(),
random()
)
object IdGenerator {
fun generate(
effect: GenerateEffect = defaultEffect
): String {
return String.format(
"%s-%d-%s", effect.hostName,
effect.timestamp,
effect.seed
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment