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