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