001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2020 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     * @return the name of this severity level.
051     */
052    public String getName() {
053        return name().toLowerCase(Locale.ENGLISH);
054    }
055
056    /**
057     * Checks if this scope is a subscope of another scope.
058     * Example: PUBLIC is a subscope of PRIVATE.
059     *
060     * @param scope a {@code Scope} value
061     * @return if {@code this} is a subscope of {@code scope}.
062     */
063    public boolean isIn(Scope scope) {
064        return compareTo(scope) <= 0;
065    }
066
067    /**
068     * Scope factory method.
069     *
070     * @param scopeName scope name, such as "nothing", "public", etc.
071     * @return the {@code Scope} associated with {@code scopeName}
072     */
073    public static Scope getInstance(String scopeName) {
074        return valueOf(Scope.class, scopeName.trim().toUpperCase(Locale.ENGLISH));
075    }
076
077}