implement node addition and removal events
[jSite2.git] / src / net / pterodactylus / util / fcp / FcpUtils.java
1 /**
2  * © 2008 INA Service GmbH
3  */
4 package net.pterodactylus.util.fcp;
5
6 import java.util.StringTokenizer;
7 import java.util.concurrent.atomic.AtomicLong;
8
9 /**
10  * Helper class with utility methods for the FCP protocol.
11  * 
12  * @author <a href="mailto:dr@ina-germany.de">David Roden</a>
13  * @version $Id$
14  */
15 public class FcpUtils {
16
17         /** Counter for unique identifiers. */
18         private static AtomicLong counter = new AtomicLong();
19
20         /**
21          * Returns a unique identifier.
22          * 
23          * @return A unique identifier
24          */
25         public static String getUniqueIdentifier() {
26                 return new StringBuilder().append(System.currentTimeMillis()).append('-').append(counter.getAndIncrement()).toString();
27         }
28
29         /**
30          * Parses an integer field, separated by ‘;’ and returns the parsed values.
31          * 
32          * @param field
33          *            The field to parse
34          * @return An array with the parsed values
35          * @throws NumberFormatException
36          *             if a value can not be converted to a number
37          */
38         public static int[] decodeMultiIntegerField(String field) throws NumberFormatException {
39                 StringTokenizer fieldTokens = new StringTokenizer(field, ";");
40                 int[] result = new int[fieldTokens.countTokens()];
41                 int counter = 0;
42                 while (fieldTokens.hasMoreTokens()) {
43                         String fieldToken = fieldTokens.nextToken();
44                         result[counter++] = Integer.valueOf(fieldToken);
45                 }
46                 return result;
47         }
48
49         /**
50          * Encodes the given integer array into a string, separating the values by
51          * ‘;’.
52          * 
53          * @param values
54          *            The values to encode
55          * @return The encoded values
56          */
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(';');
62                         }
63                         encodedField.append(value);
64                 }
65                 return encodedField.toString();
66         }
67
68         /**
69          * Encodes the given string array into a string, separating the values by
70          * ‘;’.
71          * 
72          * @param values
73          *            The values to encode
74          * @return The encoded values
75          */
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(';');
81                         }
82                         encodedField.append(value);
83                 }
84                 return encodedField.toString();
85         }
86
87         /**
88          * Tries to parse the given string into an int, returning <code>-1</code>
89          * if the string can not be parsed.
90          * 
91          * @param value
92          *            The string to parse
93          * @return The parsed int, or <code>-1</code>
94          */
95         public static int safeParseInt(String value) {
96                 return safeParseInt(value, -1);
97         }
98
99         /**
100          * Tries to parse the given string into an int, returning
101          * <code>defaultValue</code> if the string can not be parsed.
102          * 
103          * @param value
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>
108          */
109         public static int safeParseInt(String value, int defaultValue) {
110                 try {
111                         return Integer.valueOf(value);
112                 } catch (NumberFormatException nfe1) {
113                         return defaultValue;
114                 }
115         }
116
117         /**
118          * Tries to parse the given string into an long, returning <code>-1</code>
119          * if the string can not be parsed.
120          * 
121          * @param value
122          *            The string to parse
123          * @return The parsed long, or <code>-1</code>
124          */
125         public static long safeParseLong(String value) {
126                 return safeParseLong(value, -1);
127         }
128
129         /**
130          * Tries to parse the given string into an long, returning
131          * <code>defaultValue</code> if the string can not be parsed.
132          * 
133          * @param value
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>
138          */
139         public static long safeParseLong(String value, long defaultValue) {
140                 try {
141                         return Integer.valueOf(value);
142                 } catch (NumberFormatException nfe1) {
143                         return defaultValue;
144                 }
145         }
146
147 }