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

org.dizitart.no2.rocksdb.formatter.DefaultTimeKeySerializers Maven / Gradle / Ivy

The newest version!
package org.dizitart.no2.rocksdb.formatter;

import com.esotericsoftware.kryo.kryo5.Kryo;
import com.esotericsoftware.kryo.kryo5.io.Output;
import org.dizitart.no2.exceptions.NitriteIOException;

import java.sql.Time;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;

/**
 * @since 4.0
 * @author Anindya Chatterjee
 */
class DefaultTimeKeySerializers {
    private static final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.ENGLISH);

    private static class DateSerializer extends ComparableKeySerializer {

        @Override
        public void writeKeyInternal(Kryo kryo, Output output, Date object) {
            output.writeString(format.format(object));
        }

        @Override
        public Date readKeyInternal(Kryo kryo, String value, Class type) {
            try {
                return format.parse(value);
            } catch (Exception e) {
                throw new NitriteIOException("Failed to read java.util.Date", e);
            }
        }
    }

    private static class TimestampSerializer extends ComparableKeySerializer {

        @Override
        public void writeKeyInternal(Kryo kryo, Output output, Timestamp object) {
            output.writeString(format.format(object));
        }

        @Override
        public Timestamp readKeyInternal(Kryo kryo, String value, Class type) {
            try {
                return new Timestamp(format.parse(value).getTime());
            } catch (Exception e) {
                throw new NitriteIOException("Failed to read java.sql.Timestamp", e);
            }
        }
    }

    private static class SqlDateSerializer extends ComparableKeySerializer {

        @Override
        public void writeKeyInternal(Kryo kryo, Output output, java.sql.Date object) {
            output.writeString(format.format(object));
        }

        @Override
        public java.sql.Date readKeyInternal(Kryo kryo, String value, Class type) {
            try {
                return new java.sql.Date(format.parse(value).getTime());
            } catch (Exception e) {
                throw new NitriteIOException("Failed to read java.sql.Date", e);
            }
        }
    }

    private static class TimeSerializer extends ComparableKeySerializer




© 2015 - 2024 Weber Informatics LLC | Privacy Policy