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.bundlers; 27 28 import java.util.Map; 29 import java.util.function.Function; 30 31 public class BundlerParamInfo<T> { 32 33 /** 34 * The user friendly name of the parameter 35 */ 36 String name; 37 38 /** 39 * A more verbose description of the parameter 40 */ 41 String description; 42 43 /** 44 * The command line and hashmap name of the parameter 45 */ 46 String id; 47 48 /** 49 * Type of the parameter. Typically String.class 50 */ 51 Class<T> valueType; 52 53 /** 54 * If the parameter is not set, what parameter the bundler will fall back on to use 55 */ 56 String[] fallbackIDs; 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 * Does the parameter require the user or tool to set a value? i.e. if the parameter is 65 * not set will it cause the bundler to fail? 66 */ 67 boolean requiresUserSetting; 68 69 /** 70 * An optional string converter for command line arguments. 71 */ 72 Function<String, T> stringConverter; 73 74 public String getName() { 75 return name; 76 } 77 78 public void setName(String name) { 79 this.name = name; 80 } 81 82 public String getDescription() { 83 return description; 84 } 85 86 public void setDescription(String description) { 87 this.description = description; 88 } 89 90 public String getID() { 91 return id; 92 } 93 94 public void setId(String id) { 95 this.id = id; 96 } 97 98 public Class<T> getValueType() { 99 return valueType; 100 } 101 102 public void setValueType(Class<T> valueType) { 103 this.valueType = valueType; 104 } 105 106 public String[] getFallbackIDs() { 107 return fallbackIDs; 108 } 109 110 public void setFallbackIDs(String[] fallbackID) { 111 this.fallbackIDs = fallbackID; 112 } 113 114 public Function<Map<String, ? super Object>, T> getDefaultValueFunction() { 115 return defaultValueFunction; 116 } 117 118 public void setDefaultValueFunction(Function<Map<String, ? super Object>, T> defaultValueFunction) { 119 this.defaultValueFunction = defaultValueFunction; 120 } 121 122 public boolean isRequiresUserSetting() { 123 return requiresUserSetting; 124 } 125 126 public void setRequiresUserSetting(boolean requiresUserSetting) { 127 this.requiresUserSetting = requiresUserSetting; 128 } 129 130 public Function<String, T> getStringConverter() { 131 return stringConverter; 132 } 133 134 public void setStringConverter(Function<String, T> stringConverter) { 135 this.stringConverter = stringConverter; 136 } 137 138 @SuppressWarnings("unchecked") 139 public final T fetchFrom(Map<String, ? super Object> params) { 140 Object o = params.get(getID()); 141 if (o instanceof String && getStringConverter() != null) { 142 return getStringConverter().apply((String)o); 143 } 144 145 Class klass = getValueType(); 146 if (klass.isInstance(o)) { 147 return (T) o; 148 } 149 if (o != null) { 150 throw new IllegalArgumentException("Param " + getID() + " should be of type " + getValueType() + " but is a " + o.getClass()); 151 } 152 if (params.containsKey(getID())) { 153 // explicit nulls are allowed 154 return null; 155 } 156 157 if (getFallbackIDs() != null) { 158 for (String fallback: getFallbackIDs()) { 159 o = params.get(fallback); 160 if (klass.isInstance(o)) { 161 return (T) o; 162 } 163 } 164 } 165 166 if (getDefaultValueFunction() != null) { 167 T result = getDefaultValueFunction().apply(params); 168 if (result != null) { 169 params.put(getID(), result); 170 } 171 return result; 172 } 173 174 // ultimate fallback 175 return null; 176 } 177 }