001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2024 the original author or authors.
004//
005// This library is free software; you can redistribute it and/or
006// modify it under the terms of the GNU Lesser General Public
007// License as published by the Free Software Foundation; either
008// version 2.1 of the License, or (at your option) any later version.
009//
010// This library is distributed in the hope that it will be useful,
011// but WITHOUT ANY WARRANTY; without even the implied warranty of
012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013// Lesser General Public License for more details.
014//
015// You should have received a copy of the GNU Lesser General Public
016// License along with this library; if not, write to the Free Software
017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
018///////////////////////////////////////////////////////////////////////////////////////////////
019
020package com.puppycrawl.tools.checkstyle.api;
021
022import java.util.Locale;
023
024/**
025 * Represents a Java visibility scope.
026 *
027 */
028public enum Scope {
029
030    /** Nothing scope. */
031    NOTHING,
032    /** Public scope. */
033    PUBLIC,
034    /** Protected scope. */
035    PROTECTED,
036    /** Package or default scope. */
037    PACKAGE,
038    /** Private scope. */
039    PRIVATE,
040    /** Anonymous inner scope. */
041    ANONINNER;
042
043    @Override
044    public String toString() {
045        return getName();
046    }
047
048    /**
049     * Returns name of severity level.
050     *
051     * @return the name of this severity level.
052     */
053    public String getName() {
054        return name().toLowerCase(Locale.ENGLISH);
055    }
056
057    /**
058     * Checks if this scope is a subscope of another scope.
059     * Example: PUBLIC is a subscope of PRIVATE.
060     *
061     * @param scope a {@code Scope} value
062     * @return if {@code this} is a subscope of {@code scope}.
063     */
064    public boolean isIn(Scope scope) {
065        return compareTo(scope) <= 0;
066    }
067
068    /**
069     * Scope factory method.
070     *
071     * @param scopeName scope name, such as "nothing", "public", etc.
072     * @return the {@code Scope} associated with {@code scopeName}
073     */
074    public static Scope getInstance(String scopeName) {
075        return valueOf(Scope.class, scopeName.trim().toUpperCase(Locale.ENGLISH));
076    }
077
078}