1   /*
2    * SPDX-FileCopyrightText: none
3    * SPDX-License-Identifier: CC0-1.0
4    */
5   
6   package dev.metaschema.core.metapath;
7   
8   import dev.metaschema.core.metapath.impl.ErrorCodeImpl;
9   import dev.metaschema.core.util.ObjectUtils;
10  import edu.umd.cs.findbugs.annotations.NonNull;
11  
12  /**
13   * Provides an error code that identifies the type of message.
14   * <p>
15   * Implementations of this interface are expected to be immutable.
16   */
17  public interface IErrorCode {
18    /**
19     * Create a new error code with the provided prefix and code value.
20     *
21     * @param prefix
22     *          the error code prefix
23     * @param code
24     *          the error code value
25     * @return a new error code instance
26     */
27    @NonNull
28    static IErrorCode of(@NonNull String prefix, int code) {
29      return new ErrorCodeImpl(prefix, code);
30    }
31  
32    /**
33     * Get the error code prefix, which indicates what type of error it is.
34     *
35     * @return the error code prefix
36     */
37    @NonNull
38    String getPrefix();
39  
40    /**
41     * Get the error code value.
42     *
43     * @return the error code value
44     */
45    int getCode();
46  
47    /**
48     * Get a combination of the error code family and value.
49     *
50     * @return the full error code.
51     */
52    @NonNull
53    default String getCodeAsString() {
54      return ObjectUtils.notNull(String.format("%s%04d", getPrefix(), getCode()));
55    }
56  }