X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fnet%2Fpterodactylus%2Futil%2Ffcp%2FFcpUtils.java;h=ad2a7983a040ad00410e6f24b943e3b0fe703437;hb=9d55936cc51f39b1f45d28b7872ddc712f7c9b2f;hp=989261001bf1924d1b66a7f97402bcfd3cef987d;hpb=84c226bae470c8779ae699d7b0f50bfe196b450c;p=jSite2.git diff --git a/src/net/pterodactylus/util/fcp/FcpUtils.java b/src/net/pterodactylus/util/fcp/FcpUtils.java index 9892610..ad2a798 100644 --- a/src/net/pterodactylus/util/fcp/FcpUtils.java +++ b/src/net/pterodactylus/util/fcp/FcpUtils.java @@ -3,6 +3,7 @@ */ package net.pterodactylus.util.fcp; +import java.util.StringTokenizer; import java.util.concurrent.atomic.AtomicLong; /** @@ -25,4 +26,122 @@ public class FcpUtils { return new StringBuilder().append(System.currentTimeMillis()).append('-').append(counter.getAndIncrement()).toString(); } + /** + * Parses an integer field, separated by ‘;’ and returns the parsed values. + * + * @param field + * The field to parse + * @return An array with the parsed values + * @throws NumberFormatException + * if a value can not be converted to a number + */ + public static int[] decodeMultiIntegerField(String field) throws NumberFormatException { + StringTokenizer fieldTokens = new StringTokenizer(field, ";"); + int[] result = new int[fieldTokens.countTokens()]; + int counter = 0; + while (fieldTokens.hasMoreTokens()) { + String fieldToken = fieldTokens.nextToken(); + result[counter++] = Integer.valueOf(fieldToken); + } + 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; + } + } + }