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