🔀 Merge “release/v81” into “master”
[Sone.git] / src / main / java / net / pterodactylus / sone / freenet / fcp / AbstractCommand.java
1 /*
2  * Sone - AbstractCommand.java - Copyright Â© 2011–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.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 public abstract class AbstractCommand implements Command {
28
29         //
30         // PROTECTED METHODS
31         //
32
33         /**
34          * Returns a String value from the given simple field set.
35          *
36          * @param simpleFieldSet
37          *            The simple field set to get the value from
38          * @param key
39          *            The key of the value
40          * @return The String value
41          * @throws FcpException
42          *             if there is no value for the given key in the simple field
43          *             set, or the value can not be converted to a String
44          */
45         protected static String getString(SimpleFieldSet simpleFieldSet, String key) throws FcpException {
46                 try {
47                         return simpleFieldSet.getString(key);
48                 } catch (FSParseException fspe1) {
49                         throw new FcpException("Could not get parameter â€ś" + key + "” as String.", fspe1);
50                 }
51         }
52
53         /**
54          * Returns an int value from the given simple field set.
55          *
56          * @param simpleFieldSet
57          *            The simple field set to get the value from
58          * @param key
59          *            The key of the value
60          * @return The int value
61          * @throws FcpException
62          *             if there is no value for the given key in the simple field
63          *             set, or the value can not be converted to an int
64          */
65         protected static int getInt(SimpleFieldSet simpleFieldSet, String key) throws FcpException {
66                 try {
67                         return simpleFieldSet.getInt(key);
68                 } catch (FSParseException fspe1) {
69                         throw new FcpException("Could not get parameter â€ś" + key + "” as int.", fspe1);
70                 }
71         }
72
73         /**
74          * Returns an int value from the given simple field set, returning a default
75          * value if the value can not be found or converted.
76          *
77          * @param simpleFieldSet
78          *            The simple field set to get the value from
79          * @param key
80          *            The key of the value
81          * @param defaultValue
82          *            The default value
83          * @return The int value
84          */
85         protected static int getInt(SimpleFieldSet simpleFieldSet, String key, int defaultValue) {
86                 return simpleFieldSet.getInt(key, defaultValue);
87         }
88
89         /**
90          * Returns a boolean value from the given simple field set.
91          *
92          * @param simpleFieldSet
93          *            The simple field set to get the value from
94          * @param key
95          *            The key of the value
96          * @return The boolean value
97          * @throws FcpException
98          *             if there is no value for the given key in the simple field
99          *             set, or the value can not be converted to a boolean
100          */
101         protected static boolean getBoolean(SimpleFieldSet simpleFieldSet, String key) throws FcpException {
102                 try {
103                         return simpleFieldSet.getBoolean(key);
104                 } catch (FSParseException fspe1) {
105                         throw new FcpException("Could not get parameter â€ś" + key + "” as boolean.", fspe1);
106                 }
107         }
108
109         /**
110          * Returns a boolean value from the given simple field set, returning a
111          * default value if the value can not be found or converted.
112          *
113          * @param simpleFieldSet
114          *            The simple field set to get the value from
115          * @param key
116          *            The key of the value
117          * @param defaultValue
118          *            The default value
119          * @return The boolean value
120          */
121         protected static boolean getBoolean(SimpleFieldSet simpleFieldSet, String key, boolean defaultValue) {
122                 return simpleFieldSet.getBoolean(key, defaultValue);
123         }
124
125 }