!cdd #17770 Use self recursive type in AbstractReplicatedData (Java API)
* complete the TwoPhaseSet java sample with serialization
This commit is contained in:
parent
49e6e7f38c
commit
da07a2e68e
9 changed files with 315 additions and 26 deletions
48
akka-docs/rst/java/code/docs/ddata/japi/TwoPhaseSet.java
Normal file
48
akka-docs/rst/java/code/docs/ddata/japi/TwoPhaseSet.java
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
/**
|
||||
* Copyright (C) 2015 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
package docs.ddata.japi;
|
||||
|
||||
import java.util.HashSet;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import akka.cluster.ddata.AbstractReplicatedData;
|
||||
import akka.cluster.ddata.GSet;
|
||||
|
||||
//#twophaseset
|
||||
public class TwoPhaseSet extends AbstractReplicatedData<TwoPhaseSet> {
|
||||
|
||||
public final GSet<String> adds;
|
||||
public final GSet<String> removals;
|
||||
|
||||
public TwoPhaseSet(GSet<String> adds, GSet<String> removals) {
|
||||
this.adds = adds;
|
||||
this.removals = removals;
|
||||
}
|
||||
|
||||
public static TwoPhaseSet create() {
|
||||
return new TwoPhaseSet(GSet.create(), GSet.create());
|
||||
}
|
||||
|
||||
public TwoPhaseSet add(String element) {
|
||||
return new TwoPhaseSet(adds.add(element), removals);
|
||||
}
|
||||
|
||||
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) {
|
||||
return new TwoPhaseSet(this.adds.merge(that.adds),
|
||||
this.removals.merge(that.removals));
|
||||
}
|
||||
}
|
||||
//#twophaseset
|
||||
Loading…
Add table
Add a link
Reference in a new issue