Class VectorClock

  • All Implemented Interfaces:, java.lang.Cloneable, java.util.Map<java.lang.String,​java.lang.Long>, java.util.NavigableMap<java.lang.String,​java.lang.Long>, java.util.SortedMap<java.lang.String,​java.lang.Long>

    public class VectorClock
    extends java.util.TreeMap<java.lang.String,​java.lang.Long>
    Implements a vector clock that records the time stamps of all send and receive events. It contains functions to compare and merge two vector clocks.

    Vector clocks are a mechanism to track events/actions across multiple distributed clients. Using vector clocks, one can determine relationships between different events. In particular:

    • Event A happened before / was caused by event B (cause)
    • Event B happened after / caused event B (effect)
    • Event A and B happened simultaneously (no cause/effect relationship)
    See Also:
    Serialized Form
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  VectorClock.VectorClockComparison  
      • Nested classes/interfaces inherited from class java.util.AbstractMap

        java.util.AbstractMap.SimpleEntry<K extends java.lang.Object,​V extends java.lang.Object>, java.util.AbstractMap.SimpleImmutableEntry<K extends java.lang.Object,​V extends java.lang.Object>
    • Field Summary

      Modifier and Type Field Description
      static java.util.regex.Pattern MACHINE_PATTERN  
    • Constructor Summary

      Constructor Description
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      VectorClock clone()  
      static VectorClock.VectorClockComparison compare​(VectorClock clock1, VectorClock clock2)
      VectorClock compare operation.
      java.lang.Long get​(java.lang.Object unit)  
      java.lang.Long getClock​(java.lang.String unit)
      Retrieve the unit's value
      java.lang.Long incrementClock​(java.lang.String unit)
      Increases the component of a unit by 1.
      static VectorClock parseVectorClock​(java.lang.String serializedVectorClock)
      Converts a serialized vector clock back into a VectorClock object.
      void setClock​(java.lang.String unit, long value)
      Set the component of a unit.
      java.lang.String toString()  
      • Methods inherited from class java.util.TreeMap

        ceilingEntry, ceilingKey, clear, comparator, containsKey, containsValue, descendingKeySet, descendingMap, entrySet, firstEntry, firstKey, floorEntry, floorKey, forEach, headMap, headMap, higherEntry, higherKey, keySet, lastEntry, lastKey, lowerEntry, lowerKey, navigableKeySet, pollFirstEntry, pollLastEntry, put, putAll, remove, replace, replace, replaceAll, size, subMap, subMap, tailMap, tailMap, values
      • Methods inherited from class java.util.AbstractMap

        equals, hashCode, isEmpty
      • Methods inherited from class java.lang.Object

        finalize, getClass, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.util.Map

        compute, computeIfAbsent, computeIfPresent, equals, getOrDefault, hashCode, isEmpty, merge, putIfAbsent, remove
    • Field Detail


        public static final java.util.regex.Pattern MACHINE_PATTERN
    • Method Detail

      • incrementClock

        public java.lang.Long incrementClock​(java.lang.String unit)
        Increases the component of a unit by 1.
        unit - The identifier of the vector element being increased
        Returns the new clock value for the given unit
      • setClock

        public void setClock​(java.lang.String unit,
                             long value)
        Set the component of a unit.
        unit - The identifier of the vector element being set
        value - The new value of the unit being set
      • getClock

        public java.lang.Long getClock​(java.lang.String unit)
        Retrieve the unit's value
        unit - The identifier of the vector element being retrieved
        Returns the value of the unit (if existent), or null if it does not exist
      • get

        public java.lang.Long get​(java.lang.Object unit)
        Specified by:
        get in interface java.util.Map<java.lang.String,​java.lang.Long>
        get in class java.util.TreeMap<java.lang.String,​java.lang.Long>
      • clone

        public VectorClock clone()
        clone in class java.util.TreeMap<java.lang.String,​java.lang.Long>
      • toString

        public java.lang.String toString()
        toString in class java.util.AbstractMap<java.lang.String,​java.lang.Long>
      • compare

        public static VectorClock.VectorClockComparison compare​(VectorClock clock1,
                                                                VectorClock clock2)
        VectorClock compare operation. Returns one of four possible values indicating how clock one relates to clock two: VectorComparison.GREATER If One > Two. VectorComparison.EQUAL If One = Two. VectorComparison.SMALLER If One < Two. VectorComparison.SIMULTANEOUS If One != Two.
        clock1 - First Clock being compared.
        clock2 - Second Clock being compared.
        VectorComparison value indicating how One relates to Two.