001/*
002 * SPDX-FileCopyrightText: none
003 * SPDX-License-Identifier: CC0-1.0
004 */
005
006package dev.metaschema.cli.processor.command;
007
008import org.apache.commons.cli.CommandLine;
009
010import dev.metaschema.cli.processor.CallingContext;
011import edu.umd.cs.findbugs.annotations.NonNull;
012
013/**
014 * An abstract base class that implements the {@link ICommandExecutor}
015 * interface, providing common functionality for command execution
016 * implementations. Concrete subclasses must implement the {@link #execute()}
017 * method to define specific command behavior.
018 */
019@FunctionalInterface
020public interface ICommandExecutor {
021  /**
022   * Execute the command operation.
023   *
024   * @throws CommandExecutionException
025   *           if an error occurred while executing the command operation
026   */
027  void execute() throws CommandExecutionException;
028
029  /**
030   * Create a new command executor.
031   *
032   * @param callingContext
033   *          the context of the command execution
034   * @param commandLine
035   *          the parsed command line details
036   * @param function
037   *          a function that accepts a calling context and command line
038   *          information
039   * @return the executor instance
040   */
041  @NonNull
042  static ICommandExecutor using(
043      @NonNull CallingContext callingContext,
044      @NonNull CommandLine commandLine,
045      @NonNull ExecutionFunction function) {
046    return () -> function.execute(callingContext, commandLine);
047  }
048
049  /**
050   * This functional interface represents a method that is used to execute a
051   * command operation.
052   */
053  @FunctionalInterface
054  interface ExecutionFunction {
055    /**
056     * Execute a command operation.
057     *
058     * @param callingContext
059     *          the context of the command execution
060     * @param commandLine
061     *          the parsed command line details
062     * @throws CommandExecutionException
063     *           if an error occurred while executing the command operation
064     */
065    void execute(
066        @NonNull CallingContext callingContext,
067        @NonNull CommandLine commandLine) throws CommandExecutionException;
068  }
069}