1 /*
   2  * Copyright (c) 2002, 2019, 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 
  26 package com.sun.tools.javac.jvm;
  27 
  28 import com.sun.tools.javac.util.Context;
  29 import com.sun.tools.javac.util.Options;
  30 import java.util.EnumSet;
  31 import java.util.Set;
  32 
  33 import 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  */
  42 public enum Profile {
  43     COMPACT1("compact1", 1, Target.JDK1_8, Target.JDK1_9, Target.JDK1_10,
  44              Target.JDK1_11, Target.JDK1_12, Target.JDK1_13, Target.JDK1_14, Target.JDK1_15),
  45     COMPACT2("compact2", 2, Target.JDK1_8, Target.JDK1_9, Target.JDK1_10,
  46              Target.JDK1_11, Target.JDK1_12, Target.JDK1_13, Target.JDK1_14, Target.JDK1_15),
  47     COMPACT3("compact3", 3, Target.JDK1_8, Target.JDK1_9, Target.JDK1_10,
  48              Target.JDK1_11, Target.JDK1_12, Target.JDK1_13, Target.JDK1_14, Target.JDK1_15),
  49 
  50     DEFAULT {
  51         @Override
  52         public boolean isValid(Target t) {
  53             return true;
  54         }
  55     };
  56 
  57     private static final Context.Key<Profile> profileKey = new Context.Key<>();
  58 
  59     public static Profile instance(Context context) {
  60         Profile instance = context.get(profileKey);
  61         if (instance == null) {
  62             Options options = Options.instance(context);
  63             String profileString = options.get(PROFILE);
  64             if (profileString != null) instance = lookup(profileString);
  65             if (instance == null) instance = DEFAULT;
  66             context.put(profileKey, instance);
  67         }
  68         return instance;
  69     }
  70 
  71     public final String name;
  72     public final int value;
  73     final Set<Target> targets;
  74 
  75     Profile() {
  76         name = null;
  77         value = Integer.MAX_VALUE;
  78         targets = null;
  79     }
  80 
  81     Profile(String name, int value, Target t, Target... targets) {
  82         this.name = name;
  83         this.value = value;
  84         this.targets = EnumSet.of(t, targets);
  85     }
  86 
  87     public static Profile lookup(String name) {
  88         // the set of values is small enough to do linear search
  89         for (Profile p: values()) {
  90             if (name.equals(p.name))
  91                 return p;
  92         }
  93         return null;
  94     }
  95 
  96     public static Profile lookup(int value) {
  97         // the set of values is small enough to do linear search
  98         for (Profile p: values()) {
  99             if (value == p.value)
 100                 return p;
 101         }
 102         return null;
 103     }
 104 
 105     public boolean isValid(Target t) {
 106         return targets.contains(t);
 107     }
 108 }