2 * Sone - AbstractCommand.java - Copyright © 2011–2013 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 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
29 public abstract class AbstractCommand implements Command {
36 * Returns a String value from the given simple field set.
38 * @param simpleFieldSet
39 * The simple field set to get the value from
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
47 protected static String getString(SimpleFieldSet simpleFieldSet, String key) throws FcpException {
49 return simpleFieldSet.getString(key);
50 } catch (FSParseException fspe1) {
51 throw new FcpException("Could not get parameter “" + key + "” as String.", fspe1);
55 protected static String getString(SimpleFieldSet simpleFieldSet, String key, String defaultValue) {
57 return simpleFieldSet.getString(key);
58 } catch (FSParseException fspe1) {
64 * Returns an int value from the given simple field set.
66 * @param simpleFieldSet
67 * The simple field set to get the value from
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
75 protected static int getInt(SimpleFieldSet simpleFieldSet, String key) throws FcpException {
77 return simpleFieldSet.getInt(key);
78 } catch (FSParseException fspe1) {
79 throw new FcpException("Could not get parameter “" + key + "” as int.", fspe1);
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.
87 * @param simpleFieldSet
88 * The simple field set to get the value from
90 * The key of the value
93 * @return The int value
95 protected static int getInt(SimpleFieldSet simpleFieldSet, String key, int defaultValue) {
96 return simpleFieldSet.getInt(key, defaultValue);
100 * Returns a boolean value from the given simple field set.
102 * @param simpleFieldSet
103 * The simple field set to get the value from
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
111 protected static boolean getBoolean(SimpleFieldSet simpleFieldSet, String key) throws FcpException {
113 return simpleFieldSet.getBoolean(key);
114 } catch (FSParseException fspe1) {
115 throw new FcpException("Could not get parameter “" + key + "” as boolean.", fspe1);
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.
123 * @param simpleFieldSet
124 * The simple field set to get the value from
126 * The key of the value
127 * @param defaultValue
129 * @return The boolean value
131 protected static boolean getBoolean(SimpleFieldSet simpleFieldSet, String key, boolean defaultValue) {
132 return simpleFieldSet.getBoolean(key, defaultValue);