1/*
2 * Copyright (C) 2017 The Android Open Source Project
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 libcore.heapmetrics;
18
19import com.android.ahat.heapdump.AhatInstance;
20
21/**
22 * An enumeration of the different ways that an object could be reachable.
23 */
24enum Reachability {
25
26    /** Objects that are rooted. */
27    ROOTED("Rooted"),
28
29    /** Objects that are strongly reachable but not rooted. */
30    NON_ROOTED_STRONG("NonRootedStrong"),
31
32    /** Objects that are weakly reachable (only through soft/weak/phantom/finalizer references). */
33    WEAK("Weak"),
34
35    /** Objects that are unreachable. */
36    UNREACHABLE("Unreachable");
37
38    private final String metricSuffix;
39
40    Reachability(String metricSuffix) {
41        this.metricSuffix = metricSuffix;
42    }
43
44    /** Returns a name for a metric combining the given prefix with a reachability suffix. */
45    String metricName(String metricPrefix) {
46        return metricPrefix + metricSuffix;
47    }
48
49    /** Returns the reachability of the given object. */
50    static final Reachability ofInstance(AhatInstance instance) {
51        if (instance.isRoot()) {
52            return ROOTED;
53        } else if (instance.isStronglyReachable()) {
54            return NON_ROOTED_STRONG;
55        } else if (instance.isWeaklyReachable()) {
56            return WEAK;
57        } else if (instance.isUnreachable()) {
58            return UNREACHABLE;
59        }
60        throw new AssertionError("Impossible reachability data for instance " + instance);
61    }
62}
63