Created
January 22, 2018 05:28
-
-
Save PureSin/95b317f0fbde0fc0ce712b1730ed9386 to your computer and use it in GitHub Desktop.
Custom Colelctor
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
import java.io.BufferedReader | |
import java.io.File | |
import java.io.InputStream | |
import java.io.InputStreamReader | |
import java.util.* | |
import java.util.function.Function | |
import java.util.function.BiConsumer | |
import java.util.function.BinaryOperator | |
import java.util.function.Supplier | |
import java.util.stream.Collector | |
data class Foo(val output: String) | |
fun main(args: Array<String>) { | |
println(parseFile(File("src/input.txt").inputStream())) | |
println(parseFilesFunc(File("src/input.txt").inputStream())) | |
} | |
fun parseFile(input: InputStream): List<Foo> { | |
val result = ArrayList<Foo>() | |
// convert inputstream to a list of lines | |
val lines = BufferedReader(InputStreamReader(input)).readLines() | |
var stringBuilder = StringJoiner(" ") | |
for (line in lines) { // iterate through the list | |
if (createNewObject(line)) { | |
if (stringBuilder.length() > 0) { | |
result.add(Foo(stringBuilder.toString())) | |
} | |
stringBuilder = StringJoiner(" ") | |
} else { | |
stringBuilder.add(line) | |
} | |
} | |
if (stringBuilder.length() > 0) { | |
result.add(Foo(stringBuilder.toString())) | |
} | |
return result | |
} | |
fun createNewObject(line: String): Boolean { | |
for (c in line) { | |
if (c != '=') { | |
return false | |
} | |
} | |
return true | |
} | |
fun parseFilesFunc(input: InputStream): List<Foo> { | |
val lines = BufferedReader(InputStreamReader(input)).lines() | |
return lines.collect(FooCollector) | |
} | |
object FooCollector : Collector<String, MutableList<MutableList<String>>, List<Foo>> { | |
override fun accumulator() = BiConsumer<MutableList<MutableList<String>>, String> { stringGroups, line -> | |
// looks for a specific line, creates a new MutableList<String> and append it to the List of Lists, otherwise add non-empty lines to the last List of strings | |
if (line.isEmpty()) return@BiConsumer | |
if (line.all { it == '=' }) { | |
stringGroups.add(ArrayList()) | |
} else { | |
stringGroups.last().add(line) | |
} | |
} | |
override fun combiner() = BinaryOperator<MutableList<MutableList<String>>> { t, u -> | |
t.apply { | |
addAll(u) | |
} | |
} | |
override fun characteristics() = setOf(Collector.Characteristics.CONCURRENT) | |
override fun supplier() = Supplier<MutableList<MutableList<String>>> { ArrayList() } | |
override fun finisher(): Function<MutableList<MutableList<String>>, List<Foo>> { | |
return Function { input -> input.map { parsedLines -> Foo(parsedLines.joinToString(" ")) } } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment