1 /*
   2  * Copyright (c) 2017, 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 package org.graalvm.options;
  26 
  27 import java.util.HashMap;
  28 import java.util.Map;
  29 import java.util.Objects;
  30 import java.util.function.Consumer;
  31 import java.util.function.Function;
  32 
  33 /**
  34  * Represents a type of an option that allows to convert string values to Java values.
  35  *
  36  * @since 1.0
  37  */
  38 public final class OptionType<T> {
  39 
  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) {
  81         this(name, defaultValue, stringConverter, new Consumer<T>() {
  82             public void accept(T t) {
  83             }
  84         });
  85     }
  86 
  87     /**
  88      * Returns the default value of this type. Used if no value is available.
  89      *
  90      * @since 1.0
  91      */
  92     public T getDefaultValue() {
  93         return defaultValue;
  94     }
  95 
  96     /**
  97      * Returns the name of this type.
  98      *
  99      * @since 1.0
 100      */
 101     public String getName() {
 102         return name;
 103     }
 104 
 105     /**
 106      * Converts a string value, validates it, and converts it to an object of this type.
 107      *
 108      * @throws IllegalArgumentException if the value is invalid or cannot be converted.
 109      * @since 1.0
 110      */
 111     public T convert(String value) {
 112         T v = stringConverter.apply(value);
 113         validate(v);
 114         return v;
 115     }
 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             }
 157         }));
 158         DEFAULTTYPES.put(Integer.class, new OptionType<>("Integer", 0, new Function<String, Integer>() {
 159             public Integer apply(String t) {
 160                 try {
 161                     return Integer.parseInt(t);
 162                 } catch (NumberFormatException e) {
 163                     throw new IllegalArgumentException(e.getMessage(), e);
 164                 }
 165             }
 166         }));
 167         DEFAULTTYPES.put(Long.class, new OptionType<>("Long", 0L, new Function<String, Long>() {
 168             public Long apply(String t) {
 169                 try {
 170                     return Long.parseLong(t);
 171                 } catch (NumberFormatException e) {
 172                     throw new IllegalArgumentException(e.getMessage(), e);
 173                 }
 174             }
 175         }));
 176         DEFAULTTYPES.put(Float.class, new OptionType<>("Float", 0.0f, new Function<String, Float>() {
 177             public Float apply(String t) {
 178                 try {
 179                     return Float.parseFloat(t);
 180                 } catch (NumberFormatException e) {
 181                     throw new IllegalArgumentException(e.getMessage(), e);
 182                 }
 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 }