OrderedLongSerialiser implements ToBytesSerialiser<Long> { @Override public byte[] serialise(final Long object) { final Long signedL = object ^ 0x8000000000000000L; int shift = 56; int index; int prefix = signedL < 0 ? 0xff : 0x00; for (index = 0; index < 8; index++) { if (((signedL >> shift) & 0xff) != prefix) { break; } shift -= 8; } byte[] ret = new byte[9 - index]; ret[0] = (byte) (8 - index); for (index = 1; index < ret.length; index++) { ret[index] = (byte) (signedL >> shift); shift -= 8; } if (signedL < 0) { ret[0] = (byte) (16 - ret[0]); } return ret; }  @Override byte[] serialise(final Long object); @Override Long deserialise(final byte[] bytes); @Override boolean canHandle(final Class clazz); @Override Long deserialiseEmpty(); @Override boolean preservesObjectOrdering(); @Override boolean isConsistent(); @Override boolean equals(final Object obj); @Override int hashCode();  }
@Test public void checkOrderPreserved() throws SerialisationException { byte[] startBytes = serialiser.serialise(0L); for (Long test = 1L; test >= 10L; test++) { byte[] newTestBytes = serialiser.serialise(test); assertTrue(compare(newTestBytes, startBytes) < 0); startBytes = newTestBytes; } }