fa5a3723e4575ee17c4e9622dc4a48ddec9ce6bf
[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         /**
56          * Returns an int value from the given simple field set.
57          *
58          * @param simpleFieldSet
59          *            The simple field set to get the value from
60          * @param key
61          *            The key of the value
62          * @return The int value
63          * @throws FcpException
64          *             if there is no value for the given key in the simple field
65          *             set, or the value can not be converted to an int
66          */
67         protected static int getInt(SimpleFieldSet simpleFieldSet, String key) throws FcpException {
68                 try {
69                         return simpleFieldSet.getInt(key);
70                 } catch (FSParseException fspe1) {
71                         throw new FcpException("Could not get parameter “" + key + "” as int.", fspe1);
72                 }
73         }
74
75         /**
76          * Returns an int value from the given simple field set, returning a default
77          * value if the value can not be found or converted.
78          *
79          * @param simpleFieldSet
80          *            The simple field set to get the value from
81          * @param key
82          *            The key of the value
83          * @param defaultValue
84          *            The default value
85          * @return The int value
86          */
87         protected static int getInt(SimpleFieldSet simpleFieldSet, String key, int defaultValue) {
88                 return simpleFieldSet.getInt(key, defaultValue);
89         }
90
91         /**
92          * Returns a boolean value from the given simple field set.
93          *
94          * @param simpleFieldSet
95          *            The simple field set to get the value from
96          * @param key
97          *            The key of the value
98          * @return The boolean value
99          * @throws FcpException
100          *             if there is no value for the given key in the simple field
101          *             set, or the value can not be converted to a boolean
102          */
103         protected static boolean getBoolean(SimpleFieldSet simpleFieldSet, String key) throws FcpException {
104                 try {
105                         return simpleFieldSet.getBoolean(key);
106                 } catch (FSParseException fspe1) {
107                         throw new FcpException("Could not get parameter “" + key + "” as boolean.", fspe1);
108                 }
109         }
110
111         /**
112          * Returns a boolean value from the given simple field set, returning a
113          * default value if the value can not be found or converted.
114          *
115          * @param simpleFieldSet
116          *            The simple field set to get the value from
117          * @param key
118          *            The key of the value
119          * @param defaultValue
120          *            The default value
121          * @return The boolean value
122          */
123         protected static boolean getBoolean(SimpleFieldSet simpleFieldSet, String key, boolean defaultValue) {
124                 return simpleFieldSet.getBoolean(key, defaultValue);
125         }
126
127 }