2 * Sone - Options.java - Copyright © 2010 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.ArrayList;
21 import java.util.Arrays;
22 import java.util.Collections;
23 import java.util.HashMap;
24 import java.util.List;
27 import net.pterodactylus.util.validation.Validator;
30 * Stores various options that influence Sone’s behaviour.
32 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
34 public class Options {
37 * Contains current and default value of an option.
40 * The type of the option
41 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
43 public static interface Option<T> {
46 * Returns the default value of the option.
48 * @return The default value of the option
50 public T getDefault();
53 * Returns the current value of the option. If the current value is not
54 * set (usually {@code null}), the default value is returned.
56 * @return The current value of the option
61 * Returns the real value of the option. This will also return an unset
62 * value (usually {@code null})!
64 * @return The real value of the option
69 * Validates the given value. Note that {@code null} is always a valid
73 * The value to validate
74 * @return {@code true} if this option does not have a {@link Validator}
75 * , or the {@link Validator} validates this object, {@code
78 public boolean validate(T value);
81 * Sets the current value of the option.
84 * The new value of the option
85 * @throws IllegalArgumentException
86 * if the value is not valid for this option
88 public void set(T value) throws IllegalArgumentException;
93 * Interface for objects that want to be notified when an option changes its
97 * The type of the option
98 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
100 public static interface OptionWatcher<T> {
103 * Notifies an object that an option has been changed.
106 * The option that has changed
108 * The old value of the option
110 * The new value of the option
112 public void optionChanged(Option<T> option, T oldValue, T newValue);
117 * Basic implementation of an {@link Option} that notifies an
118 * {@link OptionWatcher} if the value changes.
121 * The type of the option
122 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
124 public static class DefaultOption<T> implements Option<T> {
126 /** The default value. */
127 private final T defaultValue;
129 /** The current value. */
130 private volatile T value;
132 /** The validator. */
133 private Validator<T> validator;
135 /** The option watcher. */
136 private final List<OptionWatcher<T>> optionWatchers = new ArrayList<OptionWatcher<T>>();
139 * Creates a new default option.
141 * @param defaultValue
142 * The default value of the option
143 * @param optionWatchers
144 * The option watchers
146 public DefaultOption(T defaultValue, OptionWatcher<T>... optionWatchers) {
147 this(defaultValue, null, optionWatchers);
151 * Creates a new default option.
153 * @param defaultValue
154 * The default value of the option
156 * The validator for value validation
157 * @param optionWatchers
158 * The option watchers
160 public DefaultOption(T defaultValue, Validator<T> validator, OptionWatcher<T>... optionWatchers) {
161 this.defaultValue = defaultValue;
162 this.validator = validator;
163 this.optionWatchers.addAll(Arrays.asList(optionWatchers));
170 public T getDefault() {
179 return (value != null) ? value : defaultValue;
183 * Returns the real value of the option. This will also return an unset
184 * value (usually {@code null})!
186 * @return The real value of the option
197 public boolean validate(T value) {
198 return (validator == null) || (value == null) || validator.validate(value);
205 public void set(T value) {
206 if ((value != null) && (validator != null) && (!validator.validate(value))) {
207 throw new IllegalArgumentException("New Value (" + value + ") could not be validated.");
209 T oldValue = this.value;
211 if (!get().equals(oldValue)) {
212 for (OptionWatcher<T> optionWatcher : optionWatchers) {
213 optionWatcher.optionChanged(this, oldValue, get());
220 /** Holds all {@link Boolean} {@link Option}s. */
221 private final Map<String, Option<Boolean>> booleanOptions = Collections.synchronizedMap(new HashMap<String, Option<Boolean>>());
223 /** Holds all {@link Integer} {@link Option}s. */
224 private final Map<String, Option<Integer>> integerOptions = Collections.synchronizedMap(new HashMap<String, Option<Integer>>());
226 /** Holds all {@link String} {@link Option}s. */
227 private final Map<String, Option<String>> stringOptions = Collections.synchronizedMap(new HashMap<String, Option<String>>());
229 /** Holds all {@link Enum} {@link Option}s. */
230 private final Map<String, Option<? extends Enum<?>>> enumOptions = Collections.synchronizedMap(new HashMap<String, Option<? extends Enum<?>>>());
233 * Adds a boolean option.
236 * The name of the option
237 * @param booleanOption
239 * @return The given option
241 public Option<Boolean> addBooleanOption(String name, Option<Boolean> booleanOption) {
242 booleanOptions.put(name, booleanOption);
243 return booleanOption;
247 * Returns the boolean option with the given name.
250 * The name of the option
251 * @return The option, or {@code null} if there is no option with the given
254 public Option<Boolean> getBooleanOption(String name) {
255 return booleanOptions.get(name);
259 * Adds an {@link Integer} {@link Option}.
262 * The name of the option
263 * @param integerOption
265 * @return The given option
267 public Option<Integer> addIntegerOption(String name, Option<Integer> integerOption) {
268 integerOptions.put(name, integerOption);
269 return integerOption;
273 * Returns an {@link Integer} {@link Option}.
276 * The name of the integer option to get
277 * @return The integer option, or {@code null} if there is no option with
280 public Option<Integer> getIntegerOption(String name) {
281 return integerOptions.get(name);
285 * Adds a {@link String} {@link Option}.
288 * The name of the option
289 * @param stringOption
291 * @return The given option
293 public Option<String> addStringOption(String name, Option<String> stringOption) {
294 stringOptions.put(name, stringOption);
299 * Returns a {@link String} {@link Option}.
302 * The name of the string option to get
303 * @return The string option, or {@code null} if there is no option with the
306 public Option<String> getStringOption(String name) {
307 return stringOptions.get(name);
311 * Adds an {@link Enum} {@link Option}.
314 * The name of the option
317 * @return The given option
319 public <T extends Enum<T>> Option<T> addEnumOption(String name, Option<T> enumOption) {
320 enumOptions.put(name, enumOption);
325 * Returns a {@link Enum} {@link Option}. As the type can probably not be
326 * interred correctly you could help the compiler by calling this method
331 * options.<SomeEnum> getEnumOption("SomeEnumOption").get();
335 * The name of the option
336 * @return The enum option, or {@code null} if there is no enum option with
339 public <T extends Enum<T>> Option<T> getEnumOption(String name) {
340 return (Option<T>) enumOptions.get(name);