package org.implab.gradle.common.exec;

import static java.util.Objects.requireNonNull;

import java.util.Optional;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.implab.gradle.common.utils.Values;

@NonNullByDefault
public interface CommandArgumentsBuilder<S extends CommandArgumentsBuilder<S>> {

    S self();

    default S flag(String name, boolean value) {
        requireNonNull(name, "Parameter name cannot be null");
        if (value)
            addArguments(name);
        return self();
    }

    default S param(String name, String value) {
        requireNonNull(name, "Parameter name cannot be null");
        requireNonNull(value, "Parameter value cannot be null");
        if (value != null && value.length() > 0)
            addArguments(name, value);
        return self();
    }

    default S param(String name, Optional<String> value) {
        value.ifPresent(v -> param(name, v));
        return self();
    }

    /** Adds the specified arguments to this builder */
    S addArguments(String... args);

    default S addArguments(Iterable<String> args) {
        for (String arg : args)
            addArguments(arg);

        return self();
    }

    /** Replaces arguments in the builder with the specified one. */
    default S arguments(String... args) {
        return arguments(Values.iterable(args));
    }

    /** Replaces arguments in the builder with the specified one. */
    S arguments(Iterable<String> args);
}
