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 default value of the option.
45 * @return The default value of the option
47 public T getDefault();
50 * Returns the current value of the option. If the current value is not
51 * set (usually {@code null}), the default value is returned.
53 * @return The current value of the option
58 * Returns the real value of the option. This will also return an unset
59 * value (usually {@code null})!
61 * @return The real value of the option
66 * Validates the given value. Note that {@code null} is always a valid
70 * The value to validate
71 * @return {@code true} if this option does not have a validator, or the
72 * validator validates this object, {@code false} otherwise
74 public boolean validate(T value);
77 * Sets the current value of the option.
80 * The new value of the option
81 * @throws IllegalArgumentException
82 * if the value is not valid for this option
84 public void set(T value) throws IllegalArgumentException;
89 * Interface for objects that want to be notified when an option changes its
93 * The type of the option
94 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
96 public static interface OptionWatcher<T> {
99 * Notifies an object that an option has been changed.
102 * The option that has changed
104 * The old value of the option
106 * The new value of the option
108 public void optionChanged(Option<T> option, T oldValue, T newValue);
113 * Basic implementation of an {@link Option} that notifies an
114 * {@link OptionWatcher} if the value changes.
117 * The type of the option
118 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
120 public static class DefaultOption<T> implements Option<T> {
122 /** The default value. */
123 private final T defaultValue;
125 /** The current value. */
126 private volatile T value;
128 /** The validator. */
129 private Predicate<T> validator;
131 /** The option watcher. */
132 private final OptionWatcher<T> optionWatcher;
135 * Creates a new default option.
137 * @param defaultValue
138 * The default value of the option
140 public DefaultOption(T defaultValue) {
141 this(defaultValue, (OptionWatcher<T>) null);
145 * Creates a new default option.
147 * @param defaultValue
148 * The default value of the option
150 * The validator for value validation (may be {@code null})
152 public DefaultOption(T defaultValue, Predicate<T> validator) {
153 this(defaultValue, validator, null);
157 * Creates a new default option.
159 * @param defaultValue
160 * The default value of the option
161 * @param optionWatchers
162 * The option watchers (may be {@code null})
164 public DefaultOption(T defaultValue, OptionWatcher<T> optionWatchers) {
165 this(defaultValue, null, optionWatchers);
169 * Creates a new default option.
171 * @param defaultValue
172 * The default value of the option
174 * The validator for value validation (may be {@code null})
175 * @param optionWatcher
176 * The option watcher (may be {@code null})
178 public DefaultOption(T defaultValue, Predicate<T> validator, OptionWatcher<T> optionWatcher) {
179 this.defaultValue = defaultValue;
180 this.validator = validator;
181 this.optionWatcher = optionWatcher;
188 public T getDefault() {
197 return (value != null) ? value : defaultValue;
201 * Returns the real value of the option. This will also return an unset
202 * value (usually {@code null})!
204 * @return The real value of the option
215 public boolean validate(T value) {
216 return (validator == null) || (value == null) || validator.apply(value);
223 public void set(T value) {
224 if ((value != null) && (validator != null) && (!validator.apply(value))) {
225 throw new IllegalArgumentException("New Value (" + value + ") could not be validated.");
227 T oldValue = this.value;
229 if (!get().equals(oldValue)) {
230 if (optionWatcher != null) {
231 optionWatcher.optionChanged(this, oldValue, get());
238 /** Holds all {@link Boolean} {@link Option}s. */
239 private final Map<String, Option<Boolean>> booleanOptions = Collections.synchronizedMap(new HashMap<String, Option<Boolean>>());
241 /** Holds all {@link Integer} {@link Option}s. */
242 private final Map<String, Option<Integer>> integerOptions = Collections.synchronizedMap(new HashMap<String, Option<Integer>>());
244 /** Holds all {@link String} {@link Option}s. */
245 private final Map<String, Option<String>> stringOptions = Collections.synchronizedMap(new HashMap<String, Option<String>>());
247 /** Holds all {@link Enum} {@link Option}s. */
248 private final Map<String, Option<? extends Enum<?>>> enumOptions = Collections.synchronizedMap(new HashMap<String, Option<? extends Enum<?>>>());
251 * Adds a boolean option.
254 * The name of the option
255 * @param booleanOption
257 * @return The given option
259 public Option<Boolean> addBooleanOption(String name, Option<Boolean> booleanOption) {
260 booleanOptions.put(name, booleanOption);
261 return booleanOption;
265 * Returns the boolean option with the given name.
268 * The name of the option
269 * @return The option, or {@code null} if there is no option with the given
272 public Option<Boolean> getBooleanOption(String name) {
273 return booleanOptions.get(name);
277 * Adds an {@link Integer} {@link Option}.
280 * The name of the option
281 * @param integerOption
283 * @return The given option
285 public Option<Integer> addIntegerOption(String name, Option<Integer> integerOption) {
286 integerOptions.put(name, integerOption);
287 return integerOption;
291 * Returns an {@link Integer} {@link Option}.
294 * The name of the integer option to get
295 * @return The integer option, or {@code null} if there is no option with
298 public Option<Integer> getIntegerOption(String name) {
299 return integerOptions.get(name);
303 * Adds a {@link String} {@link Option}.
306 * The name of the option
307 * @param stringOption
309 * @return The given option
311 public Option<String> addStringOption(String name, Option<String> stringOption) {
312 stringOptions.put(name, stringOption);
317 * Returns a {@link String} {@link Option}.
320 * The name of the string option to get
321 * @return The string option, or {@code null} if there is no option with the
324 public Option<String> getStringOption(String name) {
325 return stringOptions.get(name);
329 * Adds an {@link Enum} {@link Option}.
334 * The name of the option
337 * @return The given option
339 public <T extends Enum<T>> Option<T> addEnumOption(String name, Option<T> enumOption) {
340 enumOptions.put(name, enumOption);
345 * Returns a {@link Enum} {@link Option}. As the type can probably not be
346 * interred correctly you could help the compiler by calling this method
351 * options.<SomeEnum> getEnumOption("SomeEnumOption").get();
357 * The name of the option
358 * @return The enum option, or {@code null} if there is no enum option with
361 @SuppressWarnings("unchecked")
362 public <T extends Enum<T>> Option<T> getEnumOption(String name) {
363 return (Option<T>) enumOptions.get(name);