2018-10-29 17:19:37 +08:00
|
|
|
/*
|
2020-01-02 07:24:59 -05:00
|
|
|
* Copyright (C) 2015-2020 Lightbend Inc. <https://www.lightbend.com>
|
2015-07-02 12:12:34 +02:00
|
|
|
*/
|
2018-03-13 23:45:55 +09:00
|
|
|
|
2017-03-16 09:30:00 +01:00
|
|
|
package jdocs.ddata;
|
2015-07-02 12:12:34 +02:00
|
|
|
|
|
|
|
|
import java.util.HashSet;
|
|
|
|
|
|
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
|
|
import akka.cluster.ddata.AbstractReplicatedData;
|
|
|
|
|
import akka.cluster.ddata.GSet;
|
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
// #twophaseset
|
2015-07-02 12:12:34 +02:00
|
|
|
public class TwoPhaseSet extends AbstractReplicatedData<TwoPhaseSet> {
|
2019-01-12 04:00:53 +08:00
|
|
|
|
2015-07-02 12:12:34 +02:00
|
|
|
public final GSet<String> adds;
|
|
|
|
|
public final GSet<String> removals;
|
2019-01-12 04:00:53 +08:00
|
|
|
|
2015-07-02 12:12:34 +02:00
|
|
|
public TwoPhaseSet(GSet<String> adds, GSet<String> removals) {
|
|
|
|
|
this.adds = adds;
|
|
|
|
|
this.removals = removals;
|
|
|
|
|
}
|
2019-01-12 04:00:53 +08:00
|
|
|
|
2015-07-02 12:12:34 +02:00
|
|
|
public static TwoPhaseSet create() {
|
|
|
|
|
return new TwoPhaseSet(GSet.create(), GSet.create());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public TwoPhaseSet add(String element) {
|
|
|
|
|
return new TwoPhaseSet(adds.add(element), removals);
|
|
|
|
|
}
|
2019-01-12 04:00:53 +08:00
|
|
|
|
2015-07-02 12:12:34 +02:00
|
|
|
public TwoPhaseSet remove(String element) {
|
|
|
|
|
return new TwoPhaseSet(adds, removals.add(element));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Set<String> getElements() {
|
|
|
|
|
Set<String> result = new HashSet<>(adds.getElements());
|
|
|
|
|
result.removeAll(removals.getElements());
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public TwoPhaseSet mergeData(TwoPhaseSet that) {
|
2019-01-12 04:00:53 +08:00
|
|
|
return new TwoPhaseSet(this.adds.merge(that.adds), this.removals.merge(that.removals));
|
2015-07-02 12:12:34 +02:00
|
|
|
}
|
|
|
|
|
}
|
2019-01-12 04:00:53 +08:00
|
|
|
// #twophaseset
|