-
Notifications
You must be signed in to change notification settings - Fork 383
/
VectorClock.java
115 lines (101 loc) · 3.22 KB
/
VectorClock.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
* Copyright 2017-present Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.atomix.time;
import com.google.common.annotations.Beta;
import io.atomix.utils.Identifier;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import static com.google.common.base.MoreObjects.toStringHelper;
/**
* Vector clock.
*/
@Beta
public class VectorClock<T extends Identifier> implements Clock<VectorTimestamp<T>> {
private final T localIdentifier;
private final Map<T, VectorTimestamp<T>> vector = new HashMap<>();
public VectorClock(T localIdentifier) {
this(new VectorTimestamp<T>(localIdentifier, 0));
}
public VectorClock(VectorTimestamp<T> localTimestamp) {
this(localTimestamp, Collections.emptyList());
}
public VectorClock(VectorTimestamp<T> localTimestamp, Collection<VectorTimestamp<T>> vector) {
this.localIdentifier = localTimestamp.identifier();
this.vector.put(localTimestamp.identifier(), localTimestamp);
for (VectorTimestamp<T> timestamp : vector) {
this.vector.put(timestamp.identifier(), timestamp);
}
}
@Override
public VectorTimestamp<T> getTime() {
return vector.get(localIdentifier);
}
/**
* Returns the local logical timestamp.
*
* @return the logical timestamp for the local identifier
*/
public LogicalTimestamp getLocalTimestamp() {
return getTime();
}
/**
* Returns the logical timestamp for the given identifier.
*
* @param identifier the identifier for which to return the timestamp
* @return the logical timestamp for the given identifier
*/
public LogicalTimestamp getTimestamp(T identifier) {
return vector.get(identifier);
}
/**
* Returns a collection of identifier-timestamp pairs.
*
* @return a collection of identifier-timestamp pairs
*/
public Collection<VectorTimestamp<T>> getTimestamps() {
return vector.values();
}
/**
* Updates the given timestamp.
*
* @param timestamp the timestamp to update
*/
public void update(VectorTimestamp<T> timestamp) {
VectorTimestamp<T> currentTimestamp = vector.get(timestamp.identifier());
if (currentTimestamp == null || currentTimestamp.value() < timestamp.value()) {
vector.put(timestamp.identifier(), timestamp);
}
}
/**
* Updates the vector clock.
*
* @param clock the vector clock with which to update this clock
*/
public void update(VectorClock<T> clock) {
for (VectorTimestamp<T> timestamp : clock.vector.values()) {
update(timestamp);
}
}
@Override
public String toString() {
return toStringHelper(this)
.add("time", getTime())
.add("vector", getTimestamps())
.toString();
}
}