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.*;
  29 import java.util.function.BiFunction;
  30 import java.util.function.Function;
  31 
  32 /**
  33  * EnumeratedBundlerParams<T>
  34  *
  35  * Contains key-value pairs (elements) where keys are "displayable"
  36  * keys which the IDE can display/choose and values are "identifier" values
  37  * which can be stored in parameters' map.
  38  *
  39  * For instance the Mac has a predefined set of categories which can be applied
  40  * to LSApplicationCategoryType which is required for the mac app store.
  41  *
  42  * The following example illustrates a simple usage of
  43  *     the MAC_CATEGORY parameter:
  44  *
  45  * <pre>{@code
  46  *     Set<String> keys = MAC_CATEGORY.getDisplayableKeys();
  47  *
  48  *     String key = getLastValue(keys); // get last value for example
  49  *
  50  *     String value = MAC_CATEGORY.getValueForDisplayableKey(key);
  51  *     params.put(MAC_CATEGORY.getID(), value);
  52  * }</pre>
  53  *
  54  */
  55 class EnumeratedBundlerParam<T> extends BundlerParamInfo<T> {
  56     // Not sure if this is the correct order, my idea is that from IDE
  57     // perspective the string to display to the user is the key and then the
  58     // value is some type of object (although probably a String in most cases)
  59     private final Map<String, T> elements;
  60     private final boolean strict;
  61 
  62     EnumeratedBundlerParam(String id, Class<T> valueType,
  63             Function<Map<String, ? super Object>, T> defaultValueFunction,
  64             BiFunction<String, Map<String, ? super Object>, T> stringConverter,
  65             Map<String, T> elements, boolean strict) {
  66         this.id = id;
  67         this.valueType = valueType;
  68         this.defaultValueFunction = defaultValueFunction;
  69         this.stringConverter = stringConverter;
  70         this.elements = elements;
  71         this.strict = strict;
  72     }
  73 
  74     boolean isInPossibleValues(T value) {
  75         return elements.values().contains(value);
  76     }
  77 
  78     // Having the displayable values as the keys seems a bit wacky
  79     Set<String> getDisplayableKeys() {
  80         return Collections.unmodifiableSet(elements.keySet());
  81     }
  82 
  83     // mapping from a "displayable" key to an "identifier" value.
  84     T getValueForDisplayableKey(String displayableKey) {
  85         return elements.get(displayableKey);
  86     }
  87 
  88     boolean isStrict() {
  89         return strict;
  90     }
  91 
  92     boolean isLoose() {
  93         return !isStrict();
  94     }
  95 
  96 }