-
Notifications
You must be signed in to change notification settings - Fork 2
/
G-Set.java
48 lines (36 loc) · 953 Bytes
/
G-Set.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
package crdt.sets;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Set;
import crdt.CRDT;
/**
* Grow only set. Items can only be added
*/
public class GSet<T> implements CRDT<GSet<T>> {
private static final long serialVersionUID = 1L;
private Set<T> items = new LinkedHashSet<T>();
public void add(T item) {
items.add(item);
}
public Set<T> get() {
return Collections.unmodifiableSet(items);
}
/**
* Merge another set into this one
*/
public void merge(GSet<T> set) {
items.addAll(set.items);
}
public boolean contains(T item) {
return items.contains(item);
}
public void addAll(Collection<T> itemToAdd) {
items.addAll(itemToAdd);
}
public GSet<T> copy() {
GSet<T> copy = new GSet<T>();
copy.items = new LinkedHashSet<T>(items);
return copy;
}
}