001/*
002 * SPDX-FileCopyrightText: none
003 * SPDX-License-Identifier: CC0-1.0
004 */
005
006package gov.nist.secauto.metaschema.cli.processor;
007
008import java.util.Arrays;
009import java.util.Collections;
010import java.util.List;
011
012import edu.umd.cs.findbugs.annotations.NonNull;
013
014public class MessageExitStatus
015    extends AbstractExitStatus {
016  private final List<Object> messageArguments;
017
018  /**
019   * Construct a new {@link ExitStatus} based on an array of message arguments.
020   *
021   * @param code
022   *          the exit code to use.
023   * @param messageArguments
024   *          the arguments that can be passed to a formatted string to generate
025   *          the message
026   */
027  public MessageExitStatus(@NonNull ExitCode code, @NonNull Object... messageArguments) {
028    super(code);
029    if (messageArguments.length == 0) {
030      this.messageArguments = Collections.emptyList();
031    } else {
032      this.messageArguments = Arrays.asList(messageArguments);
033    }
034  }
035
036  @Override
037  public String getMessage() {
038    String format = lookupMessageForCode(getExitCode());
039    return String.format(format, messageArguments.toArray());
040  }
041
042  private String lookupMessageForCode(@SuppressWarnings("unused") ExitCode ignoredExitCode) {
043    // TODO: add message bundle support
044    StringBuilder builder = new StringBuilder();
045    // builder.append(getExitCode()).append(":");
046    for (int index = 1; index <= messageArguments.size(); index++) {
047      if (index > 1) {
048        builder.append(' ');
049      }
050      builder.append("%s");
051      // builder.append(index);
052    }
053    return builder.toString();
054  }
055}