All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.magentys.FunctionalAgent Maven / Gradle / Ivy

There is a newer version: 1.0.7
Show newest version
package io.magentys;


import io.magentys.functional.Functions;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Future;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;


public class FunctionalAgent extends Agent {


    public FunctionalAgent(Memory memory) {
        super(memory);
    }

    public static FunctionalAgent functionalAgent() { return new FunctionalAgent(new CoreMemory()); }

    public  OUTPUT performs(Function myFunction, INPUT input) {
         return myFunction.apply(input);
    }

    public  Boolean tests(Predicate predicate, INPUT input) {
         return predicate.test(input);
    }


    public  FunctionalAgent keepsInMindTheResultOf(Supplier supplier, String uniqueKey) {
        this.keepsInMind(uniqueKey, supplier.get());
        return this;
    }


    public  OUTPUT performs(Functions.Function3 functionalMission, One one, Two two) {
        return functionalMission.apply(one,two, this);
    }

    public  OUTPUT performs(Functions.Function4 functionalMission, One one, Two two, Three three) {
        return functionalMission.apply(one,two, three, this);
    }

    public  OUTPUT performs(Functions.Function5 functionalMission, One one, Two two, Three three, Four four) {
        return functionalMission.apply(one,two, three, four, this);
    }

    public  Result performs(Functions.FunctionalMission1 mission, One one) {
        return mission.apply(one, this);
    }

    public  Result performs(Functions.FunctionalMission2 mission, One one, Two two) {
        return mission.apply(one, two, this);
    }

    public  Result performs(Functions.FunctionalMission3 mission, One one, Two two, Three three) {
        return mission.apply(one, two, three, this);
    }

    public  Result performs(Functions.FunctionalMission4 mission, One one, Two two, Three three, Four four) {
        return mission.apply(one, two, three, four, this);
    }

    public  CompletableFuture performsAsync(FutureMission mission) {
        return mission.accomplishAsync(mission,this);
    }

    public  Future performsAsync(Functions.FunctionalMission1 mission, Input input) {
        CompletableFuture futureResult = new CompletableFuture<>();
        Runnable runnable = () -> {
            try {
                Result result = mission.apply(input, this);
                futureResult.complete(result);
            } catch (Throwable e) {
                futureResult.completeExceptionally(e);
            }
        };
        new Thread(runnable).start();
        return futureResult;
    }

   public  Future performsAsync(Functions.FunctionalMission2 mission, One one, Two two) {
        CompletableFuture futureResult = new CompletableFuture<>();
        Runnable runnable = () -> {
            try {
                Result result = mission.apply(one, two, this);
                futureResult.complete(result);
            } catch (Throwable e) {
                futureResult.completeExceptionally(e);
            }
        };
        new Thread(runnable).start();
        return futureResult;
    }

    public  Future performsAsync(Functions.FunctionalMission3 mission, One one, Two two, Three three) {
        CompletableFuture futureResult = new CompletableFuture<>();
        Runnable runnable = () -> {
            try {
                Result result = mission.apply(one, two, three, this);
                futureResult.complete(result);
            } catch (Throwable e) {
                futureResult.completeExceptionally(e);
            }
        };
        new Thread(runnable).start();
        return futureResult;
    }

    public  Future performsAsync(Functions.FunctionalMission4 mission, One one, Two two, Three three, Four four) {
        CompletableFuture futureResult = new CompletableFuture<>();
        Runnable runnable = () -> {
            try {
                Result result = mission.apply(one, two, three, four, this);
                futureResult.complete(result);
            } catch (Throwable e) {
                futureResult.completeExceptionally(e);
            }
        };
        new Thread(runnable).start();
        return futureResult;
    }





}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy