@Override public int compareTo(final BsonTimestamp ts) { return UnsignedLongs.compare(value, ts.value); }
/** * Equivalent of Long.compareUnsigned in Java 8. * * @param first the first value * @param second the second value * @return 0 if the values are equal, a value greater than zero if first is greater than second, * a value less than zero if first is less than second */ public static int compare(final long first, final long second) { return compareLongs(first + Long.MIN_VALUE, second + Long.MIN_VALUE); }
/** * Equivalent to Long.parseUnsignedLong in Java 8. * * @param string the string representation of an unsigned long * @return the unsigned long */ public static long parse(final String string) { if (string.length() == 0) { throw new NumberFormatException("empty string"); } int radix = 10; int maxSafePos = MAX_SAFE_DIGITS[radix] - 1; long value = 0; for (int pos = 0; pos < string.length(); pos++) { int digit = Character.digit(string.charAt(pos), radix); if (digit == -1) { throw new NumberFormatException(string); } if (pos > maxSafePos && overflowInParse(value, digit, radix)) { throw new NumberFormatException("Too large for unsigned long: " + string); } value = (value * radix) + digit; } return value; }
@Override public void convert(final BsonTimestamp value, final StrictJsonWriter writer) { writer.writeStartObject(); writer.writeStartObject("$timestamp"); writer.writeNumber("t", UnsignedLongs.toString(toUnsignedLong(value.getTime()))); writer.writeNumber("i", UnsignedLongs.toString(toUnsignedLong(value.getInc()))); writer.writeEndObject(); writer.writeEndObject(); }
private static long remainder(final long dividend, final long divisor) { if (divisor < 0) { // i.e., divisor >= 2^63: if (compare(dividend, divisor) < 0) { return dividend; // dividend < divisor } else { return dividend - divisor; // dividend >= divisor } } // Optimization - use signed modulus if dividend < 2^63 if (dividend >= 0) { return dividend % divisor; } // Otherwise, approximate the quotient, check, and correct if necessary. Our approximation is // guaranteed to be either exact or one less than the correct value. This follows from the fact // that floor(floor(x)/i) == floor(x/i) for any real x and integer i != 0. The proof is not // quite trivial. long quotient = ((dividend >>> 1) / divisor) << 1; long rem = dividend - quotient * divisor; return rem - (compare(rem, divisor) >= 0 ? divisor : 0); }
private static long divide(final long dividend, final long divisor) { if (divisor < 0) { // i.e., divisor >= 2^63: if (compare(dividend, divisor) < 0) { return 0; // dividend < divisor } else { return 1; // dividend >= divisor } } // Optimization - use signed division if dividend < 2^63 if (dividend >= 0) { return dividend / divisor; } // Otherwise, approximate the quotient, check, and correct if necessary. Our approximation is // guaranteed to be either exact or one less than the correct value. This follows from fact that // floor(floor(x)/i) == floor(x/i) for any real x and integer i != 0. The proof is not quite // trivial. long quotient = ((dividend >>> 1) / divisor) << 1; long rem = dividend - quotient * divisor; return quotient + (compare(rem, divisor) >= 0 ? 1 : 0); }