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

io.dropwizard.jdbi.args.OptionalOffsetDateTimeArgumentFactory Maven / Gradle / Ivy

The newest version!
package io.dropwizard.jdbi.args;

import org.skife.jdbi.v2.StatementContext;
import org.skife.jdbi.v2.tweak.Argument;
import org.skife.jdbi.v2.tweak.ArgumentFactory;

import java.time.OffsetDateTime;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Optional;
import java.util.TimeZone;

/**
 * An {@link ArgumentFactory} for {@link OffsetDateTime} arguments wrapped by {@link Optional}.
 */
public class OptionalOffsetDateTimeArgumentFactory implements ArgumentFactory> {

    private final Optional calendar;

    public OptionalOffsetDateTimeArgumentFactory() {
        calendar = Optional.empty();
    }

    public OptionalOffsetDateTimeArgumentFactory(Optional timeZone) {
        calendar = timeZone.map(GregorianCalendar::new);
    }

    @Override
    public boolean accepts(Class expectedType, Object value, StatementContext ctx) {
        if (value instanceof Optional) {
            final Optional optionalValue = (Optional) value;
            // Fall through to OptionalArgumentFactory if absent.
            // Fall through to OptionalArgumentFactory if present, but not OffsetDateTime.
            return optionalValue.isPresent() && optionalValue.get() instanceof OffsetDateTime;
        }
        return false;
    }

    @Override
    public Argument build(Class expectedType, Optional value, StatementContext ctx) {
        // accepts guarantees that the value is present
        return new OffsetDateTimeArgument(value.orElse(null), calendar);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy