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
196 public boolean validate(T value) {
197 return (validator == null) || (value == null) || validator.validate(value);
204 public void set(T value) {
205 if ((value != null) && (validator != null) && (!validator.validate(value))) {
206 throw new IllegalArgumentException("New Value (" + value + ") could not be validated.");
208 T oldValue = this.value;
210 if (!get().equals(oldValue)) {
211 for (OptionWatcher<T> optionWatcher : optionWatchers) {
212 optionWatcher.optionChanged(this, oldValue, get());
219 /** Holds all {@link Boolean} {@link Option}s. */
220 private final Map<String, Option<Boolean>> booleanOptions = Collections.synchronizedMap(new HashMap<String, Option<Boolean>>());
222 /** Holds all {@link Integer} {@link Option}s. */
223 private final Map<String, Option<Integer>> integerOptions = Collections.synchronizedMap(new HashMap<String, Option<Integer>>());
225 /** Holds all {@link String} {@link Option}s. */
226 private final Map<String, Option<String>> stringOptions = Collections.synchronizedMap(new HashMap<String, Option<String>>());
229 * Adds a boolean option.
232 * The name of the option
233 * @param booleanOption
235 * @return The given option
237 public Option<Boolean> addBooleanOption(String name, Option<Boolean> booleanOption) {
238 booleanOptions.put(name, booleanOption);
239 return booleanOption;
243 * Returns the boolean option with the given name.
246 * The name of the option
247 * @return The option, or {@code null} if there is no option with the given
250 public Option<Boolean> getBooleanOption(String name) {
251 return booleanOptions.get(name);
255 * Adds an {@link Integer} {@link Option}.
258 * The name of the option
259 * @param integerOption
261 * @return The given option
263 public Option<Integer> addIntegerOption(String name, Option<Integer> integerOption) {
264 integerOptions.put(name, integerOption);
265 return integerOption;
269 * Returns an {@link Integer} {@link Option}.
272 * The name of the integer option to get
273 * @return The integer option, or {@code null} if there is no option with
276 public Option<Integer> getIntegerOption(String name) {
277 return integerOptions.get(name);
281 * Adds a {@link String} {@link Option}.
284 * The name of the option
285 * @param stringOption
287 * @return The given option
289 public Option<String> addStringOption(String name, Option<String> stringOption) {
290 stringOptions.put(name, stringOption);
295 * Returns a {@link String} {@link Option}.
298 * The name of the string option to get
299 * @return The string option, or {@code null} if there is no option with the
302 public Option<String> getStringOption(String name) {
303 return stringOptions.get(name);