2 * © 2008 INA Service GmbH
4 package net.pterodactylus.util.fcp;
6 import java.util.StringTokenizer;
7 import java.util.concurrent.atomic.AtomicLong;
10 * Helper class with utility methods for the FCP protocol.
12 * @author <a href="mailto:dr@ina-germany.de">David Roden</a>
15 public class FcpUtils {
17 /** Counter for unique identifiers. */
18 private static AtomicLong counter = new AtomicLong();
21 * Returns a unique identifier.
23 * @return A unique identifier
25 public static String getUniqueIdentifier() {
26 return new StringBuilder().append(System.currentTimeMillis()).append('-').append(counter.getAndIncrement()).toString();
30 * Parses an integer field, separated by ‘;’ and returns the parsed values.
34 * @return An array with the parsed values
35 * @throws NumberFormatException
36 * if a value can not be converted to a number
38 public static int[] decodeMultiIntegerField(String field) throws NumberFormatException {
39 StringTokenizer fieldTokens = new StringTokenizer(field, ";");
40 int[] result = new int[fieldTokens.countTokens()];
42 while (fieldTokens.hasMoreTokens()) {
43 String fieldToken = fieldTokens.nextToken();
44 result[counter++] = Integer.valueOf(fieldToken);
50 * Encodes the given integer array into a string, separating the values by
54 * The values to encode
55 * @return The encoded values
57 public static String encodeMultiIntegerField(int[] values) {
58 StringBuilder encodedField = new StringBuilder();
59 for (int value: values) {
60 if (encodedField.length() > 0) {
61 encodedField.append(';');
63 encodedField.append(value);
65 return encodedField.toString();