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

org.wildfly.clustering.marshalling.protostream.time.ZoneOffsetMarshaller Maven / Gradle / Ivy

/*
 * Copyright The WildFly Authors
 * SPDX-License-Identifier: Apache-2.0
 */

package org.wildfly.clustering.marshalling.protostream.time;

import java.io.IOException;
import java.time.ZoneOffset;

import org.infinispan.protostream.descriptors.WireType;
import org.wildfly.clustering.marshalling.protostream.FieldSetMarshaller;
import org.wildfly.clustering.marshalling.protostream.ProtoStreamReader;
import org.wildfly.clustering.marshalling.protostream.ProtoStreamWriter;

/**
 * Marshalling for {@link ZoneOffset} instances using the following strategy:
 * 
    *
  1. Marshal {@link ZoneOffset#UTC} as zero bytes
  2. *
  3. If offset is of form ±HH, marshal as signed integer of hours
  4. *
  5. If offset is of form ±HH:MM, marshal as signed integer of total minutes
  6. *
  7. If offset is of form ±HH:MM:SS, marshal as signed integer of total seconds
  8. *
* @author Paul Ferraro */ public enum ZoneOffsetMarshaller implements FieldSetMarshaller.Simple { INSTANCE; private static final int HOURS_INDEX = 0; private static final int MINUTES_INDEX = 1; private static final int SECONDS_INDEX = 2; private static final int FIELDS = 3; @Override public ZoneOffset createInitialValue() { return ZoneOffset.UTC; } @Override public int getFields() { return FIELDS; } @Override public ZoneOffset readFrom(ProtoStreamReader reader, int index, WireType type, ZoneOffset offset) throws IOException { switch (index) { case HOURS_INDEX: return ZoneOffset.ofHours(reader.readSInt32()); case MINUTES_INDEX: return ZoneOffset.ofTotalSeconds(reader.readSInt32() * 60); case SECONDS_INDEX: return ZoneOffset.ofTotalSeconds(reader.readSInt32()); default: reader.skipField(type); return offset; } } @Override public void writeTo(ProtoStreamWriter writer, ZoneOffset offset) throws IOException { int seconds = offset.getTotalSeconds(); if (seconds != 0) { if (seconds % 60 == 0) { int minutes = seconds / 60; if (minutes % 60 == 0) { int hours = minutes / 60; // Typical offsets writer.writeSInt32(HOURS_INDEX, hours); } else { // Uncommon fractional hour offsets writer.writeSInt32(MINUTES_INDEX, minutes); } } else { // Synthetic offsets writer.writeSInt32(SECONDS_INDEX, seconds); } } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy