001    /*
002     *  Copyright the original author or authors.
003     *
004     *  Licensed under the Apache License, Version 2.0 (the "License");
005     *  you may not use this file except in compliance with the License.
006     *  You may obtain a copy of the License at
007     *
008     *      http://www.apache.org/licenses/LICENSE-2.0
009     *
010     *  Unless required by applicable law or agreed to in writing, software
011     *  distributed under the License is distributed on an "AS IS" BASIS,
012     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     *  See the License for the specific language governing permissions and
014     *  limitations under the License.
015     */
016    package org.apache.commons.privilizer.weave;
017    
018    import java.lang.reflect.Modifier;
019    import java.util.EnumSet;
020    import java.util.Locale;
021    
022    public enum AccessLevel {
023        PUBLIC(Modifier.PUBLIC), PROTECTED(Modifier.PROTECTED), PACKAGE(0), PRIVATE(Modifier.PRIVATE);
024    
025        private final int flag;
026    
027        private AccessLevel(int flag) {
028            this.flag = flag;
029        }
030    
031        public static AccessLevel of(int mod) {
032            if (Modifier.isPublic(mod)) {
033                return PUBLIC;
034            }
035            if (Modifier.isProtected(mod)) {
036                return PROTECTED;
037            }
038            if (Modifier.isPrivate(mod)) {
039                return PRIVATE;
040            }
041            return PACKAGE;
042        }
043    
044        public int merge(int mod) {
045            int remove = 0;
046            for (AccessLevel accessLevel : EnumSet.complementOf(EnumSet.of(this))) {
047                remove |= accessLevel.flag;
048            }
049            return mod & ~remove | flag;
050        }
051    
052        @Override
053        public String toString() {
054            return name().toLowerCase(Locale.US);
055        }
056    }