X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fnet%2Fpterodactylus%2Futil%2Ffcp%2FFcpUtils.java;h=3a5d5baf55bf8d87fea6fe75d9a467c1b7b9a16b;hb=efceb56d8cedd9e18e599a2f2d1e21d64aacc002;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..3a5d5ba 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,43 @@ 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(); + } + }