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