1/*
2 * Copyright 2016 Google Inc. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.google.turbine.model;
18
19/** The visibility of a declaration. */
20public enum TurbineVisibility {
21  PUBLIC(3, TurbineFlag.ACC_PUBLIC),
22  PROTECTED(2, TurbineFlag.ACC_PROTECTED),
23  PACKAGE(1, 0),
24  PRIVATE(0, TurbineFlag.ACC_PRIVATE);
25
26  private final int level;
27  private final int flag;
28
29  TurbineVisibility(int level, int flag) {
30    this.level = level;
31    this.flag = flag;
32  }
33
34  public boolean moreVisible(TurbineVisibility other) {
35    return level > other.level;
36  }
37
38  public int flag() {
39    return flag;
40  }
41
42  public static TurbineVisibility fromAccess(int access) {
43    switch (access
44        & (TurbineFlag.ACC_PUBLIC | TurbineFlag.ACC_PRIVATE | TurbineFlag.ACC_PROTECTED)) {
45      case TurbineFlag.ACC_PUBLIC:
46        return PUBLIC;
47      case TurbineFlag.ACC_PRIVATE:
48        return PRIVATE;
49      case TurbineFlag.ACC_PROTECTED:
50        return PROTECTED;
51      case 0:
52        return PACKAGE;
53      default:
54        throw new AssertionError(String.format("0x%x", access));
55    }
56  }
57
58  public int setAccess(int access) {
59    access &= ~(TurbineFlag.ACC_PUBLIC | TurbineFlag.ACC_PRIVATE | TurbineFlag.ACC_PROTECTED);
60    access |= flag();
61    return access;
62  }
63}
64