class DeltaLongTimeSeriesSerialiser implements ToBytesSerialiser<LongTimeSeries> {

    @Override
    public byte[] serialise(final LongTimeSeries timeSeries) throws SerialisationException {
        if (null == timeSeries) {
            return EMPTY_BYTES;
        }
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        final DataOutputStream dos = new DataOutputStream(baos);
        final Mode mode = calculateMode(timeSeries);
        final SortedMap<Instant, Long> timeseriesMap = timeSeries.getTimeSeries();
        try {
            dos.write(CompactRawSerialisationUtils.writeLong(timeSeries.getTimeBucket().ordinal()));
            dos.write(CompactRawSerialisationUtils.writeLong(timeseriesMap.size()));
            boolean deltaMode = mode == Mode.DELTA;
            dos.writeBoolean(deltaMode);
            if (deltaMode) {
                deltaSerialise(timeseriesMap, dos);
            } else {
                defaultSerialise(timeseriesMap, dos);
            }
        } catch (final IOException e) {
            throw new SerialisationException("Exception writing serialised LongTimeSeries to ByteArrayOutputStream",
                    e);
        }
        return baos.toByteArray();
    }

}