-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPN-Counter.java
49 lines (36 loc) · 965 Bytes
/
PN-Counter.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
package crdt.counters;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import static java.lang.Math.max;
import crdt.CRDT;
/**
* Increment and decrement counter
*/
public class PNCounter<T> implements CRDT<PNCounter<T>> {
private static final long serialVersionUID = 1L;
private GCounter<T> inc = new GCounter<T>();
private GCounter<T> dec = new GCounter<T>();
public void increment(T key) {
inc.increment(key);
}
public int get() {
return inc.get() - dec.get();
}
public void decrement(T key) {
dec.increment(key);
}
/**
* Merge another counter into this one
*/
public void merge(PNCounter<T> other) {
inc.merge( other.inc );
dec.merge( other.dec );
}
public PNCounter<T> copy() {
PNCounter<T> copy = new PNCounter<T>();
copy.inc = inc.copy();
copy.dec = dec.copy();
return copy;
}
}