-
-
Save wyaeld/27eac0628c67528ac88d1b4437628e40 to your computer and use it in GitHub Desktop.
Compression enum example
This file contains 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
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