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 /**
  34  * @deprecated use {@link ToolProvider} to locate the {@code "javapackager"} tool instead.
  35  */
  36 @Deprecated(since="10", forRemoval=true)
  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.  Typically String.class
  55      */
  56     Class<T> valueType;
  57 
  58     /**
  59      * If the value is not set, and no fallback value is found, the parameter uses the value returned by the producer.
  60      */
  61     Function<Map<String, ? super Object>, T> defaultValueFunction;
  62 
  63     /**
  64      * An optional string converter for command line arguments.
  65      */
  66     BiFunction<String, Map<String, ? super Object>, T> stringConverter;
  67 
  68     public String getName() {
  69         return name;
  70     }
  71 
  72     public void setName(String name) {
  73         this.name = name;
  74     }
  75 
  76     public String getDescription() {
  77         return description;
  78     }
  79 
  80     public void setDescription(String description) {
  81         this.description = description;
  82     }
  83 
  84     public String getID() {
  85         return id;
  86     }
  87 
  88     public void setId(String id) {
  89         this.id = id;
  90     }
  91 
  92     public Class<T> getValueType() {
  93         return valueType;
  94     }
  95 
  96     public void setValueType(Class<T> valueType) {
  97         this.valueType = valueType;
  98     }
  99 
 100     public Function<Map<String, ? super Object>, T> getDefaultValueFunction() {
 101         return defaultValueFunction;
 102     }
 103 
 104     public void setDefaultValueFunction(Function<Map<String, ? super Object>, T> defaultValueFunction) {
 105         this.defaultValueFunction = defaultValueFunction;
 106     }
 107 
 108     public BiFunction<String, Map<String, ? super Object>,T> getStringConverter() {
 109         return stringConverter;
 110     }
 111 
 112     public void setStringConverter(BiFunction<String, Map<String, ? super Object>, T> stringConverter) {
 113         this.stringConverter = stringConverter;
 114     }
 115 
 116     @SuppressWarnings("unchecked")
 117     public final T fetchFrom(Map<String, ? super Object> params) {
 118         return fetchFrom(params, true);
 119     }
 120 
 121     @SuppressWarnings("unchecked")
 122     public final T fetchFrom(Map<String, ? super Object> params, boolean invokeDefault) {
 123         Object o = params.get(getID());
 124         if (o instanceof String && getStringConverter() != null) {
 125             return getStringConverter().apply((String)o, params);
 126         }
 127 
 128         Class klass = getValueType();
 129         if (klass.isInstance(o)) {
 130             return (T) o;
 131         }
 132         if (o != null) {
 133             throw new IllegalArgumentException("Param " + getID() + " should be of type " + getValueType() + " but is a " + o.getClass());
 134         }
 135         if (params.containsKey(getID())) {
 136             // explicit nulls are allowed
 137             return null;
 138         }
 139 
 140         if (invokeDefault && (getDefaultValueFunction() != null)) {
 141             T result =  getDefaultValueFunction().apply(params);
 142             if (result != null) {
 143                 params.put(getID(), result);
 144             }
 145             return result;
 146         }
 147 
 148         // ultimate fallback
 149         return null;
 150     }
 151 }