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  public interface ICommandExecutor {
21    /**
22     * Execute the command operation.
23     *
24     * @throws CommandExecutionException
25     *           if an error occurred while executing the command operation
26     */
27    void execute() throws CommandExecutionException;
28  
29    /**
30     * Create a new command executor.
31     *
32     * @param callingContext
33     *          the context of the command execution
34     * @param commandLine
35     *          the parsed command line details
36     * @param function
37     *          a function that accepts a calling context and command line
38     *          information
39     * @return the executor instance
40     */
41    @NonNull
42    static ICommandExecutor using(
43        @NonNull CallingContext callingContext,
44        @NonNull CommandLine commandLine,
45        @NonNull ExecutionFunction function) {
46      return () -> function.execute(callingContext, commandLine);
47    }
48  
49    /**
50     * This functional interface represents a method that is used to execute a
51     * command operation.
52     */
53    @FunctionalInterface
54    interface ExecutionFunction {
55      /**
56       * Execute a command operation.
57       *
58       * @param callingContext
59       *          the context of the command execution
60       * @param commandLine
61       *          the parsed command line details
62       * @throws CommandExecutionException
63       *           if an error occurred while executing the command operation
64       */
65      void execute(
66          @NonNull CallingContext callingContext,
67          @NonNull CommandLine commandLine) throws CommandExecutionException;
68    }
69  }