001package gudusoft.gsqlparser.lineage.v2.identity;
002
003import gudusoft.gsqlparser.lineage.v2.SqllrException;
004
005import java.util.regex.Pattern;
006
007/**
008 * A registry code: UPPER_SNAKE_CASE ASCII matching {@code [A-Z][A-Z0-9_]*}, framed with type byte
009 * {@code 0x05} ({@code identity-v1.md}, Section 3.3). A code and a string with the same text frame
010 * differently.
011 *
012 * @since 4.2.17
013 */
014public final class Code {
015
016    private static final Pattern CODE = Pattern.compile("[A-Z][A-Z0-9_]*");
017
018    private final String value;
019
020    private Code(String value) {
021        this.value = value;
022    }
023
024    /**
025     * The code with this text.
026     *
027     * @throws SqllrException {@code ID_INVALID_CODE} unless {@code value} is a string of the code form
028     */
029    public static Code of(Object value) {
030        if (!(value instanceof String) || !CODE.matcher((String) value).matches()) {
031            throw new SqllrException("ID_INVALID_CODE", String.valueOf(value));
032        }
033        return new Code((String) value);
034    }
035
036    public String value() {
037        return value;
038    }
039
040    @Override
041    public boolean equals(Object other) {
042        return other instanceof Code && ((Code) other).value.equals(value);
043    }
044
045    @Override
046    public int hashCode() {
047        return value.hashCode() * 31 + 5;
048    }
049
050    @Override
051    public String toString() {
052        return "Code(" + value + ")";
053    }
054}