package net.minecraft.util.thread;

import java.util.function.Consumer;
import com.mojang.datafixers.util.Either;
import java.util.concurrent.CompletableFuture;
import java.util.function.Function;

public interface ProcessorHandle<Msg> extends AutoCloseable {
    String name();
    
    void tell(final Msg object);
    
    default void close() {
    }
    
    default <Source> CompletableFuture<Source> ask(final Function<? super ProcessorHandle<Source>, ? extends Msg> function) {
        final CompletableFuture<Source> completableFuture3 = new CompletableFuture<Source>();
        final Msg object4 = (Msg)function.apply(ProcessorHandle.of("ask future procesor handle", completableFuture3::complete));
        this.tell(object4);
        return completableFuture3;
    }
    
    default <Source> CompletableFuture<Source> askEither(final Function<? super ProcessorHandle<Either<Source, Exception>>, ? extends Msg> function) {
        final CompletableFuture<Source> completableFuture3 = new CompletableFuture<Source>();
        final CompletableFuture<Object> completableFuture4;
        final Msg object4 = (Msg)function.apply(ProcessorHandle.of("ask future procesor handle", either -> {
            either.ifLeft((Consumer)completableFuture4::complete);
            either.ifRight((Consumer)completableFuture4::completeExceptionally);
            return;
        }));
        this.tell(object4);
        return completableFuture3;
    }
    
    default <Msg> ProcessorHandle<Msg> of(final String string, final Consumer<Msg> consumer) {
        return new ProcessorHandle<Msg>() {
            @Override
            public String name() {
                return string;
            }
            
            @Override
            public void tell(final Msg object) {
                consumer.accept(object);
            }
            
            @Override
            public String toString() {
                return string;
            }
        };
    }
}
