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