OptionalFlag.java

/*
 * SPDX-FileCopyrightText: 2025 kaumei.io
 * SPDX-License-Identifier: Apache-2.0
 */
package io.kaumei.jdbc.anno.model;

import org.jspecify.annotations.Nullable;

public enum OptionalFlag {
    UNSPECIFIED("unspecified"),
    NULLABLE("nullable"),
    NON_NULL("non-null"),
    OPTIONAL_TYPE("Optional<?>");

    private final String text;

    OptionalFlag(String text) {
        this.text = text;
    }

    @Override
    public String toString() {
        return this.text;
    }
    // ------------------------------------------------------------------------

    public boolean isNullable() {
        return this == NULLABLE;
    }

    public boolean isOptionalType() {
        return this == OPTIONAL_TYPE;
    }

    public boolean isNonNull() {
        return this == NON_NULL;
    }

    public boolean isAssignableTo(OptionalFlag other) {
        return other != NON_NULL || this == NON_NULL;
    }

    // ------------------------------------------------------------------------

    @SuppressWarnings("BooleanMethodIsAlwaysInverted")
    public boolean isNullableOrUnspecified() {
        return this == NULLABLE || this == UNSPECIFIED;
    }

    @SuppressWarnings("BooleanMethodIsAlwaysInverted")
    public boolean isNonNullOrUnspecified() {
        return this == NON_NULL || this == UNSPECIFIED;
    }

    public @Nullable String checkNonNullOrUnspecified() {
        return this == NON_NULL || this == UNSPECIFIED
                ? null
                : "nullness '" + this + "' not supported. Expected are one of 'non-null' or 'unspecified'.";
    }

}