Created
January 9, 2019 16:00
-
-
Save smitsgit/7039aba49f91d4421119544f46ddaf69 to your computer and use it in GitHub Desktop.
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
package generator | |
import java.io.File | |
fun canHandle(func: () -> Boolean): Boolean { | |
return func() | |
} | |
fun firstHandler(successor: Generator<String, String>? = null) = generate<String, String>{ | |
while (true) { | |
val data = yield ("") | |
if (canHandle { data.startsWith("2") }) { | |
println("[ FirstHandler ] : Handling the line $data") | |
}else { | |
successor?.next(data) | |
} | |
} | |
} | |
fun secondHandler(successor: Generator<String, String>? = null) = generate<String, String>{ | |
while (true) { | |
val data = yield ("") | |
if (canHandle { data.startsWith("7") }) { | |
println("[ SecondHandler ] : Handling the line $data") | |
}else { | |
successor?.next(data) | |
} | |
} | |
} | |
fun thirdHandler(successor: Generator<String, String>? = null) = generate<String, String> { | |
while (true) { | |
val data = yield ("") | |
if (canHandle { data.startsWith("8") }) { | |
println("[ ThirdHandler ] : Handling the line $data") | |
}else { | |
successor?.next(data) | |
} | |
} | |
} | |
fun startHandlers(lst: List<Generator<String, String>>){ | |
for (item in lst){ | |
item.next("") | |
} | |
} | |
fun main(args: Array<String>) { | |
val first = firstHandler() | |
val second = secondHandler(first) | |
val third = thirdHandler(second) | |
startHandlers(listOf(first, second, third)) | |
val file = File("/Users/smitaldesai/IdeaProjects/kotlintrials/src/main/kotlin/iter/access-log") | |
for (line in file.bufferedReader().lineSequence()) { | |
third.next(line) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment