-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathVectorClock.java
More file actions
101 lines (85 loc) · 2.04 KB
/
VectorClock.java
File metadata and controls
101 lines (85 loc) · 2.04 KB
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
import java.util.HashMap;
public class VectorClock implements Comparable<VectorClock> {
public HashMap<Integer, Integer> clock = new HashMap<Integer, Integer>();
public static int NumElems = 0;
public VectorClock() {
}
public VectorClock(int[] a) {
for (int i = 0; i < NumElems; i++) {
for (int j = 0; j < a[i]; j++) {
increment(i);
}
}
}
public void increment() {
increment(ServerStateManager.getID());
}
public void increment(int ID) {
if (clock.containsKey(ID))
clock.put(ID, clock.get(ID) + 1);
else
clock.put(ID, 1);
}
public void setValue(int ID, int value) {
clock.put(ID, value);
}
public void mergeIn(VectorClock v) {
for (Integer i : v.clock.keySet()) {
if (!clock.containsKey(i) || clock.get(i) < v.clock.get(i)) {
clock.put(i, v.clock.get(i));
}
}
}
public VectorClock getCopy() {
VectorClock retval = new VectorClock();
for (int i : clock.keySet()) {
retval.setValue(i, clock.get(i).intValue());
}
return retval;
}
public String toString() {
StringBuffer retval = new StringBuffer();
retval.append("( ");
for (int i = 0; i < NumElems; i++) {
retval.append((clock.containsKey(i) ? clock.get(i) : 0) + " ");
}
retval.append(")");
return new String(retval);
}
public int compareTo(VectorClock v) {
boolean isEqual = true;
boolean isGreater = true;
boolean isSmaller = true;
for (Integer i : clock.keySet()) {
if (v.clock.containsKey(i)) {
if (clock.get(i) < v.clock.get(i)) {
isEqual = false;
isGreater = false;
} else if (clock.get(i) > v.clock.get(i)) {
isEqual = false;
isSmaller = false;
}
} else {
isEqual = false;
isSmaller = false;
}
}
for (Integer i : v.clock.keySet()) {
if (!clock.containsKey(i)) {
isEqual = false;
isGreater = false;
}
}
// Return based on determined information.
if (isEqual) {
return 0;
} else if (isGreater && !isSmaller) {
return 1;
} else if (isSmaller && !isGreater) {
return -1;
} else {
System.out.println("Conflict");
return 0;
}
}
}