2 * Sone - AbstractCommand.java - Copyright © 2011–2012 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 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);
56 * Returns an int value from the given simple field set.
58 * @param simpleFieldSet
59 * The simple field set to get the value from
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
67 protected int getInt(SimpleFieldSet simpleFieldSet, String key) throws FcpException {
69 return simpleFieldSet.getInt(key);
70 } catch (FSParseException fspe1) {
71 throw new FcpException("Could not get parameter “" + key + "” as int.", fspe1);
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.
79 * @param simpleFieldSet
80 * The simple field set to get the value from
82 * The key of the value
85 * @return The int value
87 protected int getInt(SimpleFieldSet simpleFieldSet, String key, int defaultValue) {
88 return simpleFieldSet.getInt(key, defaultValue);
92 * Returns a boolean value from the given simple field set.
94 * @param simpleFieldSet
95 * The simple field set to get the value from
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
103 protected boolean getBoolean(SimpleFieldSet simpleFieldSet, String key) throws FcpException {
105 return simpleFieldSet.getBoolean(key);
106 } catch (FSParseException fspe1) {
107 throw new FcpException("Could not get parameter “" + key + "” as boolean.", fspe1);
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.
115 * @param simpleFieldSet
116 * The simple field set to get the value from
118 * The key of the value
119 * @param defaultValue
121 * @return The boolean value
123 protected boolean getBoolean(SimpleFieldSet simpleFieldSet, String key, boolean defaultValue) {
124 return simpleFieldSet.getBoolean(key, defaultValue);