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 java.util.List;
9   import java.util.ServiceLoader;
10  import java.util.ServiceLoader.Provider;
11  import java.util.stream.Collectors;
12  
13  import edu.umd.cs.findbugs.annotations.NonNull;
14  import nl.talsmasoftware.lazy4j.Lazy;
15  
16  public final class CommandService {
17    private static final Lazy<CommandService> INSTANCE = Lazy.lazy(() -> new CommandService());
18    @NonNull
19    private final ServiceLoader<ICommand> loader;
20  
21    /**
22     * Get the singleton instance of the function service.
23     *
24     * @return the service instance
25     */
26    public static CommandService getInstance() {
27      return INSTANCE.get();
28    }
29  
30    public CommandService() {
31      ServiceLoader<ICommand> loader = ServiceLoader.load(ICommand.class);
32      assert loader != null;
33      this.loader = loader;
34    }
35  
36    /**
37     * Get the function service loader instance.
38     *
39     * @return the service loader instance.
40     */
41    @NonNull
42    private ServiceLoader<ICommand> getLoader() {
43      return loader;
44    }
45  
46    @SuppressWarnings("null")
47    @NonNull
48    public List<ICommand> getCommands() {
49      return getLoader().stream()
50          .map(Provider<ICommand>::get)
51          .collect(Collectors.toUnmodifiableList());
52    }
53  }