add error messages for missing font selections
[jSite2.git] / src / net / pterodactylus / util / fcp / FcpUtils.java
index 9892610..ad2a798 100644 (file)
@@ -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 <code>-1</code>
+        * if the string can not be parsed.
+        * 
+        * @param value
+        *            The string to parse
+        * @return The parsed int, or <code>-1</code>
+        */
+       public static int safeParseInt(String value) {
+               return safeParseInt(value, -1);
+       }
+
+       /**
+        * Tries to parse the given string into an int, returning
+        * <code>defaultValue</code> 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 <code>defaultValue</code>
+        */
+       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 <code>-1</code>
+        * if the string can not be parsed.
+        * 
+        * @param value
+        *            The string to parse
+        * @return The parsed long, or <code>-1</code>
+        */
+       public static long safeParseLong(String value) {
+               return safeParseLong(value, -1);
+       }
+
+       /**
+        * Tries to parse the given string into an long, returning
+        * <code>defaultValue</code> 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 <code>defaultValue</code>
+        */
+       public static long safeParseLong(String value, long defaultValue) {
+               try {
+                       return Integer.valueOf(value);
+               } catch (NumberFormatException nfe1) {
+                       return defaultValue;
+               }
+       }
+
 }