View Javadoc
1   /*
2    * Copyright (c) 2001, 2011, 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.util;
27  
28  import java.util.*;
29  import com.sun.tools.javac.main.Option;
30  import static com.sun.tools.javac.main.Option.*;
31  
32  /** A table of all command-line options.
33   *  If an option has an argument, the option name is mapped to the argument.
34   *  If a set option has no argument, it is mapped to itself.
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 class Options {
42      private static final long serialVersionUID = 0;
43  
44      /** The context key for the options. */
45      public static final Context.Key<Options> optionsKey =
46          new Context.Key<Options>();
47  
48      private LinkedHashMap<String,String> values;
49  
50      /** Get the Options instance for this context. */
51      public static Options instance(Context context) {
52          Options instance = context.get(optionsKey);
53          if (instance == null)
54              instance = new Options(context);
55          return instance;
56      }
57  
58      protected Options(Context context) {
59  // DEBUGGING -- Use LinkedHashMap for reproducability
60          values = new LinkedHashMap<String,String>();
61          context.put(optionsKey, this);
62      }
63  
64      /**
65       * Get the value for an undocumented option.
66       */
67      public String get(String name) {
68          return values.get(name);
69      }
70  
71      /**
72       * Get the value for an option.
73       */
74      public String get(Option option) {
75          return values.get(option.text);
76      }
77  
78      /**
79       * Get the boolean value for an option, patterned after Boolean.getBoolean,
80       * essentially will return true, iff the value exists and is set to "true".
81       */
82      public boolean getBoolean(String name) {
83          return getBoolean(name, false);
84      }
85  
86      /**
87       * Get the boolean with a default value if the option is not set.
88       */
89      public boolean getBoolean(String name, boolean defaultValue) {
90          String value = get(name);
91          return (value == null) ? defaultValue : Boolean.parseBoolean(value);
92      }
93  
94      /**
95       * Check if the value for an undocumented option has been set.
96       */
97      public boolean isSet(String name) {
98          return (values.get(name) != null);
99      }
100 
101     /**
102      * Check if the value for an option has been set.
103      */
104     public boolean isSet(Option option) {
105         return (values.get(option.text) != null);
106     }
107 
108     /**
109      * Check if the value for a choice option has been set to a specific value.
110      */
111     public boolean isSet(Option option, String value) {
112         return (values.get(option.text + value) != null);
113     }
114 
115     /**
116      * Check if the value for an undocumented option has not been set.
117      */
118     public boolean isUnset(String name) {
119         return (values.get(name) == null);
120     }
121 
122     /**
123      * Check if the value for an option has not been set.
124      */
125     public boolean isUnset(Option option) {
126         return (values.get(option.text) == null);
127     }
128 
129     /**
130      * Check if the value for a choice option has not been set to a specific value.
131      */
132     public boolean isUnset(Option option, String value) {
133         return (values.get(option.text + value) == null);
134     }
135 
136     public void put(String name, String value) {
137         values.put(name, value);
138     }
139 
140     public void put(Option option, String value) {
141         values.put(option.text, value);
142     }
143 
144     public void putAll(Options options) {
145         values.putAll(options.values);
146     }
147 
148     public void remove(String name) {
149         values.remove(name);
150     }
151 
152     public Set<String> keySet() {
153         return values.keySet();
154     }
155 
156     public int size() {
157         return values.size();
158     }
159 
160     // light-weight notification mechanism
161 
162     private List<Runnable> listeners = List.nil();
163 
164     public void addListener(Runnable listener) {
165         listeners = listeners.prepend(listener);
166     }
167 
168     public void notifyListeners() {
169         for (Runnable r: listeners)
170             r.run();
171     }
172 
173     /** Check for a lint suboption. */
174     public boolean lint(String s) {
175         // return true if either the specific option is enabled, or
176         // they are all enabled without the specific one being
177         // disabled
178         return
179             isSet(XLINT_CUSTOM, s) ||
180             (isSet(XLINT) || isSet(XLINT_CUSTOM, "all")) &&
181                 isUnset(XLINT_CUSTOM, "-" + s);
182     }
183 }