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