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();
69 * Encodes the given string array into a string, separating the values by
73 * The values to encode
74 * @return The encoded values
76 public static String encodeMultiStringField(String[] values) {
77 StringBuilder encodedField = new StringBuilder();
78 for (String value: values) {
79 if (encodedField.length() > 0) {
80 encodedField.append(';');
82 encodedField.append(value);
84 return encodedField.toString();
88 * Tries to parse the given string into an int, returning <code>-1</code>
89 * if the string can not be parsed.
93 * @return The parsed int, or <code>-1</code>
95 public static int safeParseInt(String value) {
96 return safeParseInt(value, -1);
100 * Tries to parse the given string into an int, returning
101 * <code>defaultValue</code> if the string can not be parsed.
104 * The string to parse
105 * @param defaultValue
106 * The value to return if the string can not be parsed.
107 * @return The parsed int, or <code>defaultValue</code>
109 public static int safeParseInt(String value, int defaultValue) {
111 return Integer.valueOf(value);
112 } catch (NumberFormatException nfe1) {
118 * Tries to parse the given string into an long, returning <code>-1</code>
119 * if the string can not be parsed.
122 * The string to parse
123 * @return The parsed long, or <code>-1</code>
125 public static long safeParseLong(String value) {
126 return safeParseLong(value, -1);
130 * Tries to parse the given string into an long, returning
131 * <code>defaultValue</code> if the string can not be parsed.
134 * The string to parse
135 * @param defaultValue
136 * The value to return if the string can not be parsed.
137 * @return The parsed long, or <code>defaultValue</code>
139 public static long safeParseLong(String value, long defaultValue) {
141 return Integer.valueOf(value);
142 } catch (NumberFormatException nfe1) {