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.checks.naming;
021
022import java.util.Locale;
023
024/**
025 * This enum represents access modifiers.
026 * Access modifiers names are taken from
027 * <a href="https://docs.oracle.com/javase/specs/jls/se8/html/jls-6.html#jls-6.6">JLS</a>
028 *
029 */
030public enum AccessModifierOption {
031
032    /** Public access modifier. */
033    PUBLIC,
034    /** Protected access modifier. */
035    PROTECTED,
036    /** Package access modifier. */
037    PACKAGE,
038    /** Private access modifier. */
039    PRIVATE;
040
041    @Override
042    public String toString() {
043        return getName();
044    }
045
046    /**
047     * Returns the access modifier name.
048     *
049     * @return the access modifier name
050     */
051    private String getName() {
052        return name().toLowerCase(Locale.ENGLISH);
053    }
054
055    /**
056     * Factory method which returns an AccessModifier instance that corresponds to the
057     * given access modifier name represented as a {@link String}.
058     * The access modifier name can be formatted both as lower case or upper case string.
059     * For example, passing PACKAGE or package as a modifier name
060     * will return {@link AccessModifierOption#PACKAGE}.
061     *
062     * @param modifierName access modifier name represented as a {@link String}.
063     * @return the AccessModifier associated with given access modifier name.
064     */
065    public static AccessModifierOption getInstance(String modifierName) {
066        return valueOf(AccessModifierOption.class, modifierName.trim().toUpperCase(Locale.ENGLISH));
067    }
068
069}