Repository: kestra-io/kestra
File: core/src/main/java/io/kestra/core/models/executions/Execution.java (Java)

Code before:
     * @param inputs The Flow's inputs.
     * @param labels The Flow labels.
     * @return a new {@link Execution}.
     */
    public static Execution newExecution(final FlowInterface flow,
        final BiFunction<FlowInterface, Execution, Map<String, Object>> inputs,
        final List<Label> labels,
        final Optional<ZonedDateTime> scheduleDate) {
        Execution execution = builder()
            .id(IdUtils.create())
            .tenantId(flow.getTenantId())
            .namespace(flow.getNamespace())
            .flowId(flow.getId())
            .flowRevision(flow.getRevision())
            .state(new State())
            .scheduleDate(scheduleDate.map(ChronoZonedDateTime::toInstant).orElse(null))
            .variables(flow.getVariables())
            .build();

        List<Label> executionLabels = new ArrayList<>(LabelService.labelsExcludingSystem(flow));
        if (labels != null) {
            executionLabels.addAll(labels);
        }
        if (executionLabels.stream().noneMatch(label -> Label.CORRELATION_ID.equals(label.key()))) {
            // add a correlation ID if none exist
            executionLabels.add(new Label(Label.CORRELATION_ID, execution.getId()));
        }
        execution = execution.withLabels(executionLabels);

        if (inputs != null) {
            execution = execution.withInputs(inputs.apply(flow, execution));
        }

        return execution;
    }


    /**
     * Customization of Lombok-generated builder.
     */
    public static class ExecutionBuilder {

        /**
         * Enforce unique values of {@link Label} when using the builder.
         *
         * @param labels The labels.
         * @return Deduplicated labels.
         */
        public ExecutionBuilder labels(List<Label> labels) {
            this.labels = Label.deduplicate(labels);
            return this;

Review comment (suggestion):
I really don't like the use of `Optional` in the method parameters. I know we do it in other places.

Code after:
     * @param inputs The Flow's inputs.
     * @param labels The Flow labels.
     * @return a new {@link Execution}.
     */
    public static Execution newExecution(final FlowInterface flow,
        final BiFunction<FlowInterface, Execution, Map<String, Object>> inputs,
        final List<Label> labels,
        final Optional<ZonedDateTime> scheduleDate) {
        return newExecution(flow, inputs, labels, scheduleDate, null);
    }

    /**
     * Factory method for constructing a new {@link Execution} object for the given {@link Flow} and
     * inputs.
     *
     * @param flow The Flow.
     * @param inputs The Flow's inputs.
     * @param labels The Flow labels.
     * @param kind The ExecutionKind.
     *
     * @return a new {@link Execution}.
     */
    public static Execution newExecution(final FlowInterface flow,
                                         final BiFunction<FlowInterface, Execution, Map<String, Object>> inputs,
                                         final List<Label> labels,
                                         final Optional<ZonedDateTime> scheduleDate,
                                         @Nullable final ExecutionKind kind) {
        Execution execution = builder()
            .id(IdUtils.create())
            .tenantId(flow.getTenantId())
            .namespace(flow.getNamespace())
            .flowId(flow.getId())
            .flowRevision(flow.getRevision())
            .state(new State())
            .scheduleDate(scheduleDate.map(ChronoZonedDateTime::toInstant).orElse(null))
            .variables(flow.getVariables())
            .kind(kind)
            .build();

        List<Label> executionLabels = new ArrayList<>(LabelService.labelsExcludingSystem(flow));
        if (labels != null) {
            executionLabels.addAll(labels);
        }
        if (executionLabels.stream().noneMatch(label -> Label.CORRELATION_ID.equals(label.key()))) {
            // add a correlation ID if none exist
            executionLabels.add(new Label(Label.CORRELATION_ID, execution.getId()));
        }
        execution = execution.withLabels(executionLabels);

        if (inputs != null) {
            execution = execution.withInputs(inputs.apply(flow, execution));