duke@1: /*
ohair@962: * Copyright (c) 2001, 2011, Oracle and/or its affiliates. All rights reserved.
duke@1: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@1: *
duke@1: * This code is free software; you can redistribute it and/or modify it
duke@1: * under the terms of the GNU General Public License version 2 only, as
ohair@554: * published by the Free Software Foundation. Oracle designates this
duke@1: * particular file as subject to the "Classpath" exception as provided
ohair@554: * by Oracle in the LICENSE file that accompanied this code.
duke@1: *
duke@1: * This code is distributed in the hope that it will be useful, but WITHOUT
duke@1: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@1: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@1: * version 2 for more details (a copy is included in the LICENSE file that
duke@1: * accompanied this code).
duke@1: *
duke@1: * You should have received a copy of the GNU General Public License version
duke@1: * 2 along with this work; if not, write to the Free Software Foundation,
duke@1: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@1: *
ohair@554: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@554: * or visit www.oracle.com if you need additional information or have any
ohair@554: * questions.
duke@1: */
duke@1:
duke@1: package com.sun.tools.javac.util;
duke@1:
jjg@700: import java.util.*;
jjg@1157: import com.sun.tools.javac.main.Option;
jjg@1157: import static com.sun.tools.javac.main.Option.*;
duke@1:
duke@1: /** A table of all command-line options.
duke@1: * If an option has an argument, the option name is mapped to the argument.
duke@1: * If a set option has no argument, it is mapped to itself.
duke@1: *
jjg@581: *
This is NOT part of any supported API.
jjg@581: * If you write code that depends on this, you do so at your own risk.
duke@1: * This code and its internal interfaces are subject to change or
duke@1: * deletion without notice.
duke@1: */
duke@1: public class Options {
duke@1: private static final long serialVersionUID = 0;
duke@1:
duke@1: /** The context key for the options. */
duke@1: public static final Context.Key optionsKey =
duke@1: new Context.Key();
duke@1:
duke@1: private LinkedHashMap values;
duke@1:
duke@1: /** Get the Options instance for this context. */
duke@1: public static Options instance(Context context) {
duke@1: Options instance = context.get(optionsKey);
duke@1: if (instance == null)
duke@1: instance = new Options(context);
duke@1: return instance;
duke@1: }
duke@1:
duke@1: protected Options(Context context) {
duke@1: // DEBUGGING -- Use LinkedHashMap for reproducability
duke@1: values = new LinkedHashMap();
duke@1: context.put(optionsKey, this);
duke@1: }
duke@1:
jjg@700: /**
jjg@700: * Get the value for an undocumented option.
jjg@700: */
duke@1: public String get(String name) {
duke@1: return values.get(name);
duke@1: }
duke@1:
jjg@700: /**
jjg@700: * Get the value for an option.
jjg@700: */
jjg@1157: public String get(Option option) {
jjg@1157: return values.get(option.text);
duke@1: }
duke@1:
jjg@700: /**
ksrini@923: * Get the boolean value for an option, patterned after Boolean.getBoolean,
ksrini@923: * essentially will return true, iff the value exists and is set to "true".
ksrini@923: */
ksrini@923: public boolean getBoolean(String name) {
ksrini@923: return getBoolean(name, false);
ksrini@923: }
ksrini@923:
ksrini@923: /**
ksrini@923: * Get the boolean with a default value if the option is not set.
ksrini@923: */
ksrini@923: public boolean getBoolean(String name, boolean defaultValue) {
ksrini@923: String value = get(name);
ksrini@923: return (value == null) ? defaultValue : Boolean.parseBoolean(value);
ksrini@923: }
ksrini@923:
ksrini@923: /**
jjg@700: * Check if the value for an undocumented option has been set.
jjg@700: */
jjg@700: public boolean isSet(String name) {
jjg@700: return (values.get(name) != null);
jjg@700: }
jjg@700:
jjg@700: /**
jjg@700: * Check if the value for an option has been set.
jjg@700: */
jjg@1157: public boolean isSet(Option option) {
jjg@1157: return (values.get(option.text) != null);
jjg@700: }
jjg@700:
jjg@700: /**
jjg@700: * Check if the value for a choice option has been set to a specific value.
jjg@700: */
jjg@1157: public boolean isSet(Option option, String value) {
jjg@1157: return (values.get(option.text + value) != null);
jjg@700: }
jjg@700:
jjg@700: /**
jjg@700: * Check if the value for an undocumented option has not been set.
jjg@700: */
jjg@700: public boolean isUnset(String name) {
jjg@700: return (values.get(name) == null);
jjg@700: }
jjg@700:
jjg@700: /**
jjg@700: * Check if the value for an option has not been set.
jjg@700: */
jjg@1157: public boolean isUnset(Option option) {
jjg@1157: return (values.get(option.text) == null);
jjg@700: }
jjg@700:
jjg@700: /**
jjg@700: * Check if the value for a choice option has not been set to a specific value.
jjg@700: */
jjg@1157: public boolean isUnset(Option option, String value) {
jjg@1157: return (values.get(option.text + value) == null);
jjg@700: }
jjg@700:
duke@1: public void put(String name, String value) {
duke@1: values.put(name, value);
duke@1: }
duke@1:
jjg@1157: public void put(Option option, String value) {
jjg@1157: values.put(option.text, value);
duke@1: }
duke@1:
duke@1: public void putAll(Options options) {
duke@1: values.putAll(options.values);
duke@1: }
duke@1:
duke@1: public void remove(String name) {
duke@1: values.remove(name);
duke@1: }
duke@1:
duke@1: public Set keySet() {
duke@1: return values.keySet();
duke@1: }
duke@1:
duke@1: public int size() {
duke@1: return values.size();
duke@1: }
duke@1:
jjg@1135: // light-weight notification mechanism
jjg@1135:
jjg@1135: private List listeners = List.nil();
jjg@1135:
jjg@1135: public void addListener(Runnable listener) {
jjg@1135: listeners = listeners.prepend(listener);
jjg@1135: }
jjg@1135:
jjg@1135: public void notifyListeners() {
jjg@1135: for (Runnable r: listeners)
jjg@1135: r.run();
jjg@1135: }
jjg@1135:
duke@1: /** Check for a lint suboption. */
duke@1: public boolean lint(String s) {
duke@1: // return true if either the specific option is enabled, or
duke@1: // they are all enabled without the specific one being
duke@1: // disabled
duke@1: return
jjg@700: isSet(XLINT_CUSTOM, s) ||
jjg@700: (isSet(XLINT) || isSet(XLINT_CUSTOM, "all")) &&
jjg@700: isUnset(XLINT_CUSTOM, "-" + s);
duke@1: }
duke@1: }