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;
28 * Stores various options that influence Sone’s behaviour.
30 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
32 public class Options {
35 * Contains current and default value of an option.
38 * The type of the option
39 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
41 public static interface Option<T> {
44 * Returns the default value of the option.
46 * @return The default value of the option
48 public T getDefault();
51 * Returns the current value of the option. If the current value is not
52 * set (usually {@code null}), the default value is returned.
54 * @return The current value of the option
59 * Returns the real value of the option. This will also return an unset
60 * value (usually {@code null})!
62 * @return The real value of the option
67 * Sets the current value of the option.
70 * The new value of the option
72 public void set(T value);
77 * Interface for objects that want to be notified when an option changes its
81 * The type of the option
82 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
84 public static interface OptionWatcher<T> {
87 * Notifies an object that an option has been changed.
90 * The option that has changed
92 * The old value of the option
94 * The new value of the option
96 public void optionChanged(Option<T> option, T oldValue, T newValue);
101 * Basic implementation of an {@link Option} that notifies an
102 * {@link OptionWatcher} if the value changes.
105 * The type of the option
106 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
108 public static class DefaultOption<T> implements Option<T> {
110 /** The default value. */
111 private final T defaultValue;
113 /** The current value. */
114 private volatile T value;
116 /** The option watcher. */
117 private final List<OptionWatcher<T>> optionWatchers = new ArrayList<OptionWatcher<T>>();
120 * Creates a new default option.
122 * @param defaultValue
123 * The default value of the option
124 * @param optionWatchers
125 * The option watchers
127 public DefaultOption(T defaultValue, OptionWatcher<T>... optionWatchers) {
128 this.defaultValue = defaultValue;
129 this.optionWatchers.addAll(Arrays.asList(optionWatchers));
136 public T getDefault() {
145 return (value != null) ? value : defaultValue;
149 * Returns the real value of the option. This will also return an unset
150 * value (usually {@code null})!
152 * @return The real value of the option
163 public void set(T value) {
164 T oldValue = this.value;
166 if (!get().equals(oldValue)) {
167 for (OptionWatcher<T> optionWatcher : optionWatchers) {
168 optionWatcher.optionChanged(this, oldValue, get());
175 /** Holds all {@link Boolean} {@link Option}s. */
176 private final Map<String, Option<Boolean>> booleanOptions = Collections.synchronizedMap(new HashMap<String, Option<Boolean>>());
178 /** Holds all {@link Integer} {@link Option}s. */
179 private final Map<String, Option<Integer>> integerOptions = Collections.synchronizedMap(new HashMap<String, Option<Integer>>());
181 /** Holds all {@link String} {@link Option}s. */
182 private final Map<String, Option<String>> stringOptions = Collections.synchronizedMap(new HashMap<String, Option<String>>());
185 * Adds a boolean option.
188 * The name of the option
189 * @param booleanOption
191 * @return The given option
193 public Option<Boolean> addBooleanOption(String name, Option<Boolean> booleanOption) {
194 booleanOptions.put(name, booleanOption);
195 return booleanOption;
199 * Returns the boolean option with the given name.
202 * The name of the option
203 * @return The option, or {@code null} if there is no option with the given
206 public Option<Boolean> getBooleanOption(String name) {
207 return booleanOptions.get(name);
211 * Adds an {@link Integer} {@link Option}.
214 * The name of the option
215 * @param integerOption
217 * @return The given option
219 public Option<Integer> addIntegerOption(String name, Option<Integer> integerOption) {
220 integerOptions.put(name, integerOption);
221 return integerOption;
225 * Returns an {@link Integer} {@link Option}.
228 * The name of the integer option to get
229 * @return The integer option, or {@code null} if there is no option with
232 public Option<Integer> getIntegerOption(String name) {
233 return integerOptions.get(name);
237 * Adds a {@link String} {@link Option}.
240 * The name of the option
241 * @param stringOption
243 * @return The given option
245 public Option<String> addStringOption(String name, Option<String> stringOption) {
246 stringOptions.put(name, stringOption);
251 * Returns a {@link String} {@link Option}.
254 * The name of the string option to get
255 * @return The string option, or {@code null} if there is no option with the
258 public Option<String> getStringOption(String name) {
259 return stringOptions.get(name);