All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.jbock.writing.OptionNamesMethod Maven / Gradle / Ivy

There is a newer version: 5.18
Show newest version
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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy