package org.apache.cassandra.serializers;
import java.io.IOException;
import java.nio.ByteBuffer;
import org.apache.cassandra.cql3.Duration;
import org.apache.cassandra.io.util.DataInputBuffer;
import org.apache.cassandra.io.util.DataOutputBufferFixed;
import org.apache.cassandra.utils.ByteBufferUtil;
import org.apache.cassandra.utils.vint.VIntCoding;
public final class DurationSerializer implements TypeSerializer<Duration>
{
public static final DurationSerializer instance = new DurationSerializer();
public ByteBuffer serialize(Duration duration)
{
if (duration == null)
return ByteBufferUtil.EMPTY_BYTE_BUFFER;
long months = duration.getMonths();
long days = duration.getDays();
long nanoseconds = duration.getNanoseconds();
int size = VIntCoding.computeVIntSize(months)
+ VIntCoding.computeVIntSize(days)
+ VIntCoding.computeVIntSize(nanoseconds);
try (DataOutputBufferFixed output = new DataOutputBufferFixed(size))
{
output.writeVInt(months);
output.writeVInt(days);
output.writeVInt(nanoseconds);
return output.buffer();
}
catch (IOException e)
{
throw new AssertionError("Unexpected error", e);
}
}
public Duration deserialize(ByteBuffer bytes)
{
if (bytes.remaining() == 0)
return null;
try (DataInputBuffer in = new DataInputBuffer(bytes, true))
{
int months = (int) in.readVInt();
int days = (int) in.readVInt();
long nanoseconds = in.readVInt();
return Duration.newInstance(months, days, nanoseconds);
}
catch (IOException e)
{
throw new AssertionError("Unexpected error", e);
}
}
public void validate(ByteBuffer bytes) throws MarshalException
{
if (bytes.remaining() < 3)
throw new MarshalException(String.format("Expected at least 3 bytes for a duration (%d)", bytes.remaining()));
try (DataInputBuffer in = new DataInputBuffer(bytes, true))
{
long monthsAsLong = in.readVInt();
long daysAsLong = in.readVInt();
long nanoseconds = in.readVInt();
if (!canBeCastToInt(monthsAsLong))
throw new MarshalException(String.format("The duration months must be a 32 bits integer but was: %d",
monthsAsLong));
if (!canBeCastToInt(daysAsLong))
throw new MarshalException(String.format("The duration days must be a 32 bits integer but was: %d",
daysAsLong));
int months = (int) monthsAsLong;
int days = (int) daysAsLong;
if (!((months >= 0 && days >= 0 && nanoseconds >= 0) || (months <= 0 && days <=0 && nanoseconds <=0)))
throw new MarshalException(String.format("The duration months, days and nanoseconds must be all of the same sign (%d, %d, %d)",
months, days, nanoseconds));
}
catch (IOException e)
{
throw new AssertionError("Unexpected error", e);
}
}
private boolean canBeCastToInt(long l)
{
return ((int) l) == l;
}
public String toString(Duration duration)
{
return duration == null ? "" : duration.toString();
}
public Class<Duration> getType()
{
return Duration.class;
}
}