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 * Basic implementation of an {@link Option}.
85 * The type of the option
86 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
88 public static class DefaultOption<T> implements Option<T> {
90 /** The default value. */
91 private final T defaultValue;
93 /** The current value. */
94 private volatile T value;
97 private Predicate<T> validator;
100 * Creates a new default option.
102 * @param defaultValue
103 * The default value of the option
105 public DefaultOption(T defaultValue) {
106 this(defaultValue, null);
110 * Creates a new default option.
112 * @param defaultValue
113 * The default value of the option
115 * The validator for value validation (may be {@code null})
117 public DefaultOption(T defaultValue, Predicate<T> validator) {
118 this.defaultValue = defaultValue;
119 this.validator = validator;
127 return (value != null) ? value : defaultValue;
131 * Returns the real value of the option. This will also return an unset
132 * value (usually {@code null})!
134 * @return The real value of the option
145 public boolean validate(T value) {
146 return (validator == null) || (value == null) || validator.apply(value);
153 public void set(T value) {
154 if ((value != null) && (validator != null) && (!validator.apply(value))) {
155 throw new IllegalArgumentException("New Value (" + value + ") could not be validated.");
157 T oldValue = this.value;
163 /** Holds all {@link Boolean} {@link Option}s. */
164 private final Map<String, Option<Boolean>> booleanOptions = Collections.synchronizedMap(new HashMap<String, Option<Boolean>>());
166 /** Holds all {@link Integer} {@link Option}s. */
167 private final Map<String, Option<Integer>> integerOptions = Collections.synchronizedMap(new HashMap<String, Option<Integer>>());
169 /** Holds all {@link String} {@link Option}s. */
170 private final Map<String, Option<String>> stringOptions = Collections.synchronizedMap(new HashMap<String, Option<String>>());
172 /** Holds all {@link Enum} {@link Option}s. */
173 private final Map<String, Option<? extends Enum<?>>> enumOptions = Collections.synchronizedMap(new HashMap<String, Option<? extends Enum<?>>>());
176 * Adds a boolean option.
179 * The name of the option
180 * @param booleanOption
182 * @return The given option
184 public Option<Boolean> addBooleanOption(String name, Option<Boolean> booleanOption) {
185 booleanOptions.put(name, booleanOption);
186 return booleanOption;
190 * Returns the boolean option with the given name.
193 * The name of the option
194 * @return The option, or {@code null} if there is no option with the given
197 public Option<Boolean> getBooleanOption(String name) {
198 return booleanOptions.get(name);
202 * Adds an {@link Integer} {@link Option}.
205 * The name of the option
206 * @param integerOption
208 * @return The given option
210 public Option<Integer> addIntegerOption(String name, Option<Integer> integerOption) {
211 integerOptions.put(name, integerOption);
212 return integerOption;
216 * Returns an {@link Integer} {@link Option}.
219 * The name of the integer option to get
220 * @return The integer option, or {@code null} if there is no option with
223 public Option<Integer> getIntegerOption(String name) {
224 return integerOptions.get(name);
228 * Adds a {@link String} {@link Option}.
231 * The name of the option
232 * @param stringOption
234 * @return The given option
236 public Option<String> addStringOption(String name, Option<String> stringOption) {
237 stringOptions.put(name, stringOption);
242 * Returns a {@link String} {@link Option}.
245 * The name of the string option to get
246 * @return The string option, or {@code null} if there is no option with the
249 public Option<String> getStringOption(String name) {
250 return stringOptions.get(name);
254 * Adds an {@link Enum} {@link Option}.
259 * The name of the option
262 * @return The given option
264 public <T extends Enum<T>> Option<T> addEnumOption(String name, Option<T> enumOption) {
265 enumOptions.put(name, enumOption);
270 * Returns a {@link Enum} {@link Option}. As the type can probably not be
271 * interred correctly you could help the compiler by calling this method
276 * options.<SomeEnum> getEnumOption("SomeEnumOption").get();
282 * The name of the option
283 * @return The enum option, or {@code null} if there is no enum option with
286 @SuppressWarnings("unchecked")
287 public <T extends Enum<T>> Option<T> getEnumOption(String name) {
288 return (Option<T>) enumOptions.get(name);