package org.implab.gradle.common.exec;

import java.util.List;

import org.eclipse.jdt.annotation.NonNullByDefault;

import static java.util.Objects.requireNonNull;

import java.util.ArrayList;

@NonNullByDefault
public record CommandSpecRecord(String executable, List<String> arguments) implements CommandSpec {

    static class Builder implements CommandBuilder {

        private String executable;

        private List<String> arguments = new ArrayList<>();

        @Override
        public CommandBuilder executable(String executable) {
            requireNonNull(executable, "cmd can't be null");
            this.executable = executable;
            return this;
        }

        @Override
        public CommandBuilder arguments(Iterable<String> args) {
            requireNonNull(args, "Args must not be null");
            arguments.clear();
            for (var arg : args)
                arguments.add(arg);
            return this;
        }

        @Override
        public CommandBuilder addArguments(String... args) {
            for (var arg : args)
                arguments.add(requireNonNull(arg, "arguments element shouldn't be null"));

            return this;
        }

        @Override
        public CommandSpec build() {
            requireNonNull(executable, "Executable must be specified");
            return new CommandSpecRecord(executable, arguments);
        }

    }
}
