net.jbock.writing.OptionNamesMethod Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jbock-compiler Show documentation
Show all versions of jbock-compiler Show documentation
jbock annotation processor
package net.jbock.writing;
import io.jbock.javapoet.CodeBlock;
import io.jbock.javapoet.MethodSpec;
import io.jbock.javapoet.ParameterSpec;
import io.jbock.simple.Inject;
import net.jbock.annotated.Option;
import net.jbock.convert.Mapping;
import java.util.HashMap;
import java.util.List;
import java.util.function.Supplier;
import static javax.lang.model.element.Modifier.PRIVATE;
import static javax.lang.model.element.Modifier.STATIC;
import static net.jbock.common.Suppliers.memoize;
final class OptionNamesMethod extends HasCommandRepresentation {
@Inject
OptionNamesMethod(
CommandRepresentation commandRepresentation) {
super(commandRepresentation);
}
private final Supplier define = memoize(() -> {
ParameterSpec result = ParameterSpec.builder(
optionNames().type, "result").build();
long mapSize = namedOptions().stream()
.map(Mapping::item)
.map(Option::names)
.map(List::size)
.mapToLong(i -> i)
.sum();
CodeBlock.Builder code = CodeBlock.builder();
code.addStatement("$T $N = new $T<>($L)", result.type, result, HashMap.class, mapSize);
for (Mapping