de.javakaffee.kryoserializers.jodatime.JodaLocalTimeSerializer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kryo-serializers Show documentation
Show all versions of kryo-serializers Show documentation
Additional kryo (http://kryo.googlecode.com) serializers for standard jdk types (e.g. currency, jdk proxies) and some for external libs (e.g. joda time, cglib proxies, wicket).
The newest version!
package de.javakaffee.kryoserializers.jodatime;
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.Serializer;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
import org.joda.time.Chronology;
import org.joda.time.DateTimeZone;
import org.joda.time.LocalTime;
import org.joda.time.chrono.*;
/**
* A format for Joda {@link LocalTime}, that stores the milliseconds of the day and chronology
* as separate attributes.
*
* The following chronologies are supported:
*
* - {@link ISOChronology}
* - {@link CopticChronology}
* - {@link EthiopicChronology}
* - {@link GregorianChronology}
* - {@link JulianChronology}
* - {@link IslamicChronology}
* - {@link BuddhistChronology}
* - {@link GJChronology}
*
*
*
* @author Rob Reeves
*/
public class JodaLocalTimeSerializer extends Serializer {
@Override
public void write(Kryo kryo, Output output, LocalTime object) {
final int time = object.getMillisOfDay();
output.writeInt(time, true);
//LocalTime always converts the internal DateTimeZone to UTC so there is no need to serialize it.
final String chronologyId = IdentifiableChronology.getChronologyId(object.getChronology());
output.writeString(chronologyId);
}
@Override
public LocalTime read(Kryo kryo, Input input, Class extends LocalTime> type) {
final int time = input.readInt(true);
final Chronology chronology = IdentifiableChronology.readChronology(input);
//LocalTime always converts the internal DateTimeZone to UTC.
return new LocalTime(time, chronology.withZone(DateTimeZone.UTC));
}
@Override
public LocalTime copy(Kryo kryo, LocalTime original) {
return new LocalTime(original);
}
}