tipsy
08/10/2019, 8:44 PMvar initialized = false
if (!initialized) {
...
initialized = true
}
Leon K
08/10/2019, 8:52 PMtipsy
08/10/2019, 8:54 PMtipsy
08/10/2019, 8:55 PMkarelpeeters
08/10/2019, 8:57 PMlazy
delegate in your case?tipsy
08/10/2019, 9:00 PMvar initialized = false
override fun write(b: ByteArray, off: Int, len: Int) {
if (!initialized) {
...
initialized = true
}
when {
...
}
}
Leon K
08/10/2019, 9:02 PMfun initIfNecessary(isIinit: Boolean, initialize: () -> Unit): Boolean {
if (!isIinit) {
initialize()
}
return true
}
but that doesn't really help anything...tipsy
08/10/2019, 9:02 PMtipsy
08/10/2019, 9:02 PMtipsy
08/10/2019, 9:03 PMLeon K
08/10/2019, 9:04 PMLeon K
08/10/2019, 9:05 PMval toInitialize by lazy {
/**Initialization*/
10
}
fun doSomething() {
println(toInitialize)
}
karelpeeters
08/10/2019, 9:05 PMclass Foo(outputStream: OutputStream) {
val output by lazy {
outputStream.write(5)
outputStream
}
fun write(x: Int) {
output.write(x)
}
}
tipsy
08/10/2019, 9:07 PMLeon K
08/10/2019, 9:08 PMtipsy
08/10/2019, 9:08 PMLeon K
08/10/2019, 9:09 PMkarelpeeters
08/10/2019, 9:09 PMtipsy
08/10/2019, 9:09 PMkarelpeeters
08/10/2019, 9:09 PMtipsy
08/10/2019, 9:09 PMtipsy
08/10/2019, 9:10 PMoverride fun write(b: ByteArray, off: Int, len: Int) {
if (!initialized) { // set available compressors, content encoding, and compressing-stream
val isCompressible = len >= minSizeForCompression && !excludedMimeType(res.contentType) && res.getHeader(CONTENT_ENCODING).isNullOrEmpty()
if (isCompressible && rwc.acceptsBrotli && rwc.compStrat.brotli != null) {
res.setHeader(CONTENT_ENCODING, BR)
compressingStream = LeveledBrotliStream(res.outputStream, rwc.compStrat.brotli.level)
brotliEnabled = true
} else if (isCompressible && rwc.acceptsGzip && rwc.compStrat.gzip != null) {
res.setHeader(CONTENT_ENCODING, GZIP)
compressingStream = LeveledGzipStream(res.outputStream, rwc.compStrat.gzip.level)
gzipEnabled = true
}
initialized = true
}
when {
brotliEnabled -> (compressingStream as LeveledBrotliStream).write(b, off, len)
gzipEnabled -> (compressingStream as LeveledGzipStream).write(b, off, len)
else -> super.write(b, off, len) // no compression
}
}
Leon K
08/10/2019, 9:10 PMvar foo = 0
val toInitialize by lazy {
/**Initialization*/
foo
}
fun doSomething(n: Int) {
foo = n
println(toInitialize)
}
tipsy
08/10/2019, 9:10 PMkarelpeeters
08/10/2019, 9:11 PMtipsy
08/10/2019, 9:20 PMtipsy
08/10/2019, 9:20 PMkarelpeeters
08/10/2019, 9:21 PMtipsy
08/10/2019, 9:22 PMtipsy
08/10/2019, 9:22 PMkarelpeeters
08/10/2019, 9:23 PMtipsy
08/10/2019, 9:23 PMtipsy
08/10/2019, 9:23 PMtipsy
08/10/2019, 9:27 PM