changeset 2216:31fa7cae0f35

Added new helper methods to FLYSUtils that extract int[] and double[] from string. flys-artifacts/trunk@3855 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Wed, 01 Feb 2012 12:21:05 +0000
parents ebbb18ed78c4
children 54422e31dc27
files flys-artifacts/ChangeLog flys-artifacts/src/main/java/de/intevation/flys/utils/FLYSUtils.java
diffstat 2 files changed, 69 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Wed Feb 01 11:14:24 2012 +0000
+++ b/flys-artifacts/ChangeLog	Wed Feb 01 12:21:05 2012 +0000
@@ -1,3 +1,8 @@
+2012-02-01  Ingo Weinzierl <ingo@intevation.de>
+
+	* src/main/java/de/intevation/flys/utils/FLYSUtils.java: Added helper
+	  methods to extract int[] and double[] from string.
+
 2012-02-01  Ingo Weinzierl <ingo@intevation.de>
 
 	* doc/conf/artifacts/winfo.xml: Registered new facet type
--- a/flys-artifacts/src/main/java/de/intevation/flys/utils/FLYSUtils.java	Wed Feb 01 11:14:24 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/utils/FLYSUtils.java	Wed Feb 01 12:21:05 2012 +0000
@@ -17,6 +17,7 @@
 import org.hibernate.impl.SessionFactoryImpl;
 
 import gnu.trove.TDoubleArrayList;
+import gnu.trove.TIntArrayList;
 
 import de.intevation.artifacts.Artifact;
 import de.intevation.artifacts.CallContext;
@@ -716,5 +717,68 @@
             return wqkms[idx];
         }
     }
+
+
+    /**
+     * This method transform a string into an int array. Therefore, the string
+     * <i>raw</i> must consist of int values separated by a <i>';'</i>.
+     *
+     * @param raw The raw integer array as string separated by a ';'.
+     *
+     * @return an array of int values.
+     */
+    public static int[] intArrayFromString(String raw) {
+        String[] splitted = raw != null ? raw.split(";") : null;
+
+        if (splitted == null || splitted.length == 0) {
+            logger.warn("No integer values found in '" + raw + "'");
+            return new int[0];
+        }
+
+        TIntArrayList integers = new TIntArrayList(splitted.length);
+
+        for (String value: splitted) {
+            try {
+                integers.add(Integer.valueOf(value));
+            }
+            catch (NumberFormatException nfe) {
+                logger.warn("Parsing integer failed: " + nfe);
+            }
+        }
+
+        return integers.toNativeArray();
+    }
+
+
+    /**
+     * This method transform a string into an double array. Therefore, the
+     * string <i>raw</i> must consist of double values separated by a
+     * <i>';'</i>.
+     *
+     * @param raw The raw double array as string separated by a ';'.
+     *
+     * @return an array of double values.
+     */
+    public static double[] doubleArrayFromString(String raw) {
+        String[] splitted = raw != null ? raw.split(";") : null;
+
+        if (splitted == null || splitted.length == 0) {
+            logger.warn("No double values found in '" + raw + "'");
+            return new double[0];
+        }
+
+        TDoubleArrayList doubles = new TDoubleArrayList(splitted.length);
+
+        for (String value: splitted) {
+            try {
+                doubles.add(Double.valueOf(value));
+            }
+            catch (NumberFormatException nfe) {
+                logger.warn("Parsing double failed: " + nfe);
+            }
+        }
+
+        return doubles.toNativeArray();
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org