package com.mongodb.internal.connection;
import org.bson.BsonDocument;
import org.bson.BsonTimestamp;
public class ClusterClock {
private static final String CLUSTER_TIME_KEY = "clusterTime";
private BsonDocument clusterTime;
public synchronized BsonDocument getCurrent() {
return clusterTime;
}
public synchronized BsonTimestamp getClusterTime() {
return clusterTime != null ? clusterTime.getTimestamp(CLUSTER_TIME_KEY) : null;
}
public synchronized void advance(final BsonDocument other) {
this.clusterTime = greaterOf(other);
}
public synchronized BsonDocument greaterOf(final BsonDocument other) {
if (other == null) {
return clusterTime;
} else if (clusterTime == null) {
return other;
} else {
return other.getTimestamp(CLUSTER_TIME_KEY).compareTo(clusterTime.getTimestamp(CLUSTER_TIME_KEY)) > 0 ? other : clusterTime;
}
}
}