< prev index next >

src/jdk.internal.vm.compiler/share/classes/org.graalvm.options/src/org/graalvm/options/OptionType.java

Print this page




  40     private final String name;
  41     private final Function<String, T> stringConverter;
  42     private final Consumer<T> validator;
  43     private final T defaultValue;
  44 
  45     /**
  46      * Constructs a new option type with name, defaultValue, and function that allows to convert a
  47      * string to the option type.
  48      *
  49      * @param name the name of the type.
  50      * @param defaultValue the default value to use if no value is given.
  51      * @param stringConverter a function that converts a string value to the option value. Can throw
  52      *            {@link IllegalArgumentException} to indicate an invalid string.
  53      * @param validator used for validating the option value. Throws
  54      *            {@link IllegalArgumentException} if the value is invalid.
  55      *
  56      * @since 1.0
  57      */
  58     public OptionType(String name, T defaultValue, Function<String, T> stringConverter, Consumer<T> validator) {
  59         Objects.requireNonNull(name);
  60         Objects.requireNonNull(defaultValue);
  61         Objects.requireNonNull(stringConverter);
  62         Objects.requireNonNull(validator);
  63         this.name = name;
  64         this.stringConverter = stringConverter;
  65         this.defaultValue = defaultValue;
  66         this.validator = validator;
  67     }
  68 
  69     /**
  70      * Constructs a new option type with name, defaultValue, and function that allows to convert a
  71      * string to the option type.
  72      *
  73      * @param name the name of the type.
  74      * @param defaultValue the default value to use if no value is given.
  75      * @param stringConverter a function that converts a string value to the option value. Can throw
  76      *            {@link IllegalArgumentException} to indicate an invalid string.
  77      *
  78      * @since 1.0
  79      */
  80     public OptionType(String name, T defaultValue, Function<String, T> stringConverter) {


 116 
 117     /**
 118      * Validates an option value and throws an {@link IllegalArgumentException} if the value is
 119      * invalid.
 120      *
 121      * @throws IllegalArgumentException if the value is invalid or cannot be converted.
 122      * @since 1.0
 123      */
 124     public void validate(T value) {
 125         validator.accept(value);
 126     }
 127 
 128     /**
 129      * @since 1.0
 130      */
 131     @Override
 132     public String toString() {
 133         return "OptionType[name=" + name + ", defaultValue=" + defaultValue + "]";
 134     }
 135 
 136     private static Map<Class<?>, OptionType<?>> DEFAULTTYPES = new HashMap<>();
 137     static {
 138         DEFAULTTYPES.put(Boolean.class, new OptionType<>("Boolean", false, new Function<String, Boolean>() {
 139             public Boolean apply(String t) {
 140                 if ("true".equals(t)) {
 141                     return Boolean.TRUE;
 142                 } else if ("false".equals(t)) {
 143                     return Boolean.FALSE;
 144                 } else {
 145                     throw new IllegalArgumentException(String.format("Invalid boolean option value '%s'. The value of the option must be '%s' or '%s'.", t, "true", "false"));
 146                 }
 147             }
 148         }));
 149         DEFAULTTYPES.put(Byte.class, new OptionType<>("Byte", (byte) 0, new Function<String, Byte>() {
 150             public Byte apply(String t) {
 151                 try {
 152                     return Byte.parseByte(t);
 153                 } catch (NumberFormatException e) {
 154                     throw new IllegalArgumentException(e.getMessage(), e);
 155                 }
 156             }


 183             }
 184         }));
 185         DEFAULTTYPES.put(Double.class, new OptionType<>("Double", 0.0d, new Function<String, Double>() {
 186             public Double apply(String t) {
 187                 try {
 188                     return Double.parseDouble(t);
 189                 } catch (NumberFormatException e) {
 190                     throw new IllegalArgumentException(e.getMessage(), e);
 191                 }
 192             }
 193         }));
 194         DEFAULTTYPES.put(String.class, new OptionType<>("String", "0", new Function<String, String>() {
 195             public String apply(String t) {
 196                 return t;
 197             }
 198         }));
 199     }
 200 
 201     /**
 202      * Returns the default option type for a given value. Returns <code>null</code> if no default
 203      * option type is available for this Java type.
 204      *
 205      * @since 1.0
 206      */
 207     @SuppressWarnings("unchecked")
 208     public static <T> OptionType<T> defaultType(Object value) {
 209         return (OptionType<T>) DEFAULTTYPES.get(value.getClass());











 210     }
 211 
 212 }


  40     private final String name;
  41     private final Function<String, T> stringConverter;
  42     private final Consumer<T> validator;
  43     private final T defaultValue;
  44 
  45     /**
  46      * Constructs a new option type with name, defaultValue, and function that allows to convert a
  47      * string to the option type.
  48      *
  49      * @param name the name of the type.
  50      * @param defaultValue the default value to use if no value is given.
  51      * @param stringConverter a function that converts a string value to the option value. Can throw
  52      *            {@link IllegalArgumentException} to indicate an invalid string.
  53      * @param validator used for validating the option value. Throws
  54      *            {@link IllegalArgumentException} if the value is invalid.
  55      *
  56      * @since 1.0
  57      */
  58     public OptionType(String name, T defaultValue, Function<String, T> stringConverter, Consumer<T> validator) {
  59         Objects.requireNonNull(name);

  60         Objects.requireNonNull(stringConverter);
  61         Objects.requireNonNull(validator);
  62         this.name = name;
  63         this.stringConverter = stringConverter;
  64         this.defaultValue = defaultValue;
  65         this.validator = validator;
  66     }
  67 
  68     /**
  69      * Constructs a new option type with name, defaultValue, and function that allows to convert a
  70      * string to the option type.
  71      *
  72      * @param name the name of the type.
  73      * @param defaultValue the default value to use if no value is given.
  74      * @param stringConverter a function that converts a string value to the option value. Can throw
  75      *            {@link IllegalArgumentException} to indicate an invalid string.
  76      *
  77      * @since 1.0
  78      */
  79     public OptionType(String name, T defaultValue, Function<String, T> stringConverter) {


 115 
 116     /**
 117      * Validates an option value and throws an {@link IllegalArgumentException} if the value is
 118      * invalid.
 119      *
 120      * @throws IllegalArgumentException if the value is invalid or cannot be converted.
 121      * @since 1.0
 122      */
 123     public void validate(T value) {
 124         validator.accept(value);
 125     }
 126 
 127     /**
 128      * @since 1.0
 129      */
 130     @Override
 131     public String toString() {
 132         return "OptionType[name=" + name + ", defaultValue=" + defaultValue + "]";
 133     }
 134 
 135     private static final Map<Class<?>, OptionType<?>> DEFAULTTYPES = new HashMap<>();
 136     static {
 137         DEFAULTTYPES.put(Boolean.class, new OptionType<>("Boolean", false, new Function<String, Boolean>() {
 138             public Boolean apply(String t) {
 139                 if ("true".equals(t)) {
 140                     return Boolean.TRUE;
 141                 } else if ("false".equals(t)) {
 142                     return Boolean.FALSE;
 143                 } else {
 144                     throw new IllegalArgumentException(String.format("Invalid boolean option value '%s'. The value of the option must be '%s' or '%s'.", t, "true", "false"));
 145                 }
 146             }
 147         }));
 148         DEFAULTTYPES.put(Byte.class, new OptionType<>("Byte", (byte) 0, new Function<String, Byte>() {
 149             public Byte apply(String t) {
 150                 try {
 151                     return Byte.parseByte(t);
 152                 } catch (NumberFormatException e) {
 153                     throw new IllegalArgumentException(e.getMessage(), e);
 154                 }
 155             }


 182             }
 183         }));
 184         DEFAULTTYPES.put(Double.class, new OptionType<>("Double", 0.0d, new Function<String, Double>() {
 185             public Double apply(String t) {
 186                 try {
 187                     return Double.parseDouble(t);
 188                 } catch (NumberFormatException e) {
 189                     throw new IllegalArgumentException(e.getMessage(), e);
 190                 }
 191             }
 192         }));
 193         DEFAULTTYPES.put(String.class, new OptionType<>("String", "0", new Function<String, String>() {
 194             public String apply(String t) {
 195                 return t;
 196             }
 197         }));
 198     }
 199 
 200     /**
 201      * Returns the default option type for a given value. Returns <code>null</code> if no default
 202      * option type is available for the Java type of this value.
 203      *
 204      * @since 1.0
 205      */
 206     @SuppressWarnings("unchecked")
 207     public static <T> OptionType<T> defaultType(T value) {
 208         return defaultType((Class<T>) value.getClass());
 209     }
 210 
 211     /**
 212      * Returns the default option type for a class. Returns <code>null</code> if no default option
 213      * type is available for this Java type.
 214      *
 215      * @since 1.0
 216      */
 217     @SuppressWarnings("unchecked")
 218     public static <T> OptionType<T> defaultType(Class<T> clazz) {
 219         return (OptionType<T>) DEFAULTTYPES.get(clazz);
 220     }
 221 
 222 }
< prev index next >