1 package net.pterodactylus.sone.core;
3 import java.util.HashMap;
7 * Stores various options that influence Sone’s behaviour.
9 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
11 public class Options {
14 * Contains current and default value of an option.
17 * The type of the option
18 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
20 public static interface Option<T> {
23 * Returns the default value of the option.
25 * @return The default value of the option
27 public T getDefault();
30 * Returns the current value of the option. If the current value is not
31 * set (usually {@code null}), the default value is returned.
33 * @return The current value of the option
38 * Returns the real value of the option. This will also return an unset
39 * value (usually {@code null})!
41 * @return The real value of the option
46 * Sets the current value of the option.
49 * The new value of the option
51 public void set(T value);
56 * Interface for objects that want to be notified when an option changes its
60 * The type of the option
61 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
63 public static interface OptionWatcher<T> {
66 * Notifies an object that an option has been changed.
69 * The option that has changed
71 * The old value of the option
73 * The new value of the option
75 public void optionChanged(Option<T> option, T oldValue, T newValue);
80 * Basic implementation of an {@link Option} that notifies an
81 * {@link OptionWatcher} if the value changes.
84 * The type of the option
85 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
87 public static class DefaultOption<T> implements Option<T> {
89 /** The default value. */
90 private final T defaultValue;
92 /** The current value. */
95 /** The option watcher. */
96 private final OptionWatcher<T> optionWatcher;
99 * Creates a new default option.
101 * @param defaultValue
102 * The default value of the option
103 * @param optionWatcher
106 public DefaultOption(T defaultValue, OptionWatcher<T> optionWatcher) {
107 this.defaultValue = defaultValue;
108 this.optionWatcher = optionWatcher;
115 public T getDefault() {
124 return (value != null) ? value : defaultValue;
128 * Returns the real value of the option. This will also return an unset
129 * value (usually {@code null})!
131 * @return The real value of the option
142 public void set(T value) {
143 T oldValue = this.value;
145 if (!get().equals(oldValue)) {
146 optionWatcher.optionChanged(this, oldValue, get());
152 /** Holds all {@link Integer} {@link Option}s. */
153 private final Map<String, Option<Integer>> integerOptions = new HashMap<String, Option<Integer>>();
156 * Adds an {@link Integer} {@link Option}.
159 * The name of the option
160 * @param integerOption
162 * @return The given option
164 public Option<Integer> addIntegerOption(String name, Option<Integer> integerOption) {
165 integerOptions.put(name, integerOption);
166 return integerOption;
170 * Returns an {@link Integer} {@link Option}.
173 * The name of the integer option to get
174 * @return The integer option, or {@code null} if there is no option with
177 public Option<Integer> getIntegerOption(String name) {
178 return integerOptions.get(name);