package fault;

import fault.concurrent.DefaultResilientPromise;
import fault.concurrent.ResilientFuture;
import fault.concurrent.ResilientPromise;
import fault.utils.ResilientPatternAction;

/**
 * Created by timbrooks on 6/4/15.
 */
public class LoadBalancer<C> implements Pattern<C> {

    @Override
    public <T> ResilientFuture<T> submitAction(ResilientPatternAction<T, C> action, long millisTimeout) {
        return submitAction(action, new DefaultResilientPromise<T>(), null, millisTimeout);
    }

    @Override
    public <T> ResilientFuture<T> submitAction(ResilientPatternAction<T, C> action, ResilientCallback<T> callback,
                                               long millisTimeout) {
        return submitAction(action, new DefaultResilientPromise<T>(), callback, millisTimeout);
    }

    @Override
    public <T> ResilientFuture<T> submitAction(ResilientPatternAction<T, C> action, ResilientPromise<T> promise,
                                               long millisTimeout) {
        return submitAction(action, promise, null, millisTimeout);
    }

    @Override
    public <T> ResilientFuture<T> submitAction(ResilientPatternAction<T, C> action, ResilientPromise<T> promise,
                                               ResilientCallback<T> callback, long millisTimeout) {
        return submitAction(action, promise, callback, millisTimeout);
    }

    @Override
    public <T> ResilientPromise<T> performAction(ResilientPatternAction<T, C> action) {
        return null;
    }
}

@@ -185,16 +185,65 @@
 ction;%0A%0A
+import java.util.List;%0Aimport java.util.Random;%0A%0A
 /**%0A * C
@@ -331,16 +331,405 @@
 n%3CC%3E %7B%0A%0A
+    private final ServiceExecutor%5B%5D services;%0A    private final C context;%0A%0A    public LoadBalancer(List%3CServiceExecutor%3E executors, C context) %7B%0A        if (executors.size() == 0) %7B%0A            throw new IllegalArgumentException(%22Cannot create LoadBalancer with 0 Executors.%22);%0A        %7D%0A%0A        this.context = context;%0A        services = (ServiceExecutor%5B%5D) executors.toArray();%0A    %7D%0A%0A
     @Ove
@@ -1563,32 +1563,38 @@
 T%3E submitAction(
+final 
 ResilientPattern
@@ -1759,34 +1759,214 @@
 return s
-ubmitAction(action
+ervices%5BnextService()%5D.submitAction(new ResilientAction%3CT%3E() %7B%0A            @Override%0A            public T run() throws Exception %7B%0A                return action.run(context);%0A            %7D%0A        %7D
 , promis
@@ -2060,24 +2060,30 @@
 rformAction(
+final 
 ResilientPat
@@ -2124,20 +2124,328 @@
  return 
-null
+services%5BnextService()%5D.performAction(new ResilientAction%3CT%3E() %7B%0A            @Override%0A            public T run() throws Exception %7B%0A                return action.run(context);%0A            %7D%0A        %7D);%0A    %7D%0A%0A    private int nextService() %7B%0A        return new Random().nextInt(services.length) %25 services.length
 ;%0A    %7D%0A

Work on implementing LoadBalancer