purereborn
09/22/2017, 6:25 PMreturn lines.collect(CustomCollector)
.flatMap { x -> process(x)}....
It uses a CustomCollector
object CustomCollector : Collector<String, MutableList<MutableList<String>>, MutableList<MutableList<String>>> {
override fun accumulator(): BiConsumer<MutableList<MutableList<String>>, String> {
return CustomBiConsumer() // main logic here, 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
}
override fun combiner(): BinaryOperator<MutableList<MutableList<String>>> {
return CustomBinaryOperator() // combines 2 Mutable list of lists
}
override fun characteristics(): MutableSet<Collector.Characteristics> {
return Collections.singleton(Collector.Characteristics.IDENTITY_FINISH) //the accumulator object is the same as the result object
}
override fun supplier(): Supplier<MutableList<MutableList<String>>> {
return ArrayListSupplier() //creates a new Muttable list of strings
}
override fun finisher(): Function<MutableList<MutableList<String>>, MutableList<MutableList<String>>> {
return Function.identity() // noop
}
}
class CustomBinaryOperator : BinaryOperator<MutableList<MutableList<String>>> {
override fun apply(t: MutableList<MutableList<String>>,
u: MutableList<MutableList<String>>): MutableList<MutableList<String>> {
t.addAll(u)
return t
}
}
class CustomBiConsumer : BiConsumer<MutableList<MutableList<String>>, String> {
override fun accept(stringGroups: MutableList<MutableList<String>>, line: String) {
if (line.isEmpty()) return
if (line.matches(MacParser.NAME_REGEX)) {
stringGroups.add(ArrayList())
}
stringGroups.last().add(line)
}
}
class ArrayListSupplier : Supplier<MutableList<MutableList<String>>> {
override fun get(): ArrayList<MutableList<String>> {
return ArrayList()
}
}
ilya.gorbunov
09/22/2017, 7:50 PMobject CustomCollector : Collector<String, MutableList<MutableList<String>>, MutableList<MutableList<String>>> {
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.matches(MacParser.NAME_REGEX)) {
stringGroups.add(ArrayList())
}
stringGroups.last().add(line)
}
override fun combiner() = BinaryOperator<MutableList<MutableList<String>>> { t, u -> t.apply { addAll(u) }}
//the accumulator object is the same as the result object
override fun characteristics() = setOf(Collector.Characteristics.IDENTITY_FINISH)
override fun supplier() = Supplier<MutableList<MutableList<String>>> { ArrayList() }
override fun finisher() = Function.identity<MutableList<MutableList<String>>>() // noop
}
purereborn
09/22/2017, 7:58 PMreturn@BiConsume
syntax. what's that called?ilya.gorbunov
09/22/2017, 8:04 PM