Skip to content

Instantly share code, notes, and snippets.

@wyaeld
Created March 28, 2021 23:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wyaeld/27eac0628c67528ac88d1b4437628e40 to your computer and use it in GitHub Desktop.
Save wyaeld/27eac0628c67528ac88d1b4437628e40 to your computer and use it in GitHub Desktop.
Compression enum example
enum class Compression(val id: String) {
Off("off"),
Zlib("zlib");
companion object {
fun fromString(id: String) = Compression::string.find(id)
?: throw IllegalArgumentException("Bad compression method: '$id'")
}
}
enum class Environment { DEV, TEST, PROD }
class CompressionPreferences(env : Environment) {
val defaultAlgorithm : when(env) {
DEV, TEST -> Compression.Off
PROD -> Compression.Zlib
}
}
class CompressionService(val prefs: CompressionPreferences) {
fun compress(input : String, compressionAlgorithm : Compression = prefs.defaultAlgorithm) : String {
return when(compressionAlgorithm) {
Compression.Off -> input;
Compression.Zlib -> {
try {
myZlibLibrary.compress(input)
} catch (ex : Exception) {
log.error("Failure compressing due to", ex)
return ""
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment