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

net.jbock.convert.Mapping Maven / Gradle / Ivy

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

import com.squareup.javapoet.CodeBlock;
import com.squareup.javapoet.FieldSpec;
import com.squareup.javapoet.ParameterSpec;
import com.squareup.javapoet.TypeName;
import net.jbock.annotated.AnnotatedMethod;
import net.jbock.common.EnumName;
import net.jbock.convert.matching.ValidMatch;
import net.jbock.model.Multiplicity;

import javax.lang.model.type.TypeMirror;
import java.util.Optional;

import static net.jbock.model.Multiplicity.OPTIONAL;

/**
 * An annotated method with additional information about type conversion.
 *
 * @param  one of three types of annotated methods:
 *           named option, positional parameter, or repeatable positional parameter
 */
public final class Mapping {

    private final CodeBlock mapExpr;
    private final ValidMatch match;
    private final boolean multiline;
    private final boolean modeFlag;
    private final ParameterSpec asParameterSpec;
    private final FieldSpec asFieldSpec;

    private Mapping(
            CodeBlock mapExpr,
            ValidMatch match,
            boolean multiline,
            boolean modeFlag,
            ParameterSpec asParameterSpec,
            FieldSpec asFieldSpec) {
        this.asParameterSpec = asParameterSpec;
        this.match = match;
        this.multiline = multiline;
        this.modeFlag = modeFlag;
        this.mapExpr = mapExpr;
        this.asFieldSpec = asFieldSpec;
    }

    public static 
    Mapping create(
            CodeBlock code,
            ValidMatch match,
            boolean multiline) {
        return Mapping.create(code, match, multiline, false);
    }

    public static 
    Mapping createFlag(
            CodeBlock code,
            ValidMatch match) {
        return Mapping.create(code, match, false, true);
    }

    private static 
    Mapping create(
            CodeBlock mapExpr,
            ValidMatch match,
            boolean multiline,
            boolean modeFlag) {
        TypeName fieldType = TypeName.get(match.sourceMethod().returnType());
        String fieldName = match.sourceMethod().enumName().original();
        FieldSpec asFieldSpec = FieldSpec.builder(fieldType, fieldName).build();
        ParameterSpec asParameterSpec = ParameterSpec.builder(fieldType, fieldName).build();
        return new Mapping<>(mapExpr, match, multiline, modeFlag, asParameterSpec, asFieldSpec);
    }

    public Optional simpleMapExpr() {
        if (multiline) {
            return Optional.empty();
        }
        return Optional.of(mapExpr);
    }

    public Optional extractExpr() {
        return match.extractExpr();
    }

    public Multiplicity multiplicity() {
        return match.multiplicity();
    }

    public boolean multiline() {
        return multiline;
    }

    public TypeMirror baseType() {
        return match.baseType();
    }

    public CodeBlock mapExpr() {
        return mapExpr;
    }

    public EnumName enumName() {
        return sourceMethod().enumName();
    }

    public boolean isRequired() {
        return multiplicity() == Multiplicity.REQUIRED;
    }

    public boolean isRepeatable() {
        return multiplicity() == Multiplicity.REPEATABLE;
    }

    public boolean isOptional() {
        return multiplicity() == OPTIONAL;
    }

    public boolean modeFlag() {
        return modeFlag;
    }

    public M sourceMethod() {
        return match.sourceMethod();
    }

    public String paramLabel() {
        return sourceMethod().paramLabel();
    }

    public FieldSpec asField() {
        return asFieldSpec;
    }

    public ParameterSpec asParam() {
        return asParameterSpec;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy