001/*
002 * SPDX-FileCopyrightText: none
003 * SPDX-License-Identifier: CC0-1.0
004 */
005
006package gov.nist.secauto.metaschema.cli.processor.command;
007
008import java.util.List;
009import java.util.ServiceLoader;
010import java.util.ServiceLoader.Provider;
011import java.util.stream.Collectors;
012
013import edu.umd.cs.findbugs.annotations.NonNull;
014import nl.talsmasoftware.lazy4j.Lazy;
015
016public final class CommandService {
017  private static final Lazy<CommandService> INSTANCE = Lazy.lazy(() -> new CommandService());
018  @NonNull
019  private final ServiceLoader<ICommand> loader;
020
021  /**
022   * Get the singleton instance of the function service.
023   *
024   * @return the service instance
025   */
026  public static CommandService getInstance() {
027    return INSTANCE.get();
028  }
029
030  public CommandService() {
031    ServiceLoader<ICommand> loader = ServiceLoader.load(ICommand.class);
032    assert loader != null;
033    this.loader = loader;
034  }
035
036  /**
037   * Get the function service loader instance.
038   *
039   * @return the service loader instance.
040   */
041  @NonNull
042  private ServiceLoader<ICommand> getLoader() {
043    return loader;
044  }
045
046  @SuppressWarnings("null")
047  @NonNull
048  public List<ICommand> getCommands() {
049    return getLoader().stream()
050        .map(Provider<ICommand>::get)
051        .collect(Collectors.toUnmodifiableList());
052  }
053}