From: David ‘Bombe’ Roden Date: Fri, 11 Apr 2008 01:18:06 +0000 (+0000) Subject: simplify parsing of ints and longs X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=36b29f60a28c5bf236433fb1480156ba076c46f9;p=jSite2.git simplify parsing of ints and longs git-svn-id: http://trooper/svn/projects/jSite/trunk@703 c3eda9e8-030b-0410-8277-bc7414b0a119 --- diff --git a/src/net/pterodactylus/util/fcp/AllData.java b/src/net/pterodactylus/util/fcp/AllData.java index c2ead16..77c899c 100644 --- a/src/net/pterodactylus/util/fcp/AllData.java +++ b/src/net/pterodactylus/util/fcp/AllData.java @@ -51,11 +51,7 @@ public class AllData extends BaseMessage { * not be parsed */ public long getDataLength() { - try { - return Long.valueOf(getField("DataLength")); - } catch (NumberFormatException nfe1) { - return -1; - } + return FcpUtils.safeParseLong(getField("DataLength")); } /** @@ -65,11 +61,7 @@ public class AllData extends BaseMessage { * 1970 UTC), or -1 if the time could not be parsed */ public long getStartupTime() { - try { - return Long.valueOf(getField("StartupTime")); - } catch (NumberFormatException nfe1) { - return -1; - } + return FcpUtils.safeParseLong(getField("StartupTime")); } /** @@ -79,11 +71,7 @@ public class AllData extends BaseMessage { * 1970 UTC), or -1 if the time could not be parsed */ public long getCompletionTime() { - try { - return Long.valueOf(getField("CompletionTime")); - } catch (NumberFormatException nfe1) { - return -1; - } + return FcpUtils.safeParseLong(getField("CompletionTime")); } /** diff --git a/src/net/pterodactylus/util/fcp/FcpUtils.java b/src/net/pterodactylus/util/fcp/FcpUtils.java index 6f599e4..ad2a798 100644 --- a/src/net/pterodactylus/util/fcp/FcpUtils.java +++ b/src/net/pterodactylus/util/fcp/FcpUtils.java @@ -84,4 +84,64 @@ public class FcpUtils { return encodedField.toString(); } + /** + * Tries to parse the given string into an int, returning -1 + * if the string can not be parsed. + * + * @param value + * The string to parse + * @return The parsed int, or -1 + */ + public static int safeParseInt(String value) { + return safeParseInt(value, -1); + } + + /** + * Tries to parse the given string into an int, returning + * defaultValue if the string can not be parsed. + * + * @param value + * The string to parse + * @param defaultValue + * The value to return if the string can not be parsed. + * @return The parsed int, or defaultValue + */ + public static int safeParseInt(String value, int defaultValue) { + try { + return Integer.valueOf(value); + } catch (NumberFormatException nfe1) { + return defaultValue; + } + } + + /** + * Tries to parse the given string into an long, returning -1 + * if the string can not be parsed. + * + * @param value + * The string to parse + * @return The parsed long, or -1 + */ + public static long safeParseLong(String value) { + return safeParseLong(value, -1); + } + + /** + * Tries to parse the given string into an long, returning + * defaultValue if the string can not be parsed. + * + * @param value + * The string to parse + * @param defaultValue + * The value to return if the string can not be parsed. + * @return The parsed long, or defaultValue + */ + public static long safeParseLong(String value, long defaultValue) { + try { + return Integer.valueOf(value); + } catch (NumberFormatException nfe1) { + return defaultValue; + } + } + } diff --git a/src/net/pterodactylus/util/fcp/NodeHello.java b/src/net/pterodactylus/util/fcp/NodeHello.java index 8094013..921ac89 100644 --- a/src/net/pterodactylus/util/fcp/NodeHello.java +++ b/src/net/pterodactylus/util/fcp/NodeHello.java @@ -3,7 +3,6 @@ */ package net.pterodactylus.util.fcp; - /** * Some convenience methods for parsing a “NodeHello” message from the node. * @@ -40,13 +39,7 @@ public class NodeHello extends BaseMessage { * number could not be determined */ public int getBuildNumber() { - String build = getBuild(); - try { - return Integer.valueOf(build); - } catch (NumberFormatException nfe1) { - /* ignore. */ - } - return -1; + return FcpUtils.safeParseInt(getBuild()); } /** @@ -65,13 +58,7 @@ public class NodeHello extends BaseMessage { * number of compression codecs could not be determined */ public int getCompressionCodecsNumber() { - String compressionCodecs = getCompressionCodecs(); - try { - return Integer.valueOf(compressionCodecs); - } catch (NumberFormatException nfe1) { - /* ignore. */ - } - return -1; + return FcpUtils.safeParseInt(getCompressionCodecs()); } /** @@ -99,13 +86,7 @@ public class NodeHello extends BaseMessage { * if the build number could not be determined */ public int getExtBuildNumber() { - String extBuild = getExtBuild(); - try { - return Integer.valueOf(extBuild); - } catch (NumberFormatException nfe1) { - /* ignore. */ - } - return -1; + return FcpUtils.safeParseInt(getExtBuild()); } /** @@ -124,13 +105,7 @@ public class NodeHello extends BaseMessage { * -1 if the revision number could not be determined */ public int getExtRevisionNumber() { - String extRevision = getExtRevision(); - try { - return Integer.valueOf(extRevision); - } catch (NumberFormatException nfe1) { - /* ignore. */ - } - return -1; + return FcpUtils.safeParseInt(getExtRevision()); } /** @@ -177,13 +152,7 @@ public class NodeHello extends BaseMessage { * revision number coult not be determined */ public int getRevisionNumber() { - String revision = getRevision(); - try { - return Integer.valueOf(revision); - } catch (NumberFormatException nfe1) { - /* ignore. */ - } - return -1; + return FcpUtils.safeParseInt(getRevision()); } /** diff --git a/src/net/pterodactylus/util/fcp/PeerNote.java b/src/net/pterodactylus/util/fcp/PeerNote.java index bac39bd..276e33c 100644 --- a/src/net/pterodactylus/util/fcp/PeerNote.java +++ b/src/net/pterodactylus/util/fcp/PeerNote.java @@ -37,12 +37,11 @@ public class PeerNote extends BaseMessage { /** * Returns the type of the peer note. * - * @return The type of the peer note - * @throws NumberFormatException - * if the field can not be parsed + * @return The type of the peer note, or -1 if the type can + * not be parsed */ - public int getPeerNoteType() throws NumberFormatException { - return Integer.valueOf(getField("PeerNoteType")); + public int getPeerNoteType() { + return FcpUtils.safeParseInt(getField("PeerNoteType")); } } diff --git a/src/net/pterodactylus/util/fcp/PersistentPut.java b/src/net/pterodactylus/util/fcp/PersistentPut.java index 40843b9..ee56439 100644 --- a/src/net/pterodactylus/util/fcp/PersistentPut.java +++ b/src/net/pterodactylus/util/fcp/PersistentPut.java @@ -38,11 +38,7 @@ public class PersistentPut extends BaseMessage { * length could not be parsed */ public long getDataLength() { - try { - return Long.valueOf(getField("DataLength")); - } catch (NumberFormatException nfe1) { - return -1; - } + return FcpUtils.safeParseLong(getField("DataLength")); } /** @@ -73,12 +69,7 @@ public class PersistentPut extends BaseMessage { * the number of retries could not be parsed */ public int getMaxRetries() { - try { - return Integer.valueOf(getField("MaxRetries")); - } catch (NumberFormatException nfe1) { - /* we need to return -2 here as -1 is also a valid value. */ - return -2; - } + return FcpUtils.safeParseInt(getField("MaxRetries")); } /** diff --git a/src/net/pterodactylus/util/fcp/ProtocolError.java b/src/net/pterodactylus/util/fcp/ProtocolError.java index c812d42..c29e399 100644 --- a/src/net/pterodactylus/util/fcp/ProtocolError.java +++ b/src/net/pterodactylus/util/fcp/ProtocolError.java @@ -38,11 +38,7 @@ public class ProtocolError extends BaseMessage { * be parsed */ public int getCode() { - try { - return Integer.valueOf(getField("Code")); - } catch (NumberFormatException nfe1) { - return -1; - } + return FcpUtils.safeParseInt(getField("Code")); } /** diff --git a/src/net/pterodactylus/util/fcp/SimpleProgress.java b/src/net/pterodactylus/util/fcp/SimpleProgress.java index caf8065..ad775fb 100644 --- a/src/net/pterodactylus/util/fcp/SimpleProgress.java +++ b/src/net/pterodactylus/util/fcp/SimpleProgress.java @@ -45,11 +45,7 @@ public class SimpleProgress extends BaseMessage { * @return The total number of blocks */ public int getTotal() { - try { - return Integer.valueOf(getField("Total")); - } catch (NumberFormatException nfe1) { - return -1; - } + return FcpUtils.safeParseInt(getField("Total")); } /** @@ -61,11 +57,7 @@ public class SimpleProgress extends BaseMessage { * @return The number of required blocks */ public int getRequired() { - try { - return Integer.valueOf(getField("Required")); - } catch (NumberFormatException nfe1) { - return -1; - } + return FcpUtils.safeParseInt(getField("Required")); } /** @@ -74,11 +66,7 @@ public class SimpleProgress extends BaseMessage { * @return The number of failed blocks */ public int getFailed() { - try { - return Integer.valueOf(getField("Failed")); - } catch (NumberFormatException nfe1) { - return -1; - } + return FcpUtils.safeParseInt(getField("Failed")); } /** @@ -88,11 +76,7 @@ public class SimpleProgress extends BaseMessage { * @return The number of fatally failed blocks */ public int getFatallyFailed() { - try { - return Integer.valueOf(getField("FatallyFailed")); - } catch (NumberFormatException nfe1) { - return -1; - } + return FcpUtils.safeParseInt(getField("FatallyFailed")); } /** @@ -101,11 +85,7 @@ public class SimpleProgress extends BaseMessage { * @return The number of succeeded blocks */ public int getSucceeded() { - try { - return Integer.valueOf(getField("Succeeded")); - } catch (NumberFormatException nfe1) { - return -1; - } + return FcpUtils.safeParseInt(getField("Succeeded")); } /** diff --git a/src/net/pterodactylus/util/fcp/StartedCompression.java b/src/net/pterodactylus/util/fcp/StartedCompression.java index 7032ee0..a18e39b 100644 --- a/src/net/pterodactylus/util/fcp/StartedCompression.java +++ b/src/net/pterodactylus/util/fcp/StartedCompression.java @@ -55,11 +55,7 @@ public class StartedCompression extends BaseMessage { * codec could not be parsed */ public int getCodec() { - try { - return Integer.valueOf(getField("Codec")); - } catch (NumberFormatException nfe1) { - return -1; - } + return FcpUtils.safeParseInt(getField("Codec")); } }