X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fwotns%2Ffreenet%2Ffcp%2FAbstractCommand.java;fp=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fwotns%2Ffreenet%2Ffcp%2FAbstractCommand.java;h=01b103c964dbeb3784c62dddb503e821d12429fb;hb=622c4a4d3ebed447d5708a41cf3e1e82e18fa29b;hp=0000000000000000000000000000000000000000;hpb=77869390c46e8e5eff63bf00c7ef44a37ba8f317;p=WoTNS.git diff --git a/src/main/java/net/pterodactylus/wotns/freenet/fcp/AbstractCommand.java b/src/main/java/net/pterodactylus/wotns/freenet/fcp/AbstractCommand.java new file mode 100644 index 0000000..01b103c --- /dev/null +++ b/src/main/java/net/pterodactylus/wotns/freenet/fcp/AbstractCommand.java @@ -0,0 +1,127 @@ +/* + * Sone - AbstractCommand.java - Copyright © 2011 David Roden + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.pterodactylus.wotns.freenet.fcp; + +import freenet.node.FSParseException; +import freenet.support.SimpleFieldSet; + +/** + * Basic implementation of a {@link Command} with various helper methods to + * simplify processing of input parameters. + * + * @author David ‘Bombe’ Roden + */ +public abstract class AbstractCommand implements Command { + + // + // PROTECTED METHODS + // + + /** + * Returns a String value from the given simple field set. + * + * @param simpleFieldSet + * The simple field set to get the value from + * @param key + * The key of the value + * @return The String value + * @throws FcpException + * if there is no value for the given key in the simple field + * set, or the value can not be converted to a String + */ + protected String getString(SimpleFieldSet simpleFieldSet, String key) throws FcpException { + try { + return simpleFieldSet.getString(key); + } catch (FSParseException fspe1) { + throw new FcpException("Could not get parameter “" + key + "” as String.", fspe1); + } + } + + /** + * Returns an int value from the given simple field set. + * + * @param simpleFieldSet + * The simple field set to get the value from + * @param key + * The key of the value + * @return The int value + * @throws FcpException + * if there is no value for the given key in the simple field + * set, or the value can not be converted to an int + */ + protected int getInt(SimpleFieldSet simpleFieldSet, String key) throws FcpException { + try { + return simpleFieldSet.getInt(key); + } catch (FSParseException fspe1) { + throw new FcpException("Could not get parameter “" + key + "” as int.", fspe1); + } + } + + /** + * Returns an int value from the given simple field set, returning a default + * value if the value can not be found or converted. + * + * @param simpleFieldSet + * The simple field set to get the value from + * @param key + * The key of the value + * @param defaultValue + * The default value + * @return The int value + */ + protected int getInt(SimpleFieldSet simpleFieldSet, String key, int defaultValue) { + return simpleFieldSet.getInt(key, defaultValue); + } + + /** + * Returns a boolean value from the given simple field set. + * + * @param simpleFieldSet + * The simple field set to get the value from + * @param key + * The key of the value + * @return The boolean value + * @throws FcpException + * if there is no value for the given key in the simple field + * set, or the value can not be converted to a boolean + */ + protected boolean getBoolean(SimpleFieldSet simpleFieldSet, String key) throws FcpException { + try { + return simpleFieldSet.getBoolean(key); + } catch (FSParseException fspe1) { + throw new FcpException("Could not get parameter “" + key + "” as boolean.", fspe1); + } + } + + /** + * Returns a boolean value from the given simple field set, returning a + * default value if the value can not be found or converted. + * + * @param simpleFieldSet + * The simple field set to get the value from + * @param key + * The key of the value + * @param defaultValue + * The default value + * @return The boolean value + */ + protected boolean getBoolean(SimpleFieldSet simpleFieldSet, String key, boolean defaultValue) { + return simpleFieldSet.getBoolean(key, defaultValue); + } + +}