* 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() { final ByteStringBuilder builder = new ByteStringBuilder(BYTE_ENCODING_LENGTH); toByteString(builder); return builder.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. * @see #valueOf(ByteSequence) */ public void toByteString(ByteStringBuilder builder) { 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() { final StringBuilder buffer = new StringBuilder(); toString(buffer); return buffer.toString(); } /** * Appends the text representation of this {@link CSN} into the provided StringBuilder. *
* NOTE: this representation must not be modified otherwise interop with * earlier protocol versions will be broken. * * @param buffer the StringBuilder where to output the CSN text representation */ void toString(final StringBuilder buffer) { leftPadWithZeros(buffer, 16, Long.toHexString(timeStamp)); leftPadWithZeros(buffer, 4, Integer.toHexString(serverId)); leftPadWithZeros(buffer, 8, Integer.toHexString(seqnum)); } private void leftPadWithZeros(StringBuilder buffer, int nbChars, String toAppend) { final int padding = nbChars - toAppend.length(); for (int i = 0; i < padding; i++) { buffer.append('0'); } buffer.append(toAppend); } /** * Convert the {@link CSN} to a printable String with a user friendly format. * * @return the string */ public String toStringUI() { final StringBuilder buffer = new StringBuilder(); toStringUI(buffer); return buffer.toString(); } private void toStringUI(final StringBuilder buffer) { toString(buffer); buffer.append(" (sid=").append(serverId) .append(",tsd=").append(new Date(timeStamp)) .append(",ts=").append(timeStamp) .append(",seqnum=").append(seqnum) .append(")"); } /** * 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 Integer.compare(csn1.seqnum, csn2.seqnum); } else { return Integer.compare(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); } }