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

net.morimekta.providence.jdbi.v3.EnumValueArgumentFactory Maven / Gradle / Ivy

There is a newer version: 2.7.0
Show newest version
package net.morimekta.providence.jdbi.v3;

import net.morimekta.providence.PEnumValue;
import org.jdbi.v3.core.argument.Argument;
import org.jdbi.v3.core.argument.ArgumentFactory;
import org.jdbi.v3.core.argument.NullArgument;
import org.jdbi.v3.core.config.ConfigRegistry;

import java.lang.reflect.Type;
import java.sql.Types;
import java.util.Optional;

public class EnumValueArgumentFactory implements ArgumentFactory {
    private final Class enumClass;

    @SuppressWarnings("unused")
    public EnumValueArgumentFactory() {
        this(PEnumValue.class);
    }

    public EnumValueArgumentFactory(Class enumClass) {
        this.enumClass = enumClass;
    }

    @Override
    public Optional build(Type type, Object value, ConfigRegistry config) {
        if (type instanceof Class) {
            Class klass = (Class) type;
            if (enumClass.isAssignableFrom(klass)) {
                if (value == null) {
                    return Optional.of(new NullArgument(Types.INTEGER));
                }
                return Optional.of(new EnumValueArgument((PEnumValue) value));
            }
        }
        return Optional.empty();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy