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

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

There is a newer version: 5.18
Show newest version
package net.jbock.writing;

import io.jbock.javapoet.ClassName;
import io.jbock.javapoet.FieldSpec;
import net.jbock.annotated.Option;
import net.jbock.annotated.Parameter;
import net.jbock.annotated.VarargsParameter;
import net.jbock.convert.Mapping;
import net.jbock.processor.SourceElement;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.Supplier;

import static javax.lang.model.element.Modifier.FINAL;
import static javax.lang.model.element.Modifier.PRIVATE;
import static net.jbock.common.Constants.STRING;
import static net.jbock.common.Constants.mapOf;
import static net.jbock.common.Suppliers.memoize;

public final class CommandRepresentation {

    private final Supplier optionNames = memoize(() -> FieldSpec.builder(
                    mapOf(STRING, optType()), "optionNames")
            .addModifiers(PRIVATE, FINAL).build());

    private final Supplier optType = memoize(() -> namedOptions().isEmpty() ?
            ClassName.get(Void.class) : // javapoet #739
            sourceElement().optionEnumType());

    private final Supplier>> allMappings = memoize(() -> {
        List> result = new ArrayList<>();
        result.addAll(namedOptions());
        result.addAll(positionalParameters());
        varargsParameter().ifPresent(result::add);
        return result;
    });

    private final List> namedOptions;
    private final List> positionalParameters;
    private final Optional> varargsParameter;

    private final SourceElement sourceElement;

    public CommandRepresentation(
            SourceElement sourceElement,
            List> namedOptions,
            List> positionalParameters,
            Optional> varargsParameter) {
        this.namedOptions = namedOptions;
        this.positionalParameters = positionalParameters;
        this.varargsParameter = varargsParameter;
        this.sourceElement = sourceElement;
    }

    SourceElement sourceElement() {
        return sourceElement;
    }

    Optional> varargsParameter() {
        return varargsParameter;
    }

    List> positionalParameters() {
        return positionalParameters;
    }

    List> namedOptions() {
        return namedOptions;
    }

    FieldSpec optionNames() {
        return optionNames.get();
    }

    /** Returns the type of the option enum. */
    ClassName optType() {
        return optType.get();
    }

    List> allMappings() {
        return allMappings.get();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy