8196295: [Graal] remove unused org.graalvm.options package
Reviewed-by: thartmann, kvn
This commit is contained in:
parent
2e16c465c3
commit
b42858623c
@ -1,55 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
package org.graalvm.options;
|
||||
|
||||
/**
|
||||
* Categorizes options according to user relevance.
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
public enum OptionCategory {
|
||||
|
||||
/**
|
||||
* An option common for users to apply.
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
USER,
|
||||
|
||||
/**
|
||||
* An option only relevant in corner cases and for fine-tuning.
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
EXPERT,
|
||||
|
||||
/**
|
||||
* An option only relevant when debugging language or instrument implementations.
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
DEBUG
|
||||
|
||||
}
|
@ -1,222 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
package org.graalvm.options;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* Represents metadata for a single option.
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
public final class OptionDescriptor {
|
||||
|
||||
private final OptionKey<?> key;
|
||||
private final String name;
|
||||
private final String help;
|
||||
private final OptionCategory kind;
|
||||
private final boolean deprecated;
|
||||
|
||||
OptionDescriptor(OptionKey<?> key, String name, String help, OptionCategory kind, boolean deprecated) {
|
||||
this.key = key;
|
||||
this.name = name;
|
||||
this.help = help;
|
||||
this.kind = kind;
|
||||
this.deprecated = deprecated;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the name of the option that this descriptor represents.
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the key for this option.
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
public OptionKey<?> getKey() {
|
||||
return key;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns <code>true</code> if this option was marked deprecated. This indicates that the
|
||||
* option is going to be removed in a future release or its use is not recommended.
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
public boolean isDeprecated() {
|
||||
return deprecated;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the user category of this option.
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
public OptionCategory getCategory() {
|
||||
return kind;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a human-readable description on how to use the option.
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
public String getHelp() {
|
||||
return help;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return "OptionDescriptor [key=" + key + ", help=" + help + ", kind=" + kind + ", deprecated=" + deprecated + "]";
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + (deprecated ? 1231 : 1237);
|
||||
result = prime * result + ((help == null) ? 0 : help.hashCode());
|
||||
result = prime * result + ((key == null) ? 0 : key.hashCode());
|
||||
result = prime * result + ((kind == null) ? 0 : kind.hashCode());
|
||||
result = prime * result + ((name == null) ? 0 : name.hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj) {
|
||||
return true;
|
||||
} else if (obj == null) {
|
||||
return false;
|
||||
} else if (getClass() != obj.getClass()) {
|
||||
return false;
|
||||
}
|
||||
OptionDescriptor other = (OptionDescriptor) obj;
|
||||
return Objects.equals(name, other.name) &&
|
||||
Objects.equals(deprecated, other.deprecated) &&
|
||||
Objects.equals(help, other.help) &&
|
||||
Objects.equals(key, other.key) &&
|
||||
Objects.equals(kind, other.kind);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new option descriptor builder by key. The option group and name is inferred by the
|
||||
* key.
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
public static <T> Builder newBuilder(OptionKey<T> key, String name) {
|
||||
Objects.requireNonNull(key);
|
||||
Objects.requireNonNull(name);
|
||||
return EMPTY.new Builder(key, name);
|
||||
}
|
||||
|
||||
private static final OptionDescriptor EMPTY = new OptionDescriptor(null, null, null, null, false);
|
||||
|
||||
/**
|
||||
* Represents an option descriptor builder.
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
public final class Builder {
|
||||
|
||||
private final OptionKey<?> key;
|
||||
private final String name;
|
||||
private boolean deprecated;
|
||||
private OptionCategory category;
|
||||
private String help;
|
||||
|
||||
Builder(OptionKey<?> key, String name) {
|
||||
this.key = key;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Defines the user category for this option. The default value is
|
||||
* {@link OptionCategory#DEBUG}.
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
public Builder category(@SuppressWarnings("hiding") OptionCategory category) {
|
||||
Objects.requireNonNull(category);
|
||||
this.category = category;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Defines if this option is deprecated. The default value for deprecated is
|
||||
* <code>false</code>. This can be used to evolve options between releases.
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
public Builder deprecated(@SuppressWarnings("hiding") boolean deprecated) {
|
||||
this.deprecated = deprecated;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Specifies a human-readable description on how to use the option.
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
public Builder help(@SuppressWarnings("hiding") String help) {
|
||||
Objects.requireNonNull(help);
|
||||
this.help = help;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds and returns a new option descriptor.
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
public OptionDescriptor build() {
|
||||
return new OptionDescriptor(key, name, help == null ? "" : help, category == null ? OptionCategory.DEBUG : category, deprecated);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -1,186 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
package org.graalvm.options;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
/**
|
||||
* An interface to a set of {@link OptionDescriptor}s.
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
public interface OptionDescriptors extends Iterable<OptionDescriptor> {
|
||||
|
||||
/**
|
||||
* An empty set of option descriptors.
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
OptionDescriptors EMPTY = new OptionDescriptors() {
|
||||
|
||||
public Iterator<OptionDescriptor> iterator() {
|
||||
return Collections.<OptionDescriptor> emptyList().iterator();
|
||||
}
|
||||
|
||||
public OptionDescriptor get(String key) {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Gets the {@link OptionDescriptor} matching a given option name or {@code null} if this option
|
||||
* descriptor set does not contain a matching option name.
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
OptionDescriptor get(String optionName);
|
||||
|
||||
/**
|
||||
* Creates a union options descriptor out of multiple given descriptors. The operation
|
||||
* descriptors are not checked for duplicate keys. The option descriptors are iterated in
|
||||
* declaration order.
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
static OptionDescriptors createUnion(OptionDescriptors... descriptors) {
|
||||
if (descriptors.length == 0) {
|
||||
return EMPTY;
|
||||
} else if (descriptors.length == 1) {
|
||||
return descriptors[0];
|
||||
} else {
|
||||
return new UnionOptionDescriptors(descriptors);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
@Override
|
||||
Iterator<OptionDescriptor> iterator();
|
||||
|
||||
/**
|
||||
* Creates an {@link OptionDescriptors} instance from a list. The option descriptors
|
||||
* implementation is backed by a {@link LinkedHashMap} that preserves ordering.
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
static OptionDescriptors create(List<OptionDescriptor> descriptors) {
|
||||
if (descriptors == null || descriptors.isEmpty()) {
|
||||
return EMPTY;
|
||||
}
|
||||
return new OptionDescriptorsMap(descriptors);
|
||||
}
|
||||
}
|
||||
|
||||
class OptionDescriptorsMap implements OptionDescriptors {
|
||||
|
||||
final Map<String, OptionDescriptor> descriptors = new LinkedHashMap<>();
|
||||
|
||||
OptionDescriptorsMap(List<OptionDescriptor> descriptorList) {
|
||||
for (OptionDescriptor descriptor : descriptorList) {
|
||||
descriptors.put(descriptor.getName(), descriptor);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public OptionDescriptor get(String optionName) {
|
||||
return descriptors.get(optionName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<OptionDescriptor> iterator() {
|
||||
return descriptors.values().iterator();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
final class UnionOptionDescriptors implements OptionDescriptors {
|
||||
|
||||
final OptionDescriptors[] descriptorsList;
|
||||
|
||||
UnionOptionDescriptors(OptionDescriptors[] descriptors) {
|
||||
// defensive copy
|
||||
this.descriptorsList = Arrays.copyOf(descriptors, descriptors.length);
|
||||
}
|
||||
|
||||
public Iterator<OptionDescriptor> iterator() {
|
||||
return new Iterator<OptionDescriptor>() {
|
||||
|
||||
Iterator<OptionDescriptor> descriptors = descriptorsList[0].iterator();
|
||||
int descriptorsIndex = 0;
|
||||
OptionDescriptor next = null;
|
||||
|
||||
public boolean hasNext() {
|
||||
return fetchNext() != null;
|
||||
}
|
||||
|
||||
private OptionDescriptor fetchNext() {
|
||||
if (next != null) {
|
||||
return next;
|
||||
}
|
||||
if (descriptors.hasNext()) {
|
||||
next = descriptors.next();
|
||||
return next;
|
||||
} else if (descriptorsIndex < descriptorsList.length - 1) {
|
||||
descriptorsIndex++;
|
||||
descriptors = descriptorsList[descriptorsIndex].iterator();
|
||||
return fetchNext();
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public OptionDescriptor next() {
|
||||
OptionDescriptor fetchedNext = fetchNext();
|
||||
if (fetchedNext != null) {
|
||||
// consume next
|
||||
this.next = null;
|
||||
return fetchedNext;
|
||||
} else {
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public OptionDescriptor get(String value) {
|
||||
for (OptionDescriptors descriptors : descriptorsList) {
|
||||
OptionDescriptor descriptor = descriptors.get(value);
|
||||
if (descriptor != null) {
|
||||
return descriptor;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
@ -1,103 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
package org.graalvm.options;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* Represents the option key for an option specification.
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
public final class OptionKey<T> {
|
||||
|
||||
private final OptionType<T> type;
|
||||
private final T defaultValue;
|
||||
|
||||
/**
|
||||
* Constructs a new option key given a default value. Throws {@link IllegalArgumentException} if
|
||||
* no default {@link OptionType} could be {@link OptionType#defaultType(Object) resolved} for
|
||||
* the given type. The default value must not be <code>null</code>.
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
public OptionKey(T defaultValue) {
|
||||
Objects.requireNonNull(defaultValue);
|
||||
this.defaultValue = defaultValue;
|
||||
this.type = OptionType.defaultType(defaultValue);
|
||||
if (type == null) {
|
||||
throw new IllegalArgumentException("No default type specified for type " + defaultValue.getClass().getName() + ". Specify the option type explicitly to resolve this.");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a new option key given a default value and option key.
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
public OptionKey(T defaultValue, OptionType<T> type) {
|
||||
Objects.requireNonNull(type);
|
||||
this.defaultValue = defaultValue;
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the option type of this key.
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
public OptionType<T> getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the default value for this option.
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
public T getDefaultValue() {
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the value of this key given the {@link OptionValues values}.
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
public T getValue(OptionValues values) {
|
||||
return values.get(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns <code>true</code> if a value for this key has been set for the given option values or
|
||||
* <code>false</code> if no value has been set.
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
public boolean hasBeenSet(OptionValues values) {
|
||||
return values.hasBeenSet(this);
|
||||
}
|
||||
|
||||
}
|
@ -1,222 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
package org.graalvm.options;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* Represents a type of an option that allows to convert string values to Java values.
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
public final class OptionType<T> {
|
||||
|
||||
private final String name;
|
||||
private final Function<String, T> stringConverter;
|
||||
private final Consumer<T> validator;
|
||||
private final T defaultValue;
|
||||
|
||||
/**
|
||||
* Constructs a new option type with name, defaultValue, and function that allows to convert a
|
||||
* string to the option type.
|
||||
*
|
||||
* @param name the name of the type.
|
||||
* @param defaultValue the default value to use if no value is given.
|
||||
* @param stringConverter a function that converts a string value to the option value. Can throw
|
||||
* {@link IllegalArgumentException} to indicate an invalid string.
|
||||
* @param validator used for validating the option value. Throws
|
||||
* {@link IllegalArgumentException} if the value is invalid.
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
public OptionType(String name, T defaultValue, Function<String, T> stringConverter, Consumer<T> validator) {
|
||||
Objects.requireNonNull(name);
|
||||
Objects.requireNonNull(stringConverter);
|
||||
Objects.requireNonNull(validator);
|
||||
this.name = name;
|
||||
this.stringConverter = stringConverter;
|
||||
this.defaultValue = defaultValue;
|
||||
this.validator = validator;
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a new option type with name, defaultValue, and function that allows to convert a
|
||||
* string to the option type.
|
||||
*
|
||||
* @param name the name of the type.
|
||||
* @param defaultValue the default value to use if no value is given.
|
||||
* @param stringConverter a function that converts a string value to the option value. Can throw
|
||||
* {@link IllegalArgumentException} to indicate an invalid string.
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
public OptionType(String name, T defaultValue, Function<String, T> stringConverter) {
|
||||
this(name, defaultValue, stringConverter, new Consumer<T>() {
|
||||
public void accept(T t) {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the default value of this type. Used if no value is available.
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
public T getDefaultValue() {
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the name of this type.
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a string value, validates it, and converts it to an object of this type.
|
||||
*
|
||||
* @throws IllegalArgumentException if the value is invalid or cannot be converted.
|
||||
* @since 1.0
|
||||
*/
|
||||
public T convert(String value) {
|
||||
T v = stringConverter.apply(value);
|
||||
validate(v);
|
||||
return v;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates an option value and throws an {@link IllegalArgumentException} if the value is
|
||||
* invalid.
|
||||
*
|
||||
* @throws IllegalArgumentException if the value is invalid or cannot be converted.
|
||||
* @since 1.0
|
||||
*/
|
||||
public void validate(T value) {
|
||||
validator.accept(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 1.0
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return "OptionType[name=" + name + ", defaultValue=" + defaultValue + "]";
|
||||
}
|
||||
|
||||
private static final Map<Class<?>, OptionType<?>> DEFAULTTYPES = new HashMap<>();
|
||||
static {
|
||||
DEFAULTTYPES.put(Boolean.class, new OptionType<>("Boolean", false, new Function<String, Boolean>() {
|
||||
public Boolean apply(String t) {
|
||||
if ("true".equals(t)) {
|
||||
return Boolean.TRUE;
|
||||
} else if ("false".equals(t)) {
|
||||
return Boolean.FALSE;
|
||||
} else {
|
||||
throw new IllegalArgumentException(String.format("Invalid boolean option value '%s'. The value of the option must be '%s' or '%s'.", t, "true", "false"));
|
||||
}
|
||||
}
|
||||
}));
|
||||
DEFAULTTYPES.put(Byte.class, new OptionType<>("Byte", (byte) 0, new Function<String, Byte>() {
|
||||
public Byte apply(String t) {
|
||||
try {
|
||||
return Byte.parseByte(t);
|
||||
} catch (NumberFormatException e) {
|
||||
throw new IllegalArgumentException(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}));
|
||||
DEFAULTTYPES.put(Integer.class, new OptionType<>("Integer", 0, new Function<String, Integer>() {
|
||||
public Integer apply(String t) {
|
||||
try {
|
||||
return Integer.parseInt(t);
|
||||
} catch (NumberFormatException e) {
|
||||
throw new IllegalArgumentException(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}));
|
||||
DEFAULTTYPES.put(Long.class, new OptionType<>("Long", 0L, new Function<String, Long>() {
|
||||
public Long apply(String t) {
|
||||
try {
|
||||
return Long.parseLong(t);
|
||||
} catch (NumberFormatException e) {
|
||||
throw new IllegalArgumentException(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}));
|
||||
DEFAULTTYPES.put(Float.class, new OptionType<>("Float", 0.0f, new Function<String, Float>() {
|
||||
public Float apply(String t) {
|
||||
try {
|
||||
return Float.parseFloat(t);
|
||||
} catch (NumberFormatException e) {
|
||||
throw new IllegalArgumentException(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}));
|
||||
DEFAULTTYPES.put(Double.class, new OptionType<>("Double", 0.0d, new Function<String, Double>() {
|
||||
public Double apply(String t) {
|
||||
try {
|
||||
return Double.parseDouble(t);
|
||||
} catch (NumberFormatException e) {
|
||||
throw new IllegalArgumentException(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}));
|
||||
DEFAULTTYPES.put(String.class, new OptionType<>("String", "0", new Function<String, String>() {
|
||||
public String apply(String t) {
|
||||
return t;
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the default option type for a given value. Returns <code>null</code> if no default
|
||||
* option type is available for the Java type of this value.
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> OptionType<T> defaultType(T value) {
|
||||
return defaultType((Class<T>) value.getClass());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the default option type for a class. Returns <code>null</code> if no default option
|
||||
* type is available for this Java type.
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> OptionType<T> defaultType(Class<T> clazz) {
|
||||
return (OptionType<T>) DEFAULTTYPES.get(clazz);
|
||||
}
|
||||
|
||||
}
|
@ -1,70 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
package org.graalvm.options;
|
||||
|
||||
/**
|
||||
* Represents a set of option values based on an {@link OptionDescriptor}.
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
public interface OptionValues {
|
||||
|
||||
/**
|
||||
* Returns all available options.
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
OptionDescriptors getDescriptors();
|
||||
|
||||
/**
|
||||
* Sets the value of {@code optionKey} to {@code value}.
|
||||
*
|
||||
* @throws IllegalArgumentException if the given value is not {@link OptionType#validate(Object)
|
||||
* validated} by the {@link OptionKey#getType() option type} of the key. Note that
|
||||
* the operation succeeds if the option key is not described by any of the
|
||||
* associated {@link #getDescriptors() descriptors}.
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
<T> void set(OptionKey<T> optionKey, T value);
|
||||
|
||||
/**
|
||||
* Returns the value of a given option. If no value is set or the key is not described by any
|
||||
* {@link #getDescriptors() descriptors} the {@link OptionType#getDefaultValue() default value}
|
||||
* of the given key is returned.
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
<T> T get(OptionKey<T> optionKey);
|
||||
|
||||
/**
|
||||
* Determines if a value for {@code optionKey} has been {@link #set} in this set of option
|
||||
* values.
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
boolean hasBeenSet(OptionKey<?> optionKey);
|
||||
|
||||
}
|
@ -1,33 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
/**
|
||||
* The Graal-SDK options package contains reusable collection classes for options.
|
||||
*
|
||||
* @see org.graalvm.options.OptionDescriptor
|
||||
* @see org.graalvm.options.OptionValues
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
package org.graalvm.options;
|
Loading…
x
Reference in New Issue
Block a user