Skip to content
Snippets Groups Projects
Commit cd591a05 authored by Kim Kern's avatar Kim Kern
Browse files

refactor builder pattern AggregationContext

parent 8d8cdf5a
No related branches found
No related tags found
No related merge requests found
...@@ -5,9 +5,9 @@ import java.util.function.Function; ...@@ -5,9 +5,9 @@ import java.util.function.Function;
import fucoin.wallet.AbstractWallet; import fucoin.wallet.AbstractWallet;
public enum AggregationMethod { public enum AggregationMethod {
Average(x -> new double[]{x.getAmount()}, (x, y) -> new double[]{(x[0] + y[0]) / 2}), Average((x, y) -> new double[]{(x[0] + y[0]) / 2}),
Maximum(x -> new double[]{x.getAmount()}, (x, y) -> new double[]{Math.max(x[0], y[0])}), Maximum((x, y) -> new double[]{Math.max(x[0], y[0])}),
Minimum(x -> new double[]{x.getAmount()}, (x, y) -> new double[]{Math.min(x[0], y[0])}), Minimum((x, y) -> new double[]{Math.min(x[0], y[0])}),
Count(x -> new double[]{0}, (x, y) -> new double[]{(x[0] + y[0]) / 2}, x -> (1 / x[0]), false, x -> new double[]{1}), Count(x -> new double[]{0}, (x, y) -> new double[]{(x[0] + y[0]) / 2}, x -> (1 / x[0]), false, x -> new double[]{1}),
Sum(x -> new double[]{x.getAmount(), 0}, (x, y) -> new double[]{(x[0] + y[0]) / 2, (x[1] + y[1]) / 2}, x -> x[0]*(1 / x[1]), false, x -> new double[]{x.getAmount(), 1}); Sum(x -> new double[]{x.getAmount(), 0}, (x, y) -> new double[]{(x[0] + y[0]) / 2, (x[1] + y[1]) / 2}, x -> x[0]*(1 / x[1]), false, x -> new double[]{x.getAmount(), 1});
...@@ -18,8 +18,8 @@ public enum AggregationMethod { ...@@ -18,8 +18,8 @@ public enum AggregationMethod {
private double[] values; private double[] values;
private final Function<double[], Double> resultExtractor; private final Function<double[], Double> resultExtractor;
AggregationMethod(Function<AbstractWallet, double[]> valueExtractor, AggregationFunction function) { AggregationMethod(AggregationFunction function) {
this.valueExtractor = valueExtractor; this.valueExtractor = x -> new double[]{x.getAmount()};
this.function = function; this.function = function;
this.useValueExtractor = true; this.useValueExtractor = true;
this.values = new double[]{ 0 }; this.values = new double[]{ 0 };
......
...@@ -185,7 +185,10 @@ public class SuperVisorImpl extends AbstractNode implements TransactionLogger { ...@@ -185,7 +185,10 @@ public class SuperVisorImpl extends AbstractNode implements TransactionLogger {
AggregationFunction function = method.getFunction(); AggregationFunction function = method.getFunction();
double[] values = method.getValues(); double[] values = method.getValues();
Function<double[], Double> resultExtractor = method.getResultExtractor(); Function<double[], Double> resultExtractor = method.getResultExtractor();
AggregationContext context = new AggregationContextBuilder(function, valueExtractor, valueAltExtractor, values, resultExtractor) AggregationContext context = new AggregationContextBuilder(function, values)
.setValueExtractor(valueExtractor)
.setValueAltExtractor(valueAltExtractor)
.setResultExtractor(resultExtractor)
.setMaxExchanges(exchanges) .setMaxExchanges(exchanges)
.build(); .build();
ActorRef randomNode = getRandomNeighbor(); ActorRef randomNode = getRandomNeighbor();
......
...@@ -30,8 +30,8 @@ public class WalletImpl extends AbstractWallet { ...@@ -30,8 +30,8 @@ public class WalletImpl extends AbstractWallet {
private transient WalletGuiControl gui; private transient WalletGuiControl gui;
private String preKnownNeighbourName; private String preKnownNeighbourName;
private boolean isActive; private boolean isActive;
private AggregationContext aggregationContext = new AggregationContextBuilder(null, null, null, new double[] { -1 }, private AggregationContext aggregationContext = new AggregationContextBuilder((x, y) -> x, new double[] { -1 })
x -> x[0]).build(); .build();
private boolean hasPendingAggregationRequest = false; private boolean hasPendingAggregationRequest = false;
private ConcurrentLinkedQueue<ActorRef> deferedSupervisorReceivers = new ConcurrentLinkedQueue<>(); private ConcurrentLinkedQueue<ActorRef> deferedSupervisorReceivers = new ConcurrentLinkedQueue<>();
private transient final EvictingQueue<UUID> handledAggregationRequests = EvictingQueue.create(10); private transient final EvictingQueue<UUID> handledAggregationRequests = EvictingQueue.create(10);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment