2 * Sone - Options.java - Copyright © 2010–2013 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.sone.core;
20 import java.util.Collections;
21 import java.util.HashMap;
24 import com.google.common.base.Predicate;
27 * Stores various options that influence Sone’s behaviour.
29 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
31 public class Options {
34 * Contains current and default value of an option.
37 * The type of the option
38 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
40 public static interface Option<T> {
43 * Returns the current value of the option. If the current value is not
44 * set (usually {@code null}), the default value is returned.
46 * @return The current value of the option
51 * Returns the real value of the option. This will also return an unset
52 * value (usually {@code null})!
54 * @return The real value of the option
59 * Validates the given value. Note that {@code null} is always a valid
63 * The value to validate
64 * @return {@code true} if this option does not have a validator, or the
65 * validator validates this object, {@code false} otherwise
67 public boolean validate(T value);
70 * Sets the current value of the option.
73 * The new value of the option
74 * @throws IllegalArgumentException
75 * if the value is not valid for this option
77 public void set(T value) throws IllegalArgumentException;
82 * Interface for objects that want to be notified when an option changes its
86 * The type of the option
87 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
89 public static interface OptionWatcher<T> {
92 * Notifies an object that an option has been changed.
95 * The option that has changed
97 * The old value of the option
99 * The new value of the option
101 public void optionChanged(Option<T> option, T oldValue, T newValue);
106 * Basic implementation of an {@link Option} that notifies an
107 * {@link OptionWatcher} if the value changes.
110 * The type of the option
111 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
113 public static class DefaultOption<T> implements Option<T> {
115 /** The default value. */
116 private final T defaultValue;
118 /** The current value. */
119 private volatile T value;
121 /** The validator. */
122 private Predicate<T> validator;
124 /** The option watcher. */
125 private final OptionWatcher<T> optionWatcher;
128 * Creates a new default option.
130 * @param defaultValue
131 * The default value of the option
133 public DefaultOption(T defaultValue) {
134 this(defaultValue, (OptionWatcher<T>) null);
138 * Creates a new default option.
140 * @param defaultValue
141 * The default value of the option
143 * The validator for value validation (may be {@code null})
145 public DefaultOption(T defaultValue, Predicate<T> validator) {
146 this(defaultValue, validator, null);
150 * Creates a new default option.
152 * @param defaultValue
153 * The default value of the option
154 * @param optionWatchers
155 * The option watchers (may be {@code null})
157 public DefaultOption(T defaultValue, OptionWatcher<T> optionWatchers) {
158 this(defaultValue, null, optionWatchers);
162 * Creates a new default option.
164 * @param defaultValue
165 * The default value of the option
167 * The validator for value validation (may be {@code null})
168 * @param optionWatcher
169 * The option watcher (may be {@code null})
171 public DefaultOption(T defaultValue, Predicate<T> validator, OptionWatcher<T> optionWatcher) {
172 this.defaultValue = defaultValue;
173 this.validator = validator;
174 this.optionWatcher = optionWatcher;
182 return (value != null) ? value : defaultValue;
186 * Returns the real value of the option. This will also return an unset
187 * value (usually {@code null})!
189 * @return The real value of the option
200 public boolean validate(T value) {
201 return (validator == null) || (value == null) || validator.apply(value);
208 public void set(T value) {
209 if ((value != null) && (validator != null) && (!validator.apply(value))) {
210 throw new IllegalArgumentException("New Value (" + value + ") could not be validated.");
212 T oldValue = this.value;
214 if (!get().equals(oldValue)) {
215 if (optionWatcher != null) {
216 optionWatcher.optionChanged(this, oldValue, get());
223 /** Holds all {@link Boolean} {@link Option}s. */
224 private final Map<String, Option<Boolean>> booleanOptions = Collections.synchronizedMap(new HashMap<String, Option<Boolean>>());
226 /** Holds all {@link Integer} {@link Option}s. */
227 private final Map<String, Option<Integer>> integerOptions = Collections.synchronizedMap(new HashMap<String, Option<Integer>>());
229 /** Holds all {@link String} {@link Option}s. */
230 private final Map<String, Option<String>> stringOptions = Collections.synchronizedMap(new HashMap<String, Option<String>>());
232 /** Holds all {@link Enum} {@link Option}s. */
233 private final Map<String, Option<? extends Enum<?>>> enumOptions = Collections.synchronizedMap(new HashMap<String, Option<? extends Enum<?>>>());
236 * Adds a boolean option.
239 * The name of the option
240 * @param booleanOption
242 * @return The given option
244 public Option<Boolean> addBooleanOption(String name, Option<Boolean> booleanOption) {
245 booleanOptions.put(name, booleanOption);
246 return booleanOption;
250 * Returns the boolean option with the given name.
253 * The name of the option
254 * @return The option, or {@code null} if there is no option with the given
257 public Option<Boolean> getBooleanOption(String name) {
258 return booleanOptions.get(name);
262 * Adds an {@link Integer} {@link Option}.
265 * The name of the option
266 * @param integerOption
268 * @return The given option
270 public Option<Integer> addIntegerOption(String name, Option<Integer> integerOption) {
271 integerOptions.put(name, integerOption);
272 return integerOption;
276 * Returns an {@link Integer} {@link Option}.
279 * The name of the integer option to get
280 * @return The integer option, or {@code null} if there is no option with
283 public Option<Integer> getIntegerOption(String name) {
284 return integerOptions.get(name);
288 * Adds a {@link String} {@link Option}.
291 * The name of the option
292 * @param stringOption
294 * @return The given option
296 public Option<String> addStringOption(String name, Option<String> stringOption) {
297 stringOptions.put(name, stringOption);
302 * Returns a {@link String} {@link Option}.
305 * The name of the string option to get
306 * @return The string option, or {@code null} if there is no option with the
309 public Option<String> getStringOption(String name) {
310 return stringOptions.get(name);
314 * Adds an {@link Enum} {@link Option}.
319 * The name of the option
322 * @return The given option
324 public <T extends Enum<T>> Option<T> addEnumOption(String name, Option<T> enumOption) {
325 enumOptions.put(name, enumOption);
330 * Returns a {@link Enum} {@link Option}. As the type can probably not be
331 * interred correctly you could help the compiler by calling this method
336 * options.<SomeEnum> getEnumOption("SomeEnumOption").get();
342 * The name of the option
343 * @return The enum option, or {@code null} if there is no enum option with
346 @SuppressWarnings("unchecked")
347 public <T extends Enum<T>> Option<T> getEnumOption(String name) {
348 return (Option<T>) enumOptions.get(name);