X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fnet%2Fpterodactylus%2Futil%2Ffcp%2FFcpUtils.java;h=ad2a7983a040ad00410e6f24b943e3b0fe703437;hb=9d55936cc51f39b1f45d28b7872ddc712f7c9b2f;hp=a492f2e07874460d72adeab5efca89d8628593d9;hpb=9dbb34874ce7cd4fa4703f6bde00302bef2fb295;p=jSite2.git diff --git a/src/net/pterodactylus/util/fcp/FcpUtils.java b/src/net/pterodactylus/util/fcp/FcpUtils.java index a492f2e..ad2a798 100644 --- a/src/net/pterodactylus/util/fcp/FcpUtils.java +++ b/src/net/pterodactylus/util/fcp/FcpUtils.java @@ -35,7 +35,7 @@ public class FcpUtils { * @throws NumberFormatException * if a value can not be converted to a number */ - public static int[] parseMultiIntegerField(String field) throws NumberFormatException { + public static int[] decodeMultiIntegerField(String field) throws NumberFormatException { StringTokenizer fieldTokens = new StringTokenizer(field, ";"); int[] result = new int[fieldTokens.countTokens()]; int counter = 0; @@ -46,4 +46,102 @@ public class FcpUtils { return result; } + /** + * Encodes the given integer array into a string, separating the values by + * ‘;’. + * + * @param values + * The values to encode + * @return The encoded values + */ + public static String encodeMultiIntegerField(int[] values) { + StringBuilder encodedField = new StringBuilder(); + for (int value: values) { + if (encodedField.length() > 0) { + encodedField.append(';'); + } + encodedField.append(value); + } + return encodedField.toString(); + } + + /** + * Encodes the given string array into a string, separating the values by + * ‘;’. + * + * @param values + * The values to encode + * @return The encoded values + */ + public static String encodeMultiStringField(String[] values) { + StringBuilder encodedField = new StringBuilder(); + for (String value: values) { + if (encodedField.length() > 0) { + encodedField.append(';'); + } + encodedField.append(value); + } + 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; + } + } + }