Profile.java revision 2571:10fc81ac75b4
1/*
2 * Copyright (c) 2002, 2013, 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 */
25package com.sun.tools.javac.jvm;
26
27import com.sun.tools.javac.util.Context;
28import com.sun.tools.javac.util.Options;
29import java.util.EnumSet;
30import java.util.Set;
31
32import static com.sun.tools.javac.main.Option.PROFILE;
33
34/** The target profile.
35 *
36 *  <p><b>This is NOT part of any supported API.
37 *  If you write code that depends on this, you do so at your own risk.
38 *  This code and its internal interfaces are subject to change or
39 *  deletion without notice.</b>
40 */
41public enum Profile {
42    COMPACT1("compact1", 1, Target.JDK1_8, Target.JDK1_9),
43    COMPACT2("compact2", 2, Target.JDK1_8, Target.JDK1_9),
44    COMPACT3("compact3", 3, Target.JDK1_8, Target.JDK1_9),
45
46    DEFAULT {
47        @Override
48        public boolean isValid(Target t) {
49            return true;
50        }
51    };
52
53    private static final Context.Key<Profile> profileKey = new Context.Key<>();
54
55    public static Profile instance(Context context) {
56        Profile instance = context.get(profileKey);
57        if (instance == null) {
58            Options options = Options.instance(context);
59            String profileString = options.get(PROFILE);
60            if (profileString != null) instance = lookup(profileString);
61            if (instance == null) instance = DEFAULT;
62            context.put(profileKey, instance);
63        }
64        return instance;
65    }
66
67    public final String name;
68    public final int value;
69    final Set<Target> targets;
70
71    Profile() {
72        name = null;
73        value = Integer.MAX_VALUE;
74        targets = null;
75    }
76
77    Profile(String name, int value, Target t, Target... targets) {
78        this.name = name;
79        this.value = value;
80        this.targets = EnumSet.of(t, targets);
81    }
82
83    public static Profile lookup(String name) {
84        // the set of values is small enough to do linear search
85        for (Profile p: values()) {
86            if (name.equals(p.name))
87                return p;
88        }
89        return null;
90    }
91
92    public static Profile lookup(int value) {
93        // the set of values is small enough to do linear search
94        for (Profile p: values()) {
95            if (value == p.value)
96                return p;
97        }
98        return null;
99    }
100
101    public boolean isValid(Target t) {
102        return targets.contains(t);
103    }
104}
105