* NOTE: this representation must not be modified otherwise interop with * earlier protocol versions will be broken. * * @return The encoded representation of this CSN. * @see #valueOf(ByteSequence) */ public ByteString toByteString() { return toByteString(new ByteStringBuilder(BYTE_ENCODING_LENGTH)) .toByteString(); } /** * Encodes this CSN into the provided byte string builder. *
* NOTE: this representation must not be modified otherwise interop with * earlier protocol versions will be broken. * * @param builder * The byte string builder. * @return The byte string builder containing the encoded CSN. * @see #valueOf(ByteSequence) */ public ByteStringBuilder toByteString(ByteStringBuilder builder) { return builder.appendLong(timeStamp).appendShort(serverId & 0xffff).appendInt(seqnum); } /** * Convert the {@link CSN} to a printable String. *
* NOTE: this representation must not be modified otherwise interop with * earlier protocol versions will be broken. * * @return the string */ @Override public String toString() { return String.format("%016x%04x%08x", timeStamp, serverId, seqnum); } /** * Convert the {@link CSN} to a printable String with a user friendly format. * * @return the string */ public String toStringUI() { Date date = new Date(timeStamp); return String.format( "%016x%04x%08x (sid=%d,tsd=%s,ts=%d,seqnum=%d)", timeStamp, serverId, seqnum, serverId, date.toString(), timeStamp, seqnum); } /** * Compares this CSN with the provided CSN for order and returns a negative * number if {@code csn1} is older than {@code csn2}, zero if they have the * same age, or a positive number if {@code csn1} is newer than {@code csn2}. * * @param csn1 * The first CSN to be compared, which may be {@code null}. * @param csn2 * The second CSN to be compared, which may be {@code null}. * @return A negative number if {@code csn1} is older than {@code csn2}, zero * if they have the same age, or a positive number if {@code csn1} is * newer than {@code csn2}. */ public static int compare(CSN csn1, CSN csn2) { if (csn1 == null) { return csn2 == null ? 0 : -1; } else if (csn2 == null) { return 1; } else if (csn1.timeStamp != csn2.timeStamp) { return csn1.timeStamp < csn2.timeStamp ? -1 : 1; } else if (csn1.seqnum != csn2.seqnum) { return csn1.seqnum - csn2.seqnum; } else { return csn1.serverId - csn2.serverId; } } /** * Computes the difference in number of changes between 2 CSNs. First one is * expected to be newer than second one. If this is not the case, 0 will be * returned. * * @param csn1 * the first {@link CSN} * @param csn2 * the second {@link CSN} * @return the difference */ public static int diffSeqNum(CSN csn1, CSN csn2) { if (csn1 == null) { return 0; } if (csn2 == null) { return csn1.getSeqnum(); } if (csn2.isNewerThanOrEqualTo(csn1)) { return 0; } int seqnum1 = csn1.getSeqnum(); long time1 = csn1.getTime(); int seqnum2 = csn2.getSeqnum(); long time2 = csn2.getTime(); if (time2 <= time1) { if (seqnum2 <= seqnum1) { return seqnum1 - seqnum2; } return Integer.MAX_VALUE - (seqnum2 - seqnum1) + 1; } return 0; } /** * Returns {@code true} if this CSN is older than the provided CSN. * * @param csn * The CSN to be compared. * @return {@code true} if this CSN is older than the provided CSN. */ public boolean isOlderThan(CSN csn) { return compare(this, csn) < 0; } /** * Returns {@code true} if this CSN is older than or equal to the provided * CSN. * * @param csn * The CSN to be compared. * @return {@code true} if this CSN is older than or equal to the provided * CSN. */ public boolean isOlderThanOrEqualTo(CSN csn) { return compare(this, csn) <= 0; } /** * Returns {@code true} if this CSN is newer than or equal to the provided * CSN. * * @param csn * The CSN to be compared. * @return {@code true} if this CSN is newer than or equal to the provided * CSN. */ public boolean isNewerThanOrEqualTo(CSN csn) { return compare(this, csn) >= 0; } /** * Returns {@code true} if this CSN is newer than the provided CSN. * * @param csn * The CSN to be compared. * @return {@code true} if this CSN is newer than the provided CSN. */ public boolean isNewerThan(CSN csn) { return compare(this, csn) > 0; } /** * Compares this CSN with the provided CSN for order and returns a negative * number if this CSN is older than {@code csn}, zero if they have the same * age, or a positive number if this CSN is newer than {@code csn}. * * @param csn * The CSN to be compared. * @return A negative number if this CSN is older than {@code csn}, zero if * they have the same age, or a positive number if this CSN is newer * than {@code csn}. */ @Override public int compareTo(CSN csn) { return compare(this, csn); } }