Profile.java revision 3890:05b91c7f6f9e
1/*
2 * Copyright (c) 2002, 2017, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package com.sun.tools.javac.jvm;
27
28import com.sun.tools.javac.util.Context;
29import com.sun.tools.javac.util.Options;
30import java.util.EnumSet;
31import java.util.Set;
32
33import static com.sun.tools.javac.main.Option.PROFILE;
34
35/** The target profile.
36 *
37 *  <p><b>This is NOT part of any supported API.
38 *  If you write code that depends on this, you do so at your own risk.
39 *  This code and its internal interfaces are subject to change or
40 *  deletion without notice.</b>
41 */
42public enum Profile {
43    COMPACT1("compact1", 1, Target.JDK1_8, Target.JDK1_9, Target.JDK1_10),
44    COMPACT2("compact2", 2, Target.JDK1_8, Target.JDK1_9, Target.JDK1_10),
45    COMPACT3("compact3", 3, Target.JDK1_8, Target.JDK1_9, Target.JDK1_10),
46
47    DEFAULT {
48        @Override
49        public boolean isValid(Target t) {
50            return true;
51        }
52    };
53
54    private static final Context.Key<Profile> profileKey = new Context.Key<>();
55
56    public static Profile instance(Context context) {
57        Profile instance = context.get(profileKey);
58        if (instance == null) {
59            Options options = Options.instance(context);
60            String profileString = options.get(PROFILE);
61            if (profileString != null) instance = lookup(profileString);
62            if (instance == null) instance = DEFAULT;
63            context.put(profileKey, instance);
64        }
65        return instance;
66    }
67
68    public final String name;
69    public final int value;
70    final Set<Target> targets;
71
72    Profile() {
73        name = null;
74        value = Integer.MAX_VALUE;
75        targets = null;
76    }
77
78    Profile(String name, int value, Target t, Target... targets) {
79        this.name = name;
80        this.value = value;
81        this.targets = EnumSet.of(t, targets);
82    }
83
84    public static Profile lookup(String name) {
85        // the set of values is small enough to do linear search
86        for (Profile p: values()) {
87            if (name.equals(p.name))
88                return p;
89        }
90        return null;
91    }
92
93    public static Profile lookup(int value) {
94        // the set of values is small enough to do linear search
95        for (Profile p: values()) {
96            if (value == p.value)
97                return p;
98        }
99        return null;
100    }
101
102    public boolean isValid(Target t) {
103        return targets.contains(t);
104    }
105}
106