001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2017 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 JLS:
027 * https://docs.oracle.com/javase/specs/jls/se8/html/jls-6.html#jls-6.6
028 *
029 * @author Andrei Selkin
030 */
031public enum AccessModifier {
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    private String getName() {
047        return name().toLowerCase(Locale.ENGLISH);
048    }
049
050    /**
051     * Factory method which returns an AccessModifier instance that corresponds to the
052     * given access modifier name represented as a {@link String}.
053     * The access modifier name can be formatted both as lower case or upper case string.
054     * For example, passing PACKAGE or package as a modifier name
055     * will return {@link AccessModifier#PACKAGE}.
056     *
057     * @param modifierName access modifier name represented as a {@link String}.
058     * @return the AccessModifier associated with given access modifier name.
059     */
060    public static AccessModifier getInstance(String modifierName) {
061        return valueOf(AccessModifier.class, modifierName.trim().toUpperCase(Locale.ENGLISH));
062    }
063}