https://kotlinlang.org logo
#announcements
Title
# announcements
p

purereborn

09/22/2017, 6:25 PM
@kevinmost @anthonycr here's what I came up with: lines being lines of text:
Copy code
return lines.collect(CustomCollector)
                .flatMap { x -> process(x)}....
It uses a CustomCollector
Copy code
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()
    }
}
1
i

ilya.gorbunov

09/22/2017, 7:50 PM
Can be shortened a bit with the help of standard library functions and SAM-conversions:
Copy code
object 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
}
❤️ 1
p

purereborn

09/22/2017, 7:58 PM
unfamiliar with the
return@BiConsume
syntax. what's that called?
i

ilya.gorbunov

09/22/2017, 8:04 PM