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

org.sfm.map.impl.getter.time.JavaInstantFromObjectGetter Maven / Gradle / Ivy

Go to download

Java library to map flat record - ResultSet, csv - to java object with minimum configuration and low footprint.

The newest version!
package org.sfm.map.impl.getter.time;

import org.sfm.reflect.Getter;

import java.time.Instant;
import java.time.temporal.TemporalAccessor;
import java.util.Date;


public class JavaInstantFromObjectGetter implements Getter {

    private final Getter getter;

    public JavaInstantFromObjectGetter(Getter getter) {
        this.getter = getter;
    }

    @Override
    public Instant get(S target) throws Exception {
        Object o = getter.get(target);

        if (o == null) {
            return null;
        }

        if (o instanceof Date) {
            return Instant.ofEpochMilli(((Date) o).getTime());
        }

        if (o instanceof TemporalAccessor) {
            return Instant.from((TemporalAccessor) o);
        }

        if (o instanceof Long || o instanceof Integer) {
            return Instant.ofEpochMilli(((Number)o).longValue());
        }

        throw new IllegalArgumentException("Cannot convert " + o + " to Instant");
    }

    @Override
    public String toString() {
        return "JavaInstantFromObjectGetter{" +
                "getter=" + getter +
                '}';
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy