Created
August 23, 2023 14:52
-
-
Save hannut91/bef82e49343ff4f891bfd93b13231c4f to your computer and use it in GitHub Desktop.
디자인 패턴의 아름다움 스터디 코드
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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