2 * Sone - AbstractCommand.java - Copyright © 2011–2016 David Roden
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.
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.
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/>.
18 package net.pterodactylus.sone.freenet.fcp;
20 import freenet.node.FSParseException;
21 import freenet.support.SimpleFieldSet;
24 * Basic implementation of a {@link Command} with various helper methods to
25 * simplify processing of input parameters.
27 public abstract class AbstractCommand implements Command {
34 * Returns a String value from the given simple field set.
36 * @param simpleFieldSet
37 * The simple field set to get the value from
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
45 protected static String getString(SimpleFieldSet simpleFieldSet, String key) throws FcpException {
47 return simpleFieldSet.getString(key);
48 } catch (FSParseException fspe1) {
49 throw new FcpException("Could not get parameter “" + key + "” as String.", fspe1);
54 * Returns an int value from the given simple field set.
56 * @param simpleFieldSet
57 * The simple field set to get the value from
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
65 protected static int getInt(SimpleFieldSet simpleFieldSet, String key) throws FcpException {
67 return simpleFieldSet.getInt(key);
68 } catch (FSParseException fspe1) {
69 throw new FcpException("Could not get parameter “" + key + "” as int.", fspe1);
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.
77 * @param simpleFieldSet
78 * The simple field set to get the value from
80 * The key of the value
83 * @return The int value
85 protected static int getInt(SimpleFieldSet simpleFieldSet, String key, int defaultValue) {
86 return simpleFieldSet.getInt(key, defaultValue);
90 * Returns a boolean value from the given simple field set.
92 * @param simpleFieldSet
93 * The simple field set to get the value from
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
101 protected static boolean getBoolean(SimpleFieldSet simpleFieldSet, String key) throws FcpException {
103 return simpleFieldSet.getBoolean(key);
104 } catch (FSParseException fspe1) {
105 throw new FcpException("Could not get parameter “" + key + "” as boolean.", fspe1);
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.
113 * @param simpleFieldSet
114 * The simple field set to get the value from
116 * The key of the value
117 * @param defaultValue
119 * @return The boolean value
121 protected static boolean getBoolean(SimpleFieldSet simpleFieldSet, String key, boolean defaultValue) {
122 return simpleFieldSet.getBoolean(key, defaultValue);