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

net.jbock.convert.map.AutoOrEnumMapper Maven / Gradle / Ivy

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

import io.jbock.javapoet.CodeBlock;
import io.jbock.simple.Inject;
import io.jbock.util.Either;
import net.jbock.annotated.Item;
import net.jbock.common.TypeTool;
import net.jbock.common.Util;
import net.jbock.common.ValidationFailure;
import net.jbock.contrib.EnumConverter;
import net.jbock.convert.Mapping;
import net.jbock.convert.match.Match;
import net.jbock.util.StringConverter;

import javax.lang.model.element.ElementKind;
import javax.lang.model.type.DeclaredType;
import java.util.Optional;
import java.util.function.Supplier;

import static io.jbock.util.Either.left;

public final class AutoOrEnumMapper {

    private final AutoMappings autoMappings;

    @Inject
    public AutoOrEnumMapper(
            AutoMappings autoMappings) {
        this.autoMappings = autoMappings;
    }

    public 
    Either> findMapping(
            Match match) {
        return autoMappings.findAutoMapping(match)
                .or(() -> findEnumMapping(match))
                .>>map(Either::right)
                .orElseGet(() -> left(noConverterError(match)));
    }

    private 
    Optional> findEnumMapping(
            Match match) {
        return TypeTool.AS_DECLARED.visit(match.baseType())
                .map(DeclaredType::asElement)
                .flatMap(TypeTool.AS_TYPE_ELEMENT::visit)
                .filter(element -> element.getKind() == ElementKind.ENUM)
                .map(enumType -> {
                    CodeBlock mapper = CodeBlock.of("$1T.create($2T::valueOf, $2T::values)",
                            EnumConverter.class, enumType.asType());
                    return Mapping.create(mapper, match);
                });
    }

    private ValidationFailure noConverterError(Match match) {
        String expectedType = StringConverter.class.getSimpleName() +
                "<" + Util.typeToString(match.baseType()) + ">";
        return match.fail("define a converter class that extends " + expectedType +
                " or implements " +
                Supplier.class.getSimpleName() +
                "<" + expectedType + ">");
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy