Options.java revision 2571:10fc81ac75b4
1230557Sjimharris/*
2230557Sjimharris * Copyright (c) 2001, 2011, Oracle and/or its affiliates. All rights reserved.
3230557Sjimharris * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4230557Sjimharris *
5230557Sjimharris * This code is free software; you can redistribute it and/or modify it
6230557Sjimharris * under the terms of the GNU General Public License version 2 only, as
7230557Sjimharris * published by the Free Software Foundation.  Oracle designates this
8230557Sjimharris * particular file as subject to the "Classpath" exception as provided
9230557Sjimharris * by Oracle in the LICENSE file that accompanied this code.
10230557Sjimharris *
11230557Sjimharris * This code is distributed in the hope that it will be useful, but WITHOUT
12230557Sjimharris * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13230557Sjimharris * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14230557Sjimharris * version 2 for more details (a copy is included in the LICENSE file that
15230557Sjimharris * accompanied this code).
16230557Sjimharris *
17230557Sjimharris * You should have received a copy of the GNU General Public License version
18230557Sjimharris * 2 along with this work; if not, write to the Free Software Foundation,
19230557Sjimharris * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20230557Sjimharris *
21230557Sjimharris * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22230557Sjimharris * or visit www.oracle.com if you need additional information or have any
23230557Sjimharris * questions.
24230557Sjimharris */
25230557Sjimharris
26230557Sjimharrispackage com.sun.tools.javac.util;
27230557Sjimharris
28230557Sjimharrisimport java.util.*;
29230557Sjimharrisimport com.sun.tools.javac.main.Option;
30230557Sjimharrisimport static com.sun.tools.javac.main.Option.*;
31230557Sjimharris
32230557Sjimharris/** A table of all command-line options.
33230557Sjimharris *  If an option has an argument, the option name is mapped to the argument.
34230557Sjimharris *  If a set option has no argument, it is mapped to itself.
35230557Sjimharris *
36230557Sjimharris *  <p><b>This is NOT part of any supported API.
37230557Sjimharris *  If you write code that depends on this, you do so at your own risk.
38230557Sjimharris *  This code and its internal interfaces are subject to change or
39230557Sjimharris *  deletion without notice.</b>
40230557Sjimharris */
41230557Sjimharrispublic class Options {
42230557Sjimharris    private static final long serialVersionUID = 0;
43230557Sjimharris
44230557Sjimharris    /** The context key for the options. */
45230557Sjimharris    public static final Context.Key<Options> optionsKey = new Context.Key<>();
46230557Sjimharris
47230557Sjimharris    private LinkedHashMap<String,String> values;
48230557Sjimharris
49230557Sjimharris    /** Get the Options instance for this context. */
50230557Sjimharris    public static Options instance(Context context) {
51230557Sjimharris        Options instance = context.get(optionsKey);
52230557Sjimharris        if (instance == null)
53230557Sjimharris            instance = new Options(context);
54230557Sjimharris        return instance;
55230557Sjimharris    }
56230557Sjimharris
57230557Sjimharris    protected Options(Context context) {
58230557Sjimharris// DEBUGGING -- Use LinkedHashMap for reproducability
59230557Sjimharris        values = new LinkedHashMap<>();
60230557Sjimharris        context.put(optionsKey, this);
61230557Sjimharris    }
62230557Sjimharris
63230557Sjimharris    /**
64230557Sjimharris     * Get the value for an undocumented option.
65230557Sjimharris     */
66230557Sjimharris    public String get(String name) {
67230557Sjimharris        return values.get(name);
68230557Sjimharris    }
69230557Sjimharris
70230557Sjimharris    /**
71230557Sjimharris     * Get the value for an option.
72230557Sjimharris     */
73230557Sjimharris    public String get(Option option) {
74230557Sjimharris        return values.get(option.text);
75230557Sjimharris    }
76230557Sjimharris
77230557Sjimharris    /**
78230557Sjimharris     * Get the boolean value for an option, patterned after Boolean.getBoolean,
79230557Sjimharris     * essentially will return true, iff the value exists and is set to "true".
80230557Sjimharris     */
81230557Sjimharris    public boolean getBoolean(String name) {
82230557Sjimharris        return getBoolean(name, false);
83230557Sjimharris    }
84230557Sjimharris
85230557Sjimharris    /**
86230557Sjimharris     * Get the boolean with a default value if the option is not set.
87230557Sjimharris     */
88230557Sjimharris    public boolean getBoolean(String name, boolean defaultValue) {
89230557Sjimharris        String value = get(name);
90230557Sjimharris        return (value == null) ? defaultValue : Boolean.parseBoolean(value);
91230557Sjimharris    }
92230557Sjimharris
93230557Sjimharris    /**
94230557Sjimharris     * Check if the value for an undocumented option has been set.
95230557Sjimharris     */
96230557Sjimharris    public boolean isSet(String name) {
97230557Sjimharris        return (values.get(name) != null);
98230557Sjimharris    }
99230557Sjimharris
100230557Sjimharris    /**
101230557Sjimharris     * Check if the value for an option has been set.
102230557Sjimharris     */
103230557Sjimharris    public boolean isSet(Option option) {
104230557Sjimharris        return (values.get(option.text) != null);
105230557Sjimharris    }
106230557Sjimharris
107230557Sjimharris    /**
108230557Sjimharris     * Check if the value for a choice option has been set to a specific value.
109230557Sjimharris     */
110230557Sjimharris    public boolean isSet(Option option, String value) {
111230557Sjimharris        return (values.get(option.text + value) != null);
112230557Sjimharris    }
113230557Sjimharris
114230557Sjimharris    /**
115230557Sjimharris     * Check if the value for an undocumented option has not been set.
116230557Sjimharris     */
117230557Sjimharris    public boolean isUnset(String name) {
118230557Sjimharris        return (values.get(name) == null);
119230557Sjimharris    }
120230557Sjimharris
121230557Sjimharris    /**
122230557Sjimharris     * Check if the value for an option has not been set.
123230557Sjimharris     */
124230557Sjimharris    public boolean isUnset(Option option) {
125230557Sjimharris        return (values.get(option.text) == null);
126230557Sjimharris    }
127230557Sjimharris
128230557Sjimharris    /**
129230557Sjimharris     * Check if the value for a choice option has not been set to a specific value.
130230557Sjimharris     */
131230557Sjimharris    public boolean isUnset(Option option, String value) {
132230557Sjimharris        return (values.get(option.text + value) == null);
133230557Sjimharris    }
134230557Sjimharris
135230557Sjimharris    public void put(String name, String value) {
136230557Sjimharris        values.put(name, value);
137230557Sjimharris    }
138230557Sjimharris
139230557Sjimharris    public void put(Option option, String value) {
140230557Sjimharris        values.put(option.text, value);
141230557Sjimharris    }
142230557Sjimharris
143230557Sjimharris    public void putAll(Options options) {
144230557Sjimharris        values.putAll(options.values);
145230557Sjimharris    }
146230557Sjimharris
147230557Sjimharris    public void remove(String name) {
148230557Sjimharris        values.remove(name);
149230557Sjimharris    }
150230557Sjimharris
151230557Sjimharris    public Set<String> keySet() {
152230557Sjimharris        return values.keySet();
153230557Sjimharris    }
154230557Sjimharris
155230557Sjimharris    public int size() {
156230557Sjimharris        return values.size();
157230557Sjimharris    }
158230557Sjimharris
159230557Sjimharris    // light-weight notification mechanism
160230557Sjimharris
161230557Sjimharris    private List<Runnable> listeners = List.nil();
162230557Sjimharris
163230557Sjimharris    public void addListener(Runnable listener) {
164230557Sjimharris        listeners = listeners.prepend(listener);
165230557Sjimharris    }
166230557Sjimharris
167230557Sjimharris    public void notifyListeners() {
168230557Sjimharris        for (Runnable r: listeners)
169230557Sjimharris            r.run();
170230557Sjimharris    }
171230557Sjimharris
172230557Sjimharris    /** Check for a lint suboption. */
173230557Sjimharris    public boolean lint(String s) {
174230557Sjimharris        // return true if either the specific option is enabled, or
175230557Sjimharris        // they are all enabled without the specific one being
176230557Sjimharris        // disabled
177230557Sjimharris        return
178230557Sjimharris            isSet(XLINT_CUSTOM, s) ||
179230557Sjimharris            (isSet(XLINT) || isSet(XLINT_CUSTOM, "all")) &&
180230557Sjimharris                isUnset(XLINT_CUSTOM, "-" + s);
181230557Sjimharris    }
182230557Sjimharris}
183230557Sjimharris