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.*; 29 import java.util.function.BiFunction; 30 import java.util.function.Function; 31 32 /** 33 * 34 * The idea for this Param is that is contains a list of possible values which the IDE 35 * can display and then choose the appropriate value for. For instance the Mac has a 36 * predefined set of categories which can be applied to LSApplicationCategoryType which 37 * is required for the mac app store. 38 */ 39 public class EnumeratedBundlerParam<T> extends BundlerParamInfo<T> { 40 //Not sure if this is the correct order, my idea is that from and IDE's perspective 41 //the string to display to the user is the key and then the value is some type of 42 //object (although probably a String in most cases) 43 private Map<String, T> possibleValues; 44 private boolean strict; 45 46 public EnumeratedBundlerParam(String name, 47 String description, 48 String id, 49 Class<T> valueType, 50 Function<Map<String, ? super Object>, T> defaultValueFunction, 51 BiFunction<String, Map<String, ? super Object>, T> stringConverter, 52 Map<String, T> possibleValues, 53 boolean strict) { 54 this.name = name; 55 this.description = description; 56 this.id = id; 57 this.valueType = valueType; 58 this.defaultValueFunction = defaultValueFunction; 59 this.stringConverter = stringConverter; 60 this.possibleValues = possibleValues; 61 this.strict = strict; 62 } 63 64 public boolean isInPossibleValues(T value) { 65 return possibleValues.values().contains(value); 66 } 67 68 //Having the displayable values as the keys seems a bit wacky 69 public Set<String> getDisplayableKeys() { 70 return Collections.unmodifiableSet(possibleValues.keySet()); 71 } 72 73 public boolean isStrict() { 74 return strict; 75 } 76 77 public boolean isLoose() { 78 return !isStrict(); 79 } 80 81 public T validatedFetchFrom(Map<String, ? super Object> params) 82 throws InvalidBundlerParamException { 83 if (isStrict()) { 84 T value = fetchFrom(params); 85 if (!isInPossibleValues(value)) { 86 throw new InvalidBundlerParamException("Parameter " + value.toString() + " not in valid set of values for BundlerParam " + name); 87 } 88 return value; 89 } 90 return fetchFrom(params); 91 } 92 93 }