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 name, String description,
63 String id, 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, boolean strict) {
67 this.name = name;
68 this.description = description;
69 this.id = id;
70 this.valueType = valueType;
71 this.defaultValueFunction = defaultValueFunction;
72 this.stringConverter = stringConverter;
73 this.elements = elements;
74 this.strict = strict;
75 }
76
77 boolean isInPossibleValues(T value) {
78 return elements.values().contains(value);
79 }
80
81 // Having the displayable values as the keys seems a bit wacky
82 Set<String> getDisplayableKeys() {
83 return Collections.unmodifiableSet(elements.keySet());
84 }
85
86 // mapping from a "displayable" key to an "identifier" value.
87 T getValueForDisplayableKey(String displayableKey) {
88 return elements.get(displayableKey);
89 }
90
91 boolean isStrict() {
92 return strict;
93 }
94
95 boolean isLoose() {
96 return !isStrict();
97 }
98
99 T validatedFetchFrom(Map<String, ? super Object> params)
100 throws InvalidBundlerParamException {
101 if (isStrict()) {
102 T value = fetchFrom(params);
103 if (!isInPossibleValues(value)) {
104 throw new InvalidBundlerParamException("Parameter "
105 + value.toString()
106 + " not in valid set of values for BundlerParam "
107 + name);
108 }
109 return value;
110 }
111 return fetchFrom(params);
112 }
113
114 }
|
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 }
|