1   /*
2    * SPDX-FileCopyrightText: none
3    * SPDX-License-Identifier: CC0-1.0
4    */
5   
6   package gov.nist.secauto.metaschema.cli.processor.command;
7   
8   import gov.nist.secauto.metaschema.cli.processor.CLIProcessor.CallingContext;
9   
10  import org.apache.commons.cli.CommandLine;
11  
12  import edu.umd.cs.findbugs.annotations.NonNull;
13  
14  /**
15   * An abstract base class that implements the {@link ICommandExecutor}
16   * interface, providing common functionality for command execution
17   * implementations. Concrete subclasses must implement the {@link #execute()}
18   * method to define specific command behavior.
19   */
20  @FunctionalInterface
21  public interface ICommandExecutor {
22    /**
23     * Execute the command operation.
24     *
25     * @throws CommandExecutionException
26     *           if an error occurred while executing the command operation
27     */
28    void execute() throws CommandExecutionException;
29  
30    /**
31     * Create a new command executor.
32     *
33     * @param callingContext
34     *          the context of the command execution
35     * @param commandLine
36     *          the parsed command line details
37     * @param function
38     *          a function that accepts a calling context and command line
39     *          information
40     * @return the executor instance
41     */
42    @NonNull
43    static ICommandExecutor using(
44        @NonNull CallingContext callingContext,
45        @NonNull CommandLine commandLine,
46        @NonNull ExecutionFunction function) {
47      return () -> function.execute(callingContext, commandLine);
48    }
49  
50    /**
51     * This functional interface represents a method that is used to execute a
52     * command operation.
53     */
54    @FunctionalInterface
55    interface ExecutionFunction {
56      /**
57       * Execute a command operation.
58       *
59       * @param callingContext
60       *          the context of the command execution
61       * @param commandLine
62       *          the parsed command line details
63       * @throws CommandExecutionException
64       *           if an error occurred while executing the command operation
65       */
66      void execute(
67          @NonNull CallingContext callingContext,
68          @NonNull CommandLine commandLine) throws CommandExecutionException;
69    }
70  }