@Test public void testMerge() { assertEquals("Two empty clocks merge to an empty clock.", getClock().merge(getClock()).getEntries(), getClock().getEntries()); assertEquals("Merge of a clock with itself does nothing", getClock(1).merge(getClock(1)).getEntries(), getClock(1).getEntries()); assertEquals(getClock(1).merge(getClock(2)).getEntries(), getClock(1, 2).getEntries()); assertEquals(getClock(1).merge(getClock(1, 2)).getEntries(), getClock(1, 2).getEntries()); assertEquals(getClock(1, 2).merge(getClock(1)).getEntries(), getClock(1, 2).getEntries()); assertEquals("Two-way merge fails.", getClock(1, 1, 1, 2, 3, 5).merge(getClock(1, 2, 2, 4)).getEntries(), getClock(1, 1, 1, 2, 2, 3, 4, 5).getEntries()); assertEquals(getClock(2, 3, 5).merge(getClock(1, 2, 2, 4, 7)).getEntries(), getClock(1, 2, 2, 3, 4, 5, 7).getEntries()); }
public VectorClock merge(VectorClock clock) { VectorClock newClock = new VectorClock(); int i = 0; int j = 0; while(i < this.versions.size() && j < clock.versions.size()) { ClockEntry v1 = this.versions.get(i); ClockEntry v2 = clock.versions.get(j); if(v1.getNodeId() == v2.getNodeId()) { newClock.versions.add(new ClockEntry(v1.getNodeId(), Math.max(v1.getVersion(), v2.getVersion()))); i++; j++; } else if(v1.getNodeId() < v2.getNodeId()) { newClock.versions.add(v1.clone()); i++; } else { newClock.versions.add(v2.clone()); j++; } } for(int k = i; k < this.versions.size(); k++) newClock.versions.add(this.versions.get(k).clone()); for(int k = j; k < clock.versions.size(); k++) newClock.versions.add(clock.versions.get(k).clone()); return newClock; }
VectorClock implements IVersion, Serializable, Cloneable { public VectorClock merge(VectorClock clock) { VectorClock newClock = new VectorClock(); int i = 0; int j = 0; while(i < this.versions.size() && j < clock.versions.size()) { ClockEntry v1 = this.versions.get(i); ClockEntry v2 = clock.versions.get(j); if(v1.getNodeId() == v2.getNodeId()) { newClock.versions.add(new ClockEntry(v1.getNodeId(), Math.max(v1.getVersion(), v2.getVersion()))); i++; j++; } else if(v1.getNodeId() < v2.getNodeId()) { newClock.versions.add(v1.clone()); i++; } else { newClock.versions.add(v2.clone()); j++; } } for(int k = i; k < this.versions.size(); k++) newClock.versions.add(this.versions.get(k).clone()); for(int k = j; k < clock.versions.size(); k++) newClock.versions.add(clock.versions.get(k).clone()); return newClock; } }
VectorClock implements IVersion, Serializable, Cloneable { public VectorClock merge(VectorClock clock) { VectorClock newClock = new VectorClock(); int i = 0; int j = 0; while(i < this.versions.size() && j < clock.versions.size()) { ClockEntry v1 = this.versions.get(i); ClockEntry v2 = clock.versions.get(j); if(v1.getNodeId() == v2.getNodeId()) { newClock.versions.add(new ClockEntry(v1.getNodeId(), Math.max(v1.getVersion(), v2.getVersion()))); i++; j++; } else if(v1.getNodeId() < v2.getNodeId()) { newClock.versions.add(v1.clone()); i++; } else { newClock.versions.add(v2.clone()); j++; } } for(int k = i; k < this.versions.size(); k++) newClock.versions.add(this.versions.get(k).clone()); for(int k = j; k < clock.versions.size(); k++) newClock.versions.add(clock.versions.get(k).clone()); return newClock; } VectorClock();  VectorClock(long timestamp); @JsonCreator  VectorClock(@JsonProperty("entries") List<ClockEntry> versions, 
                       @JsonProperty("timestamp") long timestamp); }
VectorClock implements IVersion, Serializable, Cloneable { public VectorClock merge(VectorClock clock) { VectorClock newClock = new VectorClock(); int i = 0; int j = 0; while(i < this.versions.size() && j < clock.versions.size()) { ClockEntry v1 = this.versions.get(i); ClockEntry v2 = clock.versions.get(j); if(v1.getNodeId() == v2.getNodeId()) { newClock.versions.add(new ClockEntry(v1.getNodeId(), Math.max(v1.getVersion(), v2.getVersion()))); i++; j++; } else if(v1.getNodeId() < v2.getNodeId()) { newClock.versions.add(v1.clone()); i++; } else { newClock.versions.add(v2.clone()); j++; } } for(int k = i; k < this.versions.size(); k++) newClock.versions.add(this.versions.get(k).clone()); for(int k = j; k < clock.versions.size(); k++) newClock.versions.add(clock.versions.get(k).clone()); return newClock; } VectorClock();  VectorClock(long timestamp); @JsonCreator  VectorClock(@JsonProperty("entries") List<ClockEntry> versions, 
                       @JsonProperty("timestamp") long timestamp); VectorClock incremented(int nodeId, long time); @Override VectorClock clone(); @Override int hashCode(); @Override boolean equals(Object obj); @Override String toString(); @JsonIgnore long getMaxVersion(); VectorClock merge(VectorClock clock); @Override Occurred compare(IVersion v); static Occurred compare(VectorClock v1, VectorClock v2); long getTimestamp(); List<ClockEntry> getEntries(); }
VectorClock implements IVersion, Serializable, Cloneable { public VectorClock merge(VectorClock clock) { VectorClock newClock = new VectorClock(); int i = 0; int j = 0; while(i < this.versions.size() && j < clock.versions.size()) { ClockEntry v1 = this.versions.get(i); ClockEntry v2 = clock.versions.get(j); if(v1.getNodeId() == v2.getNodeId()) { newClock.versions.add(new ClockEntry(v1.getNodeId(), Math.max(v1.getVersion(), v2.getVersion()))); i++; j++; } else if(v1.getNodeId() < v2.getNodeId()) { newClock.versions.add(v1.clone()); i++; } else { newClock.versions.add(v2.clone()); j++; } } for(int k = i; k < this.versions.size(); k++) newClock.versions.add(this.versions.get(k).clone()); for(int k = j; k < clock.versions.size(); k++) newClock.versions.add(clock.versions.get(k).clone()); return newClock; } VectorClock();  VectorClock(long timestamp); @JsonCreator  VectorClock(@JsonProperty("entries") List<ClockEntry> versions, 
                       @JsonProperty("timestamp") long timestamp); VectorClock incremented(int nodeId, long time); @Override VectorClock clone(); @Override int hashCode(); @Override boolean equals(Object obj); @Override String toString(); @JsonIgnore long getMaxVersion(); VectorClock merge(VectorClock clock); @Override Occurred compare(IVersion v); static Occurred compare(VectorClock v1, VectorClock v2); long getTimestamp(); List<ClockEntry> getEntries();  }