1 package net.pterodactylus.sone.core;
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.HashMap;
10 * Stores various options that influence Sone’s behaviour.
12 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
14 public class Options {
17 * Contains current and default value of an option.
20 * The type of the option
21 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
23 public static interface Option<T> {
26 * Returns the default value of the option.
28 * @return The default value of the option
30 public T getDefault();
33 * Returns the current value of the option. If the current value is not
34 * set (usually {@code null}), the default value is returned.
36 * @return The current value of the option
41 * Returns the real value of the option. This will also return an unset
42 * value (usually {@code null})!
44 * @return The real value of the option
49 * Sets the current value of the option.
52 * The new value of the option
54 public void set(T value);
59 * Interface for objects that want to be notified when an option changes its
63 * The type of the option
64 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
66 public static interface OptionWatcher<T> {
69 * Notifies an object that an option has been changed.
72 * The option that has changed
74 * The old value of the option
76 * The new value of the option
78 public void optionChanged(Option<T> option, T oldValue, T newValue);
83 * Basic implementation of an {@link Option} that notifies an
84 * {@link OptionWatcher} if the value changes.
87 * The type of the option
88 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
90 public static class DefaultOption<T> implements Option<T> {
92 /** The default value. */
93 private final T defaultValue;
95 /** The current value. */
98 /** The option watcher. */
99 private final List<OptionWatcher<T>> optionWatchers = new ArrayList<OptionWatcher<T>>();
102 * Creates a new default option.
104 * @param defaultValue
105 * The default value of the option
106 * @param optionWatchers
107 * The option watchers
109 public DefaultOption(T defaultValue, OptionWatcher<T>... optionWatchers) {
110 this.defaultValue = defaultValue;
111 this.optionWatchers.addAll(Arrays.asList(optionWatchers));
118 public T getDefault() {
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 void set(T value) {
146 T oldValue = this.value;
148 if (!get().equals(oldValue)) {
149 for (OptionWatcher<T> optionWatcher : optionWatchers) {
150 optionWatcher.optionChanged(this, oldValue, get());
157 /** Holds all {@link Boolean} {@link Option}s. */
158 private final Map<String, Option<Boolean>> booleanOptions = new HashMap<String, Option<Boolean>>();
160 /** Holds all {@link Integer} {@link Option}s. */
161 private final Map<String, Option<Integer>> integerOptions = new HashMap<String, Option<Integer>>();
164 * Adds a boolean option.
167 * The name of the option
168 * @param booleanOption
170 * @return The given option
172 public Option<Boolean> addBooleanOption(String name, Option<Boolean> booleanOption) {
173 booleanOptions.put(name, booleanOption);
174 return booleanOption;
178 * Returns the boolean option with the given name.
181 * The name of the option
182 * @return The option, or {@code null} if there is no option with the given
185 public Option<Boolean> getBooleanOption(String name) {
186 return booleanOptions.get(name);
190 * Adds an {@link Integer} {@link Option}.
193 * The name of the option
194 * @param integerOption
196 * @return The given option
198 public Option<Integer> addIntegerOption(String name, Option<Integer> integerOption) {
199 integerOptions.put(name, integerOption);
200 return integerOption;
204 * Returns an {@link Integer} {@link Option}.
207 * The name of the integer option to get
208 * @return The integer option, or {@code null} if there is no option with
211 public Option<Integer> getIntegerOption(String name) {
212 return integerOptions.get(name);