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.incubator.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 boolean getIsDefaultValue() { 74 return isDefaultValue; 75 } 76 77 Function<Map<String, ? super Object>, T> getDefaultValueFunction() { 78 return defaultValueFunction; 79 } 80 81 BiFunction<String, Map<String, ? super Object>,T> 82 getStringConverter() { 83 return stringConverter; 84 } 85 86 @SuppressWarnings("unchecked") 87 final T fetchFrom(Map<String, ? super Object> params) { 88 return fetchFrom(params, true); 89 } 90 91 @SuppressWarnings("unchecked") 92 final T fetchFrom(Map<String, ? super Object> params, 93 boolean invokeDefault) { 94 Object o = params.get(getID()); 95 if (o instanceof String && getStringConverter() != null) { 96 return getStringConverter().apply((String)o, params); 97 } 98 99 Class<T> klass = getValueType(); 100 if (klass.isInstance(o)) { 101 return (T) o; 102 } 103 if (o != null) { 104 throw new IllegalArgumentException("Param " + getID() 105 + " should be of type " + getValueType() 106 + " but is a " + o.getClass()); 107 } 108 if (params.containsKey(getID())) { 109 // explicit nulls are allowed 110 return null; 111 } 112 113 if (invokeDefault && (getDefaultValueFunction() != null)) { 114 T result = getDefaultValueFunction().apply(params); 115 if (result != null) { 116 params.put(getID(), result); 117 isDefaultValue = true; 118 } 119 return result; 120 } 121 122 // ultimate fallback 123 return null; 124 } 125 }