* not be parsed
*/
public long getDataLength() {
- try {
- return Long.valueOf(getField("DataLength"));
- } catch (NumberFormatException nfe1) {
- return -1;
- }
+ return FcpUtils.safeParseLong(getField("DataLength"));
}
/**
* 1970 UTC), or <code>-1</code> 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"));
}
/**
* 1970 UTC), or <code>-1</code> 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"));
}
/**
return encodedField.toString();
}
+ /**
+ * Tries to parse the given string into an int, returning <code>-1</code>
+ * if the string can not be parsed.
+ *
+ * @param value
+ * The string to parse
+ * @return The parsed int, or <code>-1</code>
+ */
+ public static int safeParseInt(String value) {
+ return safeParseInt(value, -1);
+ }
+
+ /**
+ * Tries to parse the given string into an int, returning
+ * <code>defaultValue</code> 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 <code>defaultValue</code>
+ */
+ 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 <code>-1</code>
+ * if the string can not be parsed.
+ *
+ * @param value
+ * The string to parse
+ * @return The parsed long, or <code>-1</code>
+ */
+ public static long safeParseLong(String value) {
+ return safeParseLong(value, -1);
+ }
+
+ /**
+ * Tries to parse the given string into an long, returning
+ * <code>defaultValue</code> 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 <code>defaultValue</code>
+ */
+ public static long safeParseLong(String value, long defaultValue) {
+ try {
+ return Integer.valueOf(value);
+ } catch (NumberFormatException nfe1) {
+ return defaultValue;
+ }
+ }
+
}
*/
package net.pterodactylus.util.fcp;
-
/**
* Some convenience methods for parsing a “NodeHello” message from the node.
*
* 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());
}
/**
* 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());
}
/**
* 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());
}
/**
* <code>-1</code> 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());
}
/**
* 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());
}
/**
/**
* 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 <code>-1</code> 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"));
}
}
* length could not be parsed
*/
public long getDataLength() {
- try {
- return Long.valueOf(getField("DataLength"));
- } catch (NumberFormatException nfe1) {
- return -1;
- }
+ return FcpUtils.safeParseLong(getField("DataLength"));
}
/**
* 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"));
}
/**
* be parsed
*/
public int getCode() {
- try {
- return Integer.valueOf(getField("Code"));
- } catch (NumberFormatException nfe1) {
- return -1;
- }
+ return FcpUtils.safeParseInt(getField("Code"));
}
/**
* @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"));
}
/**
* @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"));
}
/**
* @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"));
}
/**
* @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"));
}
/**
* @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"));
}
/**
* codec could not be parsed
*/
public int getCodec() {
- try {
- return Integer.valueOf(getField("Codec"));
- } catch (NumberFormatException nfe1) {
- return -1;
- }
+ return FcpUtils.safeParseInt(getField("Codec"));
}
}