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  */
  25 package com.sun.tools.javac.jvm;
  26 
  27 import com.sun.tools.javac.util.Context;
  28 import com.sun.tools.javac.util.Options;
  29 import java.util.EnumSet;
  30 import java.util.Set;
  31 
  32 import 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  */
  41 public enum Profile {
  42     COMPACT1("compact1", 1, Target.JDK1_8),
  43     COMPACT2("compact2", 2, Target.JDK1_8),
  44     COMPACT3("compact3", 3, Target.JDK1_8),
  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 =
  54         new Context.Key<Profile>();
  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 }