1 /*
   2  * Copyright (c) 2014, 2018, 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.jpackager.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 public class BundlerParamInfo<T> {
  38     /**
  39      * The user friendly name of the parameter
  40      */
  41     String name;
  42 
  43     /**
  44      * A more verbose description of the parameter
  45      */
  46     String description;
  47 
  48     /**
  49      * The command line and hashmap name of the parameter
  50      */
  51     String id;
  52 
  53     /**
  54      * Type of the parameter.
  55      */
  56     Class<T> valueType;
  57 
  58     /**
  59      * If the value is not set, and no fallback value is found,
  60      * the parameter uses the value returned by the producer.
  61      */
  62     Function<Map<String, ? super Object>, T> defaultValueFunction;
  63 
  64     /**
  65      * An optional string converter for command line arguments.
  66      */
  67     BiFunction<String, Map<String, ? super Object>, T> stringConverter;
  68 
  69     public String getName() {
  70         return name;
  71     }
  72 
  73     public void setName(String name) {
  74         this.name = name;
  75     }
  76 
  77     public String getDescription() {
  78         return description;
  79     }
  80 
  81     public void setDescription(String description) {
  82         this.description = description;
  83     }
  84 
  85     public String getID() {
  86         return id;
  87     }
  88 
  89     public void setId(String id) {
  90         this.id = id;
  91     }
  92 
  93     public Class<T> getValueType() {
  94         return valueType;
  95     }
  96 
  97     public void setValueType(Class<T> valueType) {
  98         this.valueType = valueType;
  99     }
 100 
 101     public Function<Map<String, ? super Object>, T> getDefaultValueFunction() {
 102         return defaultValueFunction;
 103     }
 104 
 105     public void setDefaultValueFunction(
 106             Function<Map<String, ? super Object>, T> defaultValueFunction) {
 107         this.defaultValueFunction = defaultValueFunction;
 108     }
 109 
 110     public BiFunction<String, Map<String, ? super Object>,T>
 111             getStringConverter() {
 112         return stringConverter;
 113     }
 114 
 115     public void setStringConverter(BiFunction<String,
 116             Map<String, ? super Object>, T> stringConverter) {
 117         this.stringConverter = stringConverter;
 118     }
 119 
 120     @SuppressWarnings("unchecked")
 121     public final T fetchFrom(Map<String, ? super Object> params) {
 122         return fetchFrom(params, true);
 123     }
 124 
 125     @SuppressWarnings("unchecked")
 126     public final T fetchFrom(Map<String, ? super Object> params,
 127             boolean invokeDefault) {
 128         Object o = params.get(getID());
 129         if (o instanceof String && getStringConverter() != null) {
 130             return getStringConverter().apply((String)o, params);
 131         }
 132 
 133         Class<T> klass = getValueType();
 134         if (klass.isInstance(o)) {
 135             return (T) o;
 136         }
 137         if (o != null) {
 138             throw new IllegalArgumentException("Param " + getID()
 139                     + " should be of type " + getValueType()
 140                     + " but is a " + o.getClass());
 141         }
 142         if (params.containsKey(getID())) {
 143             // explicit nulls are allowed
 144             return null;
 145         }
 146 
 147         if (invokeDefault && (getDefaultValueFunction() != null)) {
 148             T result =  getDefaultValueFunction().apply(params);
 149             if (result != null) {
 150                 params.put(getID(), result);
 151             }
 152             return result;
 153         }
 154 
 155         // ultimate fallback
 156         return null;
 157     }
 158 }