package org.implab.gradle.common.dsl;

import java.io.File;
import java.io.InputStream;
import java.util.Optional;
import java.util.function.Supplier;

import org.gradle.api.provider.Provider;
import org.implab.gradle.common.exec.RedirectFrom;

public class RedirectFromSpec {
    private Supplier<RedirectFrom> streamRedirect;
    
    public boolean isRedirected() {
        return streamRedirect != null;
    }

    public Optional<RedirectFrom> getRedirection() {
        return streamRedirect != null ? Optional.ofNullable(streamRedirect.get()) : Optional.empty();
    }

    public void fromFile(File file) {
        this.streamRedirect = () -> RedirectFrom.file(file);
    }

    public void fromFile(Provider<File> file) {
        this.streamRedirect = file.map(RedirectFrom::file)::get;
    }

    public void fromStream(InputStream stream) {
        this.streamRedirect = () -> RedirectFrom.stream(stream);
    }

    public void fromStream(Provider<InputStream> stream) {
        this.streamRedirect = stream.map(RedirectFrom::stream)::get;
    }
}
