🔀 Merge branch 'release-79'
[Sone.git] / src / main / java / net / pterodactylus / sone / core / Options.java
1 /*
2  * Sone - Options.java - Copyright Â© 2010–2019 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.sone.core;
19
20 import java.util.Collections;
21 import java.util.HashMap;
22 import java.util.Map;
23
24 import net.pterodactylus.sone.utils.Option;
25
26 import com.google.common.base.Predicate;
27
28 /**
29  * Stores various options that influence Sone’s behaviour.
30  */
31 public class Options {
32
33         /** Holds all {@link Boolean} {@link Option}s. */
34         private final Map<String, Option<Boolean>> booleanOptions = Collections.synchronizedMap(new HashMap<String, Option<Boolean>>());
35
36         /** Holds all {@link Integer} {@link Option}s. */
37         private final Map<String, Option<Integer>> integerOptions = Collections.synchronizedMap(new HashMap<String, Option<Integer>>());
38
39         /** Holds all {@link String} {@link Option}s. */
40         private final Map<String, Option<String>> stringOptions = Collections.synchronizedMap(new HashMap<String, Option<String>>());
41
42         /** Holds all {@link Enum} {@link Option}s. */
43         private final Map<String, Option<? extends Enum<?>>> enumOptions = Collections.synchronizedMap(new HashMap<String, Option<? extends Enum<?>>>());
44
45         /**
46          * Adds a boolean option.
47          *
48          * @param name
49          *            The name of the option
50          * @param booleanOption
51          *            The option
52          * @return The given option
53          */
54         public Option<Boolean> addBooleanOption(String name, Option<Boolean> booleanOption) {
55                 booleanOptions.put(name, booleanOption);
56                 return booleanOption;
57         }
58
59         /**
60          * Returns the boolean option with the given name.
61          *
62          * @param name
63          *            The name of the option
64          * @return The option, or {@code null} if there is no option with the given
65          *         name
66          */
67         public Option<Boolean> getBooleanOption(String name) {
68                 return booleanOptions.get(name);
69         }
70
71         /**
72          * Adds an {@link Integer} {@link Option}.
73          *
74          * @param name
75          *            The name of the option
76          * @param integerOption
77          *            The option
78          * @return The given option
79          */
80         public Option<Integer> addIntegerOption(String name, Option<Integer> integerOption) {
81                 integerOptions.put(name, integerOption);
82                 return integerOption;
83         }
84
85         /**
86          * Returns an {@link Integer} {@link Option}.
87          *
88          * @param name
89          *            The name of the integer option to get
90          * @return The integer option, or {@code null} if there is no option with
91          *         the given name
92          */
93         public Option<Integer> getIntegerOption(String name) {
94                 return integerOptions.get(name);
95         }
96
97         /**
98          * Adds a {@link String} {@link Option}.
99          *
100          * @param name
101          *            The name of the option
102          * @param stringOption
103          *            The option
104          * @return The given option
105          */
106         public Option<String> addStringOption(String name, Option<String> stringOption) {
107                 stringOptions.put(name, stringOption);
108                 return stringOption;
109         }
110
111         /**
112          * Returns a {@link String} {@link Option}.
113          *
114          * @param name
115          *            The name of the string option to get
116          * @return The string option, or {@code null} if there is no option with the
117          *         given name
118          */
119         public Option<String> getStringOption(String name) {
120                 return stringOptions.get(name);
121         }
122
123         /**
124          * Adds an {@link Enum} {@link Option}.
125          *
126          * @param <T>
127          *            The enum type
128          * @param name
129          *            The name of the option
130          * @param enumOption
131          *            The option
132          * @return The given option
133          */
134         public <T extends Enum<T>> Option<T> addEnumOption(String name, Option<T> enumOption) {
135                 enumOptions.put(name, enumOption);
136                 return enumOption;
137         }
138
139         /**
140          * Returns a {@link Enum} {@link Option}. As the type can probably not be
141          * interred correctly you could help the compiler by calling this method
142          * like this:
143          * <p>
144          *
145          * <pre>
146          * options.&lt;SomeEnum&gt; getEnumOption(&quot;SomeEnumOption&quot;).get();
147          * </pre>
148          *
149          * @param <T>
150          *            The enum type
151          * @param name
152          *            The name of the option
153          * @return The enum option, or {@code null} if there is no enum option with
154          *         the given name
155          */
156         @SuppressWarnings("unchecked")
157         public <T extends Enum<T>> Option<T> getEnumOption(String name) {
158                 return (Option<T>) enumOptions.get(name);
159         }
160
161 }