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>>());
230 * Adds a boolean option.
233 * The name of the option
234 * @param booleanOption
236 * @return The given option
238 public Option<Boolean> addBooleanOption(String name, Option<Boolean> booleanOption) {
239 booleanOptions.put(name, booleanOption);
240 return booleanOption;
244 * Returns the boolean option with the given name.
247 * The name of the option
248 * @return The option, or {@code null} if there is no option with the given
251 public Option<Boolean> getBooleanOption(String name) {
252 return booleanOptions.get(name);
256 * Adds an {@link Integer} {@link Option}.
259 * The name of the option
260 * @param integerOption
262 * @return The given option
264 public Option<Integer> addIntegerOption(String name, Option<Integer> integerOption) {
265 integerOptions.put(name, integerOption);
266 return integerOption;
270 * Returns an {@link Integer} {@link Option}.
273 * The name of the integer option to get
274 * @return The integer option, or {@code null} if there is no option with
277 public Option<Integer> getIntegerOption(String name) {
278 return integerOptions.get(name);
282 * Adds a {@link String} {@link Option}.
285 * The name of the option
286 * @param stringOption
288 * @return The given option
290 public Option<String> addStringOption(String name, Option<String> stringOption) {
291 stringOptions.put(name, stringOption);
296 * Returns a {@link String} {@link Option}.
299 * The name of the string option to get
300 * @return The string option, or {@code null} if there is no option with the
303 public Option<String> getStringOption(String name) {
304 return stringOptions.get(name);