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