Add method to get a string with default value.
[Sone.git] / src / main / java / net / pterodactylus / sone / freenet / fcp / AbstractCommand.java
1 /*
2  * Sone - AbstractCommand.java - Copyright © 2011–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.freenet.fcp;
19
20 import freenet.node.FSParseException;
21 import freenet.support.SimpleFieldSet;
22
23 /**
24  * Basic implementation of a {@link Command} with various helper methods to
25  * simplify processing of input parameters.
26  *
27  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
28  */
29 public abstract class AbstractCommand implements Command {
30
31         //
32         // PROTECTED METHODS
33         //
34
35         /**
36          * Returns a String value from the given simple field set.
37          *
38          * @param simpleFieldSet
39          *            The simple field set to get the value from
40          * @param key
41          *            The key of the value
42          * @return The String value
43          * @throws FcpException
44          *             if there is no value for the given key in the simple field
45          *             set, or the value can not be converted to a String
46          */
47         protected static String getString(SimpleFieldSet simpleFieldSet, String key) throws FcpException {
48                 try {
49                         return simpleFieldSet.getString(key);
50                 } catch (FSParseException fspe1) {
51                         throw new FcpException("Could not get parameter “" + key + "” as String.", fspe1);
52                 }
53         }
54
55         protected static String getString(SimpleFieldSet simpleFieldSet, String key, String defaultValue) {
56                 try {
57                         return simpleFieldSet.getString(key);
58                 } catch (FSParseException fspe1) {
59                         return defaultValue;
60                 }
61         }
62
63         /**
64          * Returns an int value from the given simple field set.
65          *
66          * @param simpleFieldSet
67          *            The simple field set to get the value from
68          * @param key
69          *            The key of the value
70          * @return The int value
71          * @throws FcpException
72          *             if there is no value for the given key in the simple field
73          *             set, or the value can not be converted to an int
74          */
75         protected static int getInt(SimpleFieldSet simpleFieldSet, String key) throws FcpException {
76                 try {
77                         return simpleFieldSet.getInt(key);
78                 } catch (FSParseException fspe1) {
79                         throw new FcpException("Could not get parameter “" + key + "” as int.", fspe1);
80                 }
81         }
82
83         /**
84          * Returns an int value from the given simple field set, returning a default
85          * value if the value can not be found or converted.
86          *
87          * @param simpleFieldSet
88          *            The simple field set to get the value from
89          * @param key
90          *            The key of the value
91          * @param defaultValue
92          *            The default value
93          * @return The int value
94          */
95         protected static int getInt(SimpleFieldSet simpleFieldSet, String key, int defaultValue) {
96                 return simpleFieldSet.getInt(key, defaultValue);
97         }
98
99         /**
100          * Returns a boolean value from the given simple field set.
101          *
102          * @param simpleFieldSet
103          *            The simple field set to get the value from
104          * @param key
105          *            The key of the value
106          * @return The boolean value
107          * @throws FcpException
108          *             if there is no value for the given key in the simple field
109          *             set, or the value can not be converted to a boolean
110          */
111         protected static boolean getBoolean(SimpleFieldSet simpleFieldSet, String key) throws FcpException {
112                 try {
113                         return simpleFieldSet.getBoolean(key);
114                 } catch (FSParseException fspe1) {
115                         throw new FcpException("Could not get parameter “" + key + "” as boolean.", fspe1);
116                 }
117         }
118
119         /**
120          * Returns a boolean value from the given simple field set, returning a
121          * default value if the value can not be found or converted.
122          *
123          * @param simpleFieldSet
124          *            The simple field set to get the value from
125          * @param key
126          *            The key of the value
127          * @param defaultValue
128          *            The default value
129          * @return The boolean value
130          */
131         protected static boolean getBoolean(SimpleFieldSet simpleFieldSet, String key, boolean defaultValue) {
132                 return simpleFieldSet.getBoolean(key, defaultValue);
133         }
134
135 }