Visibility.java

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

import org.jspecify.annotations.Nullable;

import javax.lang.model.element.PackageElement;

public class Visibility {

    static Visibility packageAccess(PackageElement pkg) {
        return new Visibility(false, pkg);
    }

    static final Visibility NO_VISIBLE = new Visibility(true, null);

    public static final Visibility PUBLIC_VISIBILITY = new Visibility(false, null);

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

    private final boolean notVisible;
    private final @Nullable PackageElement pkg;

    private Visibility(boolean notVisible, @Nullable PackageElement pkg) {
        this.notVisible = notVisible;
        this.pkg = pkg;
    }

    public Visibility and(Visibility other) {
        if (this.notVisible || other.notVisible) {
            return NO_VISIBLE;
        } else if (this.pkg == null) {
            return other;
        } else if (other.pkg == null || pkg.equals(other.pkg)) {
            return this;
        }
        return NO_VISIBLE;
    }

    public boolean isPublic() {
        return !this.notVisible && this.pkg == null;
    }

    public boolean isVisible(PackageElement pkg) {
        return this.pkg != null && this.pkg.equals(pkg);
    }

    public boolean isNoVisibility() {
        return this.notVisible && this.pkg == null;
    }

    @Override
    public String toString() {
        if (this.notVisible) {
            return "notVisible";
        } else if (this.pkg != null) {
            return "package";
        }
        return "public";
    }

}