
package br.com.objectos.way.code;

import com.squareup.javapoet.CodeBlock;
import java.util.function.BiConsumer;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collector;

/**
 * @author marcio.endo@objectos.com.br (Marcio Endo)
 */
public class CodeCollectors {

  private CodeCollectors() {
  }

  public static Collector<CodeBlock, ?, CodeBlock> toCodeBlock() {
    Supplier<CodeBlock.Builder> supplier = CodeBlock::builder;
    BiConsumer<CodeBlock.Builder, CodeBlock> accumulator = (builder, e) -> builder.add(e);
    BinaryOperator<CodeBlock.Builder> combiner = (left, right) -> left.add(right.build());
    Function<CodeBlock.Builder, CodeBlock> finisher = (builder) -> builder.build();
    return Collector.of(supplier, accumulator, combiner, finisher);
  }

}