1   /*
2    * SPDX-FileCopyrightText: none
3    * SPDX-License-Identifier: CC0-1.0
4    */
5   
6   package gov.nist.secauto.metaschema.cli.processor;
7   
8   import gov.nist.secauto.metaschema.core.util.CollectionUtil;
9   import gov.nist.secauto.metaschema.core.util.ObjectUtils;
10  
11  import java.util.Arrays;
12  import java.util.List;
13  
14  import edu.umd.cs.findbugs.annotations.NonNull;
15  
16  /**
17   * An {@link ExitStatus} implementation with an associated message.
18   * <p>
19   * The message arguments are stored in an unmodifiable list to ensure
20   * thread-safety and immutability.
21   */
22  public class MessageExitStatus
23      extends AbstractExitStatus {
24    private final List<Object> messageArguments;
25  
26    /**
27     * Construct a new {@link ExitStatus} based on an array of message arguments.
28     *
29     * @param code
30     *          the exit code to use.
31     * @param messageArguments
32     *          the arguments that can be passed to a formatted string to generate
33     *          the message
34     */
35    public MessageExitStatus(@NonNull ExitCode code, @NonNull Object... messageArguments) {
36      super(code);
37      this.messageArguments = CollectionUtil.unmodifiableList(
38          ObjectUtils.notNull(Arrays.asList(messageArguments)));
39    }
40  
41    @Override
42    public String getMessage() {
43      String format = lookupMessageForCode(getExitCode());
44      return String.format(format, messageArguments.toArray());
45    }
46  
47    private String lookupMessageForCode(@SuppressWarnings("unused") ExitCode ignoredExitCode) {
48      // TODO: add message bundle support
49      StringBuilder builder = new StringBuilder();
50      // builder.append(getExitCode()).append(":");
51      for (int index = 1; index <= messageArguments.size(); index++) {
52        if (index > 1) {
53          builder.append(' ');
54        }
55        builder.append("%s");
56        // builder.append(index);
57      }
58      return builder.toString();
59    }
60  }