1 /*
   2  * Copyright (c) 2014, 2019, 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 
  26 package jdk.jpackage.internal;
  27 
  28 import java.util.Map;
  29 import java.util.function.BiFunction;
  30 import java.util.function.Function;
  31 
  32 /**
  33  * BundlerParamInfo<T>
  34  *
  35  * A BundlerParamInfo encapsulates an individual bundler parameter of type <T>.
  36  */
  37 class BundlerParamInfo<T> {
  38 
  39     /**
  40      * The command line and hashmap name of the parameter
  41      */
  42     String id;
  43 
  44     /**
  45      * Type of the parameter
  46      */
  47     Class<T> valueType;
  48 
  49     /**
  50      * Indicates if value was set using default value function
  51      */
  52     boolean isDefaultValue;
  53 
  54     /**
  55      * If the value is not set, and no fallback value is found,
  56      * the parameter uses the value returned by the producer.
  57      */
  58     Function<Map<String, ? super Object>, T> defaultValueFunction;
  59 
  60     /**
  61      * An optional string converter for command line arguments.
  62      */
  63     BiFunction<String, Map<String, ? super Object>, T> stringConverter;
  64 
  65     String getID() {
  66         return id;
  67     }
  68 
  69     Class<T> getValueType() {
  70         return valueType;
  71     }
  72 
  73     void setValueType(Class<T> valueType) {
  74         this.valueType = valueType;
  75     }
  76 
  77     boolean getIsDefaultValue() {
  78         return isDefaultValue;
  79     }
  80 
  81     Function<Map<String, ? super Object>, T> getDefaultValueFunction() {
  82         return defaultValueFunction;
  83     }
  84 
  85     void setDefaultValueFunction(
  86             Function<Map<String, ? super Object>, T> defaultValueFunction) {
  87         this.defaultValueFunction = defaultValueFunction;
  88     }
  89 
  90     BiFunction<String, Map<String, ? super Object>,T>
  91             getStringConverter() {
  92         return stringConverter;
  93     }
  94 
  95     void setStringConverter(BiFunction<String,
  96             Map<String, ? super Object>, T> stringConverter) {
  97         this.stringConverter = stringConverter;
  98     }
  99 
 100     @SuppressWarnings("unchecked")
 101     final T fetchFrom(Map<String, ? super Object> params) {
 102         return fetchFrom(params, true);
 103     }
 104 
 105     @SuppressWarnings("unchecked")
 106     final T fetchFrom(Map<String, ? super Object> params,
 107             boolean invokeDefault) {
 108         Object o = params.get(getID());
 109         if (o instanceof String && getStringConverter() != null) {
 110             return getStringConverter().apply((String)o, params);
 111         }
 112 
 113         Class<T> klass = getValueType();
 114         if (klass.isInstance(o)) {
 115             return (T) o;
 116         }
 117         if (o != null) {
 118             throw new IllegalArgumentException("Param " + getID()
 119                     + " should be of type " + getValueType()
 120                     + " but is a " + o.getClass());
 121         }
 122         if (params.containsKey(getID())) {
 123             // explicit nulls are allowed
 124             return null;
 125         }
 126 
 127         if (invokeDefault && (getDefaultValueFunction() != null)) {
 128             T result =  getDefaultValueFunction().apply(params);
 129             if (result != null) {
 130                 params.put(getID(), result);
 131                 isDefaultValue = true;
 132             }
 133             return result;
 134         }
 135 
 136         // ultimate fallback
 137         return null;
 138     }
 139 }