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