001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2018 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
033    /** Public access modifier. */
034    PUBLIC,
035    /** Protected access modifier. */
036    PROTECTED,
037    /** Package access modifier. */
038    PACKAGE,
039    /** Private access modifier. */
040    PRIVATE;
041
042    @Override
043    public String toString() {
044        return getName();
045    }
046
047    private String getName() {
048        return name().toLowerCase(Locale.ENGLISH);
049    }
050
051    /**
052     * Factory method which returns an AccessModifier instance that corresponds to the
053     * given access modifier name represented as a {@link String}.
054     * The access modifier name can be formatted both as lower case or upper case string.
055     * For example, passing PACKAGE or package as a modifier name
056     * will return {@link AccessModifier#PACKAGE}.
057     *
058     * @param modifierName access modifier name represented as a {@link String}.
059     * @return the AccessModifier associated with given access modifier name.
060     */
061    public static AccessModifier getInstance(String modifierName) {
062        return valueOf(AccessModifier.class, modifierName.trim().toUpperCase(Locale.ENGLISH));
063    }
064
065}